#1 2016-08-23 21:36:07

edunt
Member
Registered: 2015-12-21
Posts: 258

A little of PHP (server side language) and the "sprintf" function

Hi David,

What could be the issue with this line of code?

$message = sprintf("%s <%s>\n\n%s", $name, $email, $comments);

The print is just <> but I am expecting the send name and his/her comment.

Thanks

Offline

#2 2016-08-23 22:19:07

David
Admin
From: Alcobendas, Madrid, Spain
Registered: 2015-04-21
Posts: 1,657
Website

Re: A little of PHP (server side language) and the "sprintf" function

Hello Ade,

At a first view, the "sprintf" function format string appear good, then, if you get "empty" strings, there cannot be other reason than the variables "$name", "$email" and "$comments" are not filled as you expected.

Offline

#3 2016-09-01 12:33:44

edunt
Member
Registered: 2015-12-21
Posts: 258

Re: A little of PHP (server side language) and the "sprintf" function

Hi,

I read about sprintf on the website and after implement the idea I got from their on AB.

This error message came displayed

error.png

What could be the issue?

Offline

#4 2016-09-01 12:38:15

David
Admin
From: Alcobendas, Madrid, Spain
Registered: 2015-04-21
Posts: 1,657
Website

Re: A little of PHP (server side language) and the "sprintf" function

Hello Ade,

Please, can you provide me with a little application sample in order to try to reproduce the problem here? I can't figure how to help you without an small sample Ade...

Offline

#5 2016-09-01 12:55:26

edunt
Member
Registered: 2015-12-21
Posts: 258

Re: A little of PHP (server side language) and the "sprintf" function

Hi David,

Check your email I have sent the sample project I am playing with.

I tried this php script too

<?php
session_start();
if( isset($_POST['name']) && ($_POST['email']) && $_POST['comments'] )
{
	$to = 'myemail@gmail.com'; // Replace with your email	
	$subject = 'Message from website'; // Replace with your $subject
	$headers = 'From: ' . $_POST['email'] . "\r\n" . 'Reply-To: ' . $_POST['email'];	
	
	$message = 'Name: ' . $_POST['name'] . "\n" .
	           'E-mail: ' . $_POST['email'] . "\n" .
	           'Subject: ' . $_POST['subject'] . "\n" .
	           'Message: ' . $_POST['message'];
	
	mail($to, $subject, $message, $headers);	
	if( $_POST['copy'] == 'on' )
	{
		mail($_POST['email'], $subject, $message);
	}
}
?>

Last edited by edunt (2016-09-01 13:06:45)

Offline

#6 2016-09-01 13:06:19

David
Admin
From: Alcobendas, Madrid, Spain
Registered: 2015-04-21
Posts: 1,657
Website

Re: A little of PHP (server side language) and the "sprintf" function

Hello Ade,

Two things to note:

1º Your script must respond with an "http_response_code(200);" to avoid the Http Client's Error event to be fired.

2º Take care with the email input. Since they are an email input, for example, Internet Explorer put a value of "undefined" if the input's value is not a valid email address. May you can use the IsEmail action in the application.

Hope this information can help to solve your problem!

Offline

#7 2016-09-01 16:53:16

edunt
Member
Registered: 2015-12-21
Posts: 258

Re: A little of PHP (server side language) and the "sprintf" function

Hi,

I am your PHP script "AS IS" that come with AB.

1. This is line code has take care of respond with an "http_response_code(200);" correct me if I am wrong.

if (mail('myemail@domainname.com', 'Send The Message To My Inbox', $message)) {
    // Mail sent
    http_response_code(200);
  } else 
........

 
2. The email input has been taking care with this line of code:

$email = filter_input(INPUT_POST, 'EmailInput', FILTER_VALIDATE_EMAIL); and

IfEx "([NameInput.Value] == '') || ([EmailInput.Value] == '') || ([CommentsTextarea.Value] == '')"

  // If not, advert to the user
  AlertBox "Please, provide all the feedback information" "warning"
Else

Regarding the Internet Explorer I am not consider it at the moment because this will be use only in Android platform.

After could of try and error in the PHP script. I have not got it working.

Last edited by edunt (2016-09-01 16:55:08)

Offline

#8 2016-09-01 20:17:32

David
Admin
From: Alcobendas, Madrid, Spain
Registered: 2015-04-21
Posts: 1,657
Website

Re: A little of PHP (server side language) and the "sprintf" function

Hello Ade,

The "feedback.php" script including in the Calculator sample is the below one:

<?php

// Enable CORS (http://enable-cors.org/server_php.html)
header('Access-Control-Allow-Origin: *');

// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
  if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
    header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
  exit;
}

// Recolect and validate the input data
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
$comments = filter_input(INPUT_POST, 'comments', FILTER_SANITIZE_SPECIAL_CHARS);

// Test if the input data is valid
if (($email !== false) && ($name !== false) && ($comments !== false)) {

  // Prepare our mail message
  $message = sprintf("%s <%s>\n\n%s", $name, $email, $comments);

  if (mail('info@davidesperalta.com', 'App Builder Calculator Feedback', $message)) {
    // Mail sent
    http_response_code(200);
  } else {
    // mail function fail!
    http_response_code(500);
  }
} else {
  // Invalid input data
  http_response_code(406);
}

As you can see we play with response codes, then we can use such response codes our the app.

About the email validation, I note that the variable can become "undefined", which is not "" (empty) but a different thing. Anyway, we always must validate the user's input also in the app's server.

Offline

#9 2016-09-01 22:07:00

edunt
Member
Registered: 2015-12-21
Posts: 258

Re: A little of PHP (server side language) and the "sprintf" function

Hi David,

Thanks issued solved.

AB Ninja rock!

Offline

#10 2016-09-01 23:01:18

David
Admin
From: Alcobendas, Madrid, Spain
Registered: 2015-04-21
Posts: 1,657
Website

Re: A little of PHP (server side language) and the "sprintf" function

Hello!

edunt wrote:

Hi David,

Thanks issued solved.

AB Ninja rock!

Glad to know!

Offline

Board footer

Powered by FluxBB


Copyright ©2018 DecSoft. All rights reserved.