eyegraphix Posted April 1, 2007 Share Posted April 1, 2007 I have problem that I'm sure is easy to solve. The problem is getting a certain post variable from a form into a database. I'm submitting all the variables into a mysql database, but one of the variables does not show up in the database ($vote). Why oh why do the contents of the $vote variable not get into the database? <? session_start(); if (isset($_SESSION['valid_user'])) { include ("dbconnect.php"); $vote=$_POST['radiobutton']; $netid=$_SERVER['REMOTE_USER']; echo "You have selected to vote for $vote. If this is incorrect,<a href=\"index.php\"> click here </a> to go back else click <a href=\"70scluster.php?action=submitvote\"> here </a><br>"; $action=$_GET['action']; if ($action =="submitvote"){ mysql_query("INSERT INTO voting (username, cluster,vote) VALUES('$netid', '70s','$vote') ") or die(mysql_error()); echo "<input type='button' value='Click here to close this window and logout' onclick='self.close()'>"; } } ?> Quote Link to comment Share on other sites More sharing options...
interpim Posted April 1, 2007 Share Posted April 1, 2007 echo the value of the radiobutton post... if i remember correctly radiobuttons are written to an array. And you would have to pull that value out of that array. Quote Link to comment Share on other sites More sharing options...
eyegraphix Posted April 1, 2007 Author Share Posted April 1, 2007 Hmm, that still doesn't work, plus I really don't want to echo the contents of that variable onto the page. I know there's got to be a simple way around this. Quote Link to comment Share on other sites More sharing options...
tippy_102 Posted April 1, 2007 Share Posted April 1, 2007 $selected_radio = $_POST[vote]; Quote Link to comment Share on other sites More sharing options...
eyegraphix Posted April 1, 2007 Author Share Posted April 1, 2007 Um, that didn't work either. Here's the updated code. <? session_start(); if (isset($_SESSION['valid_user'])) { include ("dbconnect.php"); $vote=$_POST['radiobutton']; $selected_radio = $_POST[$vote]; $netid=$_SERVER['REMOTE_USER']; echo "You have selected to vote for $vote. If this is incorrect,<a href=\"index.php\"> click here </a> to go back else click <a href=\"70scluster.php?action=submitvote\"> here </a><br>"; $action=$_GET['action']; if ($action =="submitvote"){ mysql_query("INSERT INTO voting (username, cluster,vote) VALUES('$netid', '70s','$selected_radio') ") or die(mysql_error()); echo "<input type='button' value='Click here to close this window and logout' onclick='self.close()'>"; } } ?> Quote Link to comment Share on other sites More sharing options...
interpim Posted April 1, 2007 Share Posted April 1, 2007 is radiobutton the name of the radio field? for instance in the form... <input type='radio' name='radiobutton' value='value_1'> <input type='radio' name='radiobutton' value='value_2'> <input type='radio' name='radiobutton' value='value_3'> if the user selects say, value_2... the $_POST['radiobutton'] variable will be value_2. You should be able to echo that value from that variable. Quote Link to comment Share on other sites More sharing options...
eyegraphix Posted April 1, 2007 Author Share Posted April 1, 2007 Yes you are correct, that's what my form looks like. And yes, I know perfectly well I can echo it's contents out. When I put something like echo $vote I get what's in it, but when I try to insert it into the database it's not there. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 1, 2007 Share Posted April 1, 2007 Why are you doing this? <?php $vote=$_POST['radiobutton']; $selected_radio = $_POST[$vote]; ?> If you do an <?php echo $selected_radio; ?> What do you get? You probably want to change your MySQL query to: <?php $query = "INSERT INTO voting (username, cluster,vote) VALUES('$netid', '70s','$vote') "; $rs = mysql($query) or die("Problem with the query <pre>$query</pre><br>" . mysql_error()); ?> Ken Quote Link to comment Share on other sites More sharing options...
pentan Posted April 1, 2007 Share Posted April 1, 2007 At a quick glance this is what it looks like you have happening: Person clicks a radio button then submits it to the page you displayed here. The page you displayed here lets the person verify that was the one they wanted to vote for and re-submits it to the same page. The page then puts it in the database. If this is what happens, then the problem is you aren't re-passing the variable to the next instance of the page after they verify they picked the right choice. You can either do that with a hidden submit or a session variable: $_SESSION[selected_radio] = $_POST[$vote]; mysql_query("INSERT INTO voting (username, cluster,vote) VALUES('$netid', '70s','$_SESSION[selected_radio]') ") or die(mysql_error()); Michael Quote Link to comment Share on other sites More sharing options...
eyegraphix Posted April 1, 2007 Author Share Posted April 1, 2007 Yes pentan. That's exactly what I'm trying to do. Your suggestion makes sense, but for some reason I'm getting the same result and the vote doesn't get into the database. I can echo'd the contents of $_SESSION[selected_radio] out at various points of the script, but once I try to echo it out past the line if ($action =="submitvote"){ , it doesn't work. Here's my updated code. <? session_start(); if (isset($_SESSION['valid_user'])) { include ("dbconnect.php"); $vote=$_POST['radiobutton']; $_SESSION[selected_radio] = $_POST['radiobutton']; $netid=$_SERVER['REMOTE_USER']; echo "You have selected to vote for $vote. If this is incorrect,<a href=\"index.php\"> click here </a> to go back else click <a href=\"70scluster.php?action=submitvote\"> here </a><br>"; $action=$_GET['action']; if ($action =="submitvote"){ mysql_query("INSERT INTO voting (username, cluster,vote) VALUES('$netid', '70s','$_SESSION[selected_radio]') ") or die(mysql_error($ echo "<input type='button' value='Click here to close this window and logout' onclick='self.close()'>"; } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.