0

I was trying to pass the variable thecode, which is in the table using jquery into the function named getComments(). My code has as following. First I have my jquery script which is this:

$(document).ready(function(){                           
    $("#comment_process").click(function(){
        if($("#comment_text").val() != ""){ 
         $('.post_loader').show();
            $.post("comments_business.php?action=post", { 
                comment: $("#comment_text").val() 
            }, function(data) {
                $(".comments").hide().html(data).fadeIn('slow');
                $("#comment_text").val("");
                $('.post_loader').hide();
            });
        } 
    });   
}); 

Next I have the following script with html and php:

<!--- more code at the top---->
<?php $auto = $profile_data_business['business_code'];  ?>
<table>                             
    <textarea rows="3"  id="comment_text" placeholder="share an update."></textarea>
    <input type="" id="comment_code" name="thecode"  value="<?php echo $auto; ?>" />                            
    <input type="button" id="comment_process" /> 
</table>
<div class="comments"><?php include_once("comments_business.php");?>   </div> 

the page named comments_business.php includes a function which is the following:

<?php
function getComments(){     
    $session_user_id = $_SESSION['user_id'];
    $comments = "";
    // can't get variable $thisemail
    $thisemail = mysql_real_escape_string($_POST['thecode']);
    $sql = mysql_query("SELECT * FROM comments_business WHERE ( `flag`=0 and `user`='$thisemail' and  `comments_id` NOT IN (SELECT `comments_id` FROM `hide_comment_business` where `user`='$session_user_id') ) ORDER BY comment_date DESC LIMIT 40") or die (mysql_error());
    //more code here            
    return $comments;       
}   
?>

Any idea how should I change my jquery code so that I will be able to pass $thisemail variable successfully into getComments() function?

5
  • question is, how/where are you calling the getComments() function and has the session been started? Commented Oct 27, 2015 at 17:57
  • yes the session has started Commented Oct 27, 2015 at 18:00
  • you also don't have a type in <input type="" id="comment_code" name="thecode" it's blank. check for errors, check your console. Commented Oct 27, 2015 at 18:01
  • it is type="hidden" but I left it empty so that I could see if it passes the value Commented Oct 27, 2015 at 18:03
  • @33528 If your problem solved and my answer were useful, please accept it. Commented May 16, 2016 at 8:47

1 Answer 1

1
  • When you use $.post don't need to write GET parameter in URL (action=post).
  • When you post data by comment name, you must get data by some name in php ($_POST['comment']).
  • When you use ajax shouldn't use function in php or call function after defintion.
  • When you use ajax must print or echo data in php file to display in post result.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.