Post/json/php seems not to working, I think is php problem

georgiosrafaelavilla.com/main2carousel3.html here quick contact at the bottom right corner seems not to working, I think is php problem… post here php? by the way if have a comments textarea how handle in json and later php?

Hi,

I’ve just tried the contact form on your site, and it seems to POST the data OK, but the response from the server is false. I’d need to see your php code from the quickContactUs.php file.

<?php
function spamcheck($field)
{
  //filter_var() sanitizes the e-mail
  //address using FILTER_SANITIZE_EMAIL
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);

  //filter_var() validates the e-mail
  //address using FILTER_VALIDATE_EMAIL
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return true;
    }
  else
    {
    return false;
    }
}



/*$email = $_REQUEST['email'];
$comments = $_REQUEST['comments'];*/
	
$yourJSONString = $_POST["jsonstring"];	

$array = json_decode($yourJSONString, true);	

$email = $array['email'];	
$comments = $array['comments'];

$message = "<p>Thank you for your Quick Contact Us Request.</p><p>Email: {$email}</p><p>Comments: {$comments}</p><p>Yours, <br />W Villa - Owner</p>";

function quickContactProcess() {

/*if (isset($_REQUEST['email']) && isset($_REQUEST['comments']))
{*/


//check if the email address is invalid

$mailcheck = spamcheck($email);

	if ($mailcheck==FALSE)
		{
			$validEmail = false;
		}
	else $validEmail = true;


//$validEmail = true;

if ($validEmail == true)
  {

$subject = "Quick Contact - w Villa";

$to = "webmaster@wwwwwwww.eu,{$email}";

$headers = "From:info@wwwwwww.com";
$headers .= "\\r\
Reply-To:{$email}";
$headers .= "\\r\
Content-Type: text/html; charset=UTF-8";
$headers .= "\\r\
MIME-Version: 1.0";

mail($to,$subject,$message,$headers);

}
else $validEmail = false;


//
//}

if ($validEmail == true)
  {
    $response="true";
  }
else
  {
  $response="false";
  }

//output the response
echo $response;

}

quickContactProcess();
?>

The problem is here:


$email = $array['email'];	
$comments = $array['comments'];

$message = "<p>Thank you for your Quick Contact Us Request.</p><p>Email: {$email}</p><p>Comments: {$comments}</p><p>Yours, <br />W Villa - Owner</p>";

function quickContactProcess() {

You’re assigning your variables $email, $comments and $message outside of the function that wants to use them, so they’re out of scope. You could just pass the variables into the quickContactProcess function, which would solve the problem, but I think you could actually simplify the whole script like this:


function spamcheck($field) {
    $field = filter_var($field, FILTER_SANITIZE_EMAIL);
    return filter_var($field, FILTER_VALIDATE_EMAIL); // IF check was redundant, return boolean value directly
}

$yourJSONString = $_POST["jsonstring"];

$array = json_decode($yourJSONString, true);

$email = $array['email'];
$comments = $array['comments'];

// No need for multiple IF checks doing the same thing.. one will do
if (spamcheck($email))
{
    $subject = "Quick Contact - Georgios Rafaela Villa";
    $message = "<p>Thank you for your Quick Contact Us Request.</p><p>Email: {$email}</p>"
             . "<p>Comments: {$comments}</p><p>Yours, <br />W Villa - Owner</p>";
    $to = "webmaster@wwwwwwww.eu,{$email}";

    $headers = "From:info@wwwwwww.com";
    $headers .= "\\r\
Reply-To:{$email}";
    $headers .= "\\r\
Content-Type: text/html; charset=UTF-8";
    $headers .= "\\r\
MIME-Version: 1.0";

    mail($to, $subject, $message, $headers);

    echo "true";
}
else
{
    echo "false";
}

If you’re interested, you can simplify your PHP a little further, and replace a lot of unnecessary JS too. As you’re already including jQuery in your page, you can add this (just make sure it comes after you’ve included jQuery) which takes care of submitting the contact form:

$(function(){
    $("#quickContact").click(function(){
        var message = {
            email: $('#email').val(),
            comments: $('#comments').val()
        };
        $.post('quickContactUs.php', message, function(data) {
            if (data == "true") {
                alert("Your message sent!");
            } else {
                alert("Email NOT sent, please retry!");
            }
        });
    });
});

This way you avoid are sending your message as form data, and you avoid having to encode/decode JSON, so your php script changes a little:


//$yourJSONString = $_POST["jsonstring"];

//$array = json_decode($yourJSONString, true);

//$email = $array['email'];
//$comments = $array['comments'];

// The above code can be removed

$email = $_POST['email'];
$comments = $_POST['comments'];

thanks a lot, fixed with function quickContactProcess($email,$comments) { //and call appropriately…

Q: to get values from json can done inside the php function => with direct call function from php file?

I’m not sure what you’re asking, what is it that you want to do?

php statements for get values from post ajax request, required be outside a php function?

No, $_POST and $_GET are global variables, so they are available from anywhere.