0

I have following html form:

 <form method="post" action="">
    <input type="hidden" name="userid" id="user" value="<?php echo $user ?>"/>
    <textarea name="comment" class="subcomment_form" id="ctextarea<?php echo $suggestid ?>"></textarea>
    <input type="submit" value="Post" id="<?php echo $suggestid ?>" class="form_btn" style="margin-top: 5px"/>
 </form>

following JS code:

$('.form_btn').live("click",function() 
{

var ID = $(this).attr("id");

var user= $("input[name=userid]").val();
var comment= $("#ctextarea"+ID).val();
var dataString = 'comment='+ comment + '&suggestid=' + ID + 'user' + user;

if(comment=='')
{
alert("Please Enter Comment Text");
}
else
{
$.ajax({
type: "POST",
url: "action/subcomment.php",
data: dataString,
cache: false,
success: function(html){
$("#commentload"+ID).append("html");
$("#ctextarea"+ID).val('');
$("#ctextarea"+ID).focus();
 }
 });
}
return false;
});

and following php code for subcomment.php file:

if($_POST['comment'])
{
$comment=$_POST['comment'];
$suggestid=$_POST['suggestid'];
$user=$_POST['user'];
$sql = "INSERT INTO user_suggestions_comments (uvd_id, user_id, usc_comment) VALUES ('".$_POST['suggestid']."', '".$_POST['user']."','".$_POST['comment']."')";
mysql_query( $sql);
}

the problem I have is that value from <input type="hidden" name="userid" id="user" value="<?php echo $user ?>"/> is not passed on php file and textarea content is passed. What I need to change inside that JS code to make it work?

1 Answer 1

1

It looks like your dataString is not being built to what your PHP code is expecting. Try this change.

var dataString = 'comment='+ comment + '&suggestid=' + ID + 'user' + user;

becomes

var dataString = 'comment='+ comment + '&suggestid=' + ID + '&user=' + user;

You are looking for three values from your $_POST and only passing 2

$comment=$_POST['comment'];
$suggestid=$_POST['suggestid'];
$user=$_POST['user'];
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah I was assuming that he just passed the form as is I dont know why he is changing it into a data string...
thanks a lot to both of you, by adding & in front of user worked :)
It looks like he's changing it to a data string because the suggestId is built from data that wouldn't be submitted normally. But I would say it's best to keep the names in the data string the same as if the form were submitted naturally. Just makes things clearer.

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.