Boris Zegarac
Boris Zegarac

Reputation: 531

passing input value using jquery on mysql query

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?

Upvotes: 0

Views: 1823

Answers (1)

CLo
CLo

Reputation: 3730

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'];

Upvotes: 1

Related Questions