The jquery post from a view's form to its PHP controller is not posting
开发者_JS百科What is to be the value of $id
is determined by a php file, 'afile.php', which gets to the view using include('view_file.php')
. I want the form in 'view_file.php' to post data back to 'afile.php' and use it to again determine the value of $id
.
From the 'view_file.php': Here is my jquery/javascript:
function vote()
{
$.post('afile.php',{ poll:$('input:radio[name=poll]:checked').val(), id: $('input:hidden[name=id]').val()});
};
Here is my form:
<form name='poll' id='poll'>
<INPUT TYPE="radio" name='poll' value='poll3' checked/>Yes
<INPUT TYPE="radio" name='poll' value='poll2'/>Done
<INPUT TYPE="radio" name='poll' value='poll1'/>No
<INPUT TYPE='button' value='submit' onClick="vote();" />
<INPUT TYPE='hidden' name='id' value='<?php echo $id; ?>'/>
</form>
From the 'afile.php':
if(isset($_POST['poll']))
{
//code
}
else
//code
include('view_file.php');
However, the post is never set, and I just cannot figure out why that is.
Any help/hints/suggestions appreciated.
EDIT: I realized I can do this easily without incorporating jquery/javascript. Just use the HTML POST method:
<form name='poll' action='afile.php' method='POST'>
<INPUT TYPE="radio" name='poll' value='poll3' checked/>Yes
<INPUT TYPE="radio" name='poll' value='poll2'/>Done
<INPUT TYPE="radio" name='poll' value='poll1'/>No
<INPUT TYPE='submit' value='submit' />
<INPUT TYPE='hidden' name='id' value='<?php echo $id; ?>'/>
</form>
You could just use $('formSelector').serialize()
to get the post data from the form directly, without needing to get the values for each input. Checkboxes and Radios only send POST data if they are :checked
.
Now, it doesn't matter what the form's name is, it isn't passed to $_POST, so your condition
if(isset($_POST['poll']))
Will always be false, since there is not input with the name of 'poll'. I generally add a name to the submit button if there are multiple forms submitted to the same page.
EDIT: When testing locally the following code worked flawlessly:
<?php
$id = 1;
if (!empty($_POST)) {
if (!empty($_POST['poll'])) {
echo "POLL IS NOT EMPTY!! \n";
var_dump($_POST);
die();
}
else {
echo "BOO! POLL IS EMPTY! \n";
var_dump($_POST);
die();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="author" content="Rikudo Sennin" />
<title>Untitled</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function() {
$('#poll').submit(function() {
$this = $(this);
$.post(
"form-get-test.php",
$this.serialize(),
function(data) {
alert(data);
}
)
return false;
});
})
</script>
</head>
<body>
<form name='poll' id='poll'>
<INPUT TYPE="radio" name='poll' value='poll3' checked/>Yes
<INPUT TYPE="radio" name='poll' value='poll2'/>Done
<INPUT TYPE="radio" name='poll' value='poll1'/>No
<INPUT TYPE='button' value='submit' onClick="vote();" />
<INPUT TYPE='hidden' name='id' value='<?php echo $id; ?>'/>
<button type="submit" name="poll_submit">Send</button>
</form>
</body>
</html>
精彩评论