javascript - How do I submit this PHP poll form on click of a radio button? -
i'm using css tricks' how design , create php powered poll tutorial create own poll.
i'm trying poll submit when user clicks 1 of radio button options, instead of submitting when click "vote" button.
poll.php:
<?php require_once('connections/conn_vote.php'); ?> <?php if (!function_exists("getsqlvaluestring")) { function getsqlvaluestring($thevalue, $thetype, $thedefinedvalue = "", $thenotdefinedvalue = "") { $thevalue = get_magic_quotes_gpc() ? stripslashes($thevalue) : $thevalue; $thevalue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($thevalue) : mysql_escape_string($thevalue); switch ($thetype) { case "text": $thevalue = ($thevalue != "") ? "'" . $thevalue . "'" : "null"; break; case "long": case "int": $thevalue = ($thevalue != "") ? intval($thevalue) : "null"; break; case "double": $thevalue = ($thevalue != "") ? "'" . doubleval($thevalue) . "'" : "null"; break; case "date": $thevalue = ($thevalue != "") ? "'" . $thevalue . "'" : "null"; break; case "defined": $thevalue = ($thevalue != "") ? $thedefinedvalue : $thenotdefinedvalue; break; } return $thevalue; } } $editformaction = $_server['php_self']; if (isset($_server['query_string'])) { $editformaction .= "?" . htmlentities($_server['query_string']); } if ((isset($_post["mm_insert"])) && ($_post["mm_insert"] == "form1")) { $insertsql = sprintf("insert poll (id, question) values (%s, %s)", getsqlvaluestring($_post['id'], "int"), getsqlvaluestring($_post['poll'], "text")); mysql_select_db($database_conn_vote, $conn_vote); $result1 = mysql_query($insertsql, $conn_vote) or die(mysql_error()); $insertgoto = "results.php"; if (isset($_server['query_string'])) { $insertgoto .= (strpos($insertgoto, '?')) ? "&" : "?"; $insertgoto .= $_server['query_string']; } header(sprintf("location: %s", $insertgoto)); } $colname_rs_vote = "-1"; if (isset($_get['recordid'])) { $colname_rs_vote = $_get['recordid']; } mysql_select_db($database_conn_vote, $conn_vote); $query_rs_vote = sprintf("select * poll id = %s", getsqlvaluestring($colname_rs_vote, "int")); $rs_vote = mysql_query($query_rs_vote, $conn_vote) or die(mysql_error()); $row_rs_vote = mysql_fetch_assoc($rs_vote); $totalrows_rs_vote = mysql_num_rows($rs_vote); ?> <!doctype html> <head> <title>poll</title> <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> </head> <body> <form action="<?php echo $editformaction; ?>" id="form1" name="form1" method="post"> <label> <input type="radio" name="poll" value="snoppdogg" id="poll_0" /> snoop dogg </label> <label> <input type="radio" name="poll" value="biggie" id="poll_1" /> biggie </label> <label> <input type="radio" name="poll" value="tupac" id="poll_2" /> tupac </label> <input type="submit" name="submit" id="submit" value="vote" /> <input type="hidden" name="id" value="form1" /> <input type="hidden" name="mm_insert" value="form1"> </form> <script type="text/javascript"> $('input[type=radio]').click(function() { $(this).closest("form").submit(); }); </script> </body> </html> <?php mysql_free_result($rs_vote); ?>
conn_vote.php:
<?php # filename="connection_php_mysql.htm" # type="mysql" # http="true" $hostname_conn_vote = "localhost"; $database_conn_vote = "poll"; $username_conn_vote = "root"; $password_conn_vote = "root"; //$conn_vote = mysql_pconnect($hostname_conn_vote, $username_conn_vote, $password_conn_vote) or trigger_error(mysql_error(),e_user_error); $conn_vote = mysql_connect($hostname_conn_vote, $username_conn_vote, $password_conn_vote) or die('can\'t create connection: '.mysql_error()); mysql_select_db($database_conn_vote, $conn_vote) or die('can\'t access specified db: '.mysql_error()); ?>
here's poll.php looks like:
this works great if click "vote", not if click radio button.
i tried adding onchange event each radio input, doesn't submit form either.
<input onchange="this.form.submit();" type="radio" name="poll" value="snoopdogg" id="poll_0" />
i have feeling has hidden inputs, can't figure out needs change.
any ideas on how submit form when user clicks radio button?
thanks in advance :)
edit:
i think it's php issue, not javascript issue. i've tried of these javascript solutions , none of them have worked:
<input onchange="this.form.submit();" type="radio" name="poll" value="snoopdogg" id="poll_0" />
and
<input onclick="this.form.submit();" type="radio" name="poll" value="snoopdogg" id="poll_0" />
and
$('input').click(function(){ $('form').submit(); });
and
$('input[type="radio"]').click(function() { $("form").submit(); });
and
$('input[type=radio]').click(function() { $(this).closest("form").submit(); });
and
$('input').click(function(){ $.ajax({ type:'post', url:'results.php', data:{radio:info} }).done(function(data){ alert("show ajax call successful!"); }); });
by "submit", mean want submit form, enter values database, , go results.php page, functionality of "vote" button.
use onclick
instead of onchange
.
<input onchange="this.form.submit();" />
you can use jquery submit form onclick:
$('input').click(function(){ $('form').submit(); });
and more slick, use ajax ;)
$('input').click(function(){ $.ajax({ type:'post', url:'somepage.php', data:{radio:info} }).done(function(data){ //show ajax call successful! }); });
edit: guess problem php code, since wasn't inserting values database. reason why form wasn't submitting though, because used onchange
instead of onclick
.
if want use ajax, need values input elements, in case radio inputs. not have submit form when use ajax.
for instance:
<input type="radio" id="#tupacradio" /> //get value of radio input var tupacradio = $('#tupacradio').val();
then once call ajax:
$.ajax({ data:{value:tupacradio} })
Comments
Post a Comment