SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    May 2013
    Posts
    199
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    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?

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,412
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    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.

  3. #3
    SitePoint Zealot
    Join Date
    May 2013
    Posts
    199
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <?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\nReply-To:{$email}"; 
    $headers .= "\r\nContent-Type: text/html; charset=UTF-8";
    $headers .= "\r\nMIME-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();
    ?>

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,412
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    The problem is here:
    PHP Code:
    $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:
    PHP Code:
    function spamcheck($field) {
        
    $field filter_var($fieldFILTER_SANITIZE_EMAIL);
        return 
    filter_var($fieldFILTER_VALIDATE_EMAIL); // IF check was redundant, return boolean value directly
    }

    $yourJSONString $_POST["jsonstring"];

    $array json_decode($yourJSONStringtrue);

    $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\nReply-To:{$email}"
        
    $headers .= "\r\nContent-Type: text/html; charset=UTF-8";
        
    $headers .= "\r\nMIME-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:
    Code JavaScript:
    $(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:
    PHP Code:
    //$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']; 

  5. #5
    SitePoint Zealot
    Join Date
    May 2013
    Posts
    199
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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?

  6. #6
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,412
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by lse123 View Post
    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?

  7. #7
    SitePoint Zealot
    Join Date
    May 2013
    Posts
    199
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    php statements for get values from post ajax request, required be outside a php function?

  8. #8
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,412
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    No, $_POST and $_GET are global variables, so they are available from anywhere.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •