Mattio2187 Posted June 21, 2009 Share Posted June 21, 2009 Bare with me, this is a bit tricky So basically I already have a form with 3 radio buttons that checks for a cookie with assigned IP. This is to prevent the chances that someone has deleted their cookies, or changed their ip, or neither can be found. There are multiple result/returns based upon what the situation happens to be, but i'm really only worried about two: The form has been submitted, or You have already voted. I would like to have the form submit, values placed in the database, but without the page reloading. Instead i would like to return a message to the user, whether in an alert or revealed on the page, stating that the form was submitted or one of the other messages included as a part of the ip/cookie check. I've tried incorporating a few other methods from some other tutorials involving jquery/ajax but can't seem to figure this one out. Any feedback would be greatly appreciated. HTML code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <style type="text/css"> .selectbox { width: 100px; height: 100px; border: 1px solid red; float: left; margin: 20px; } </style> <body> <form action="vote.php" method="post"> <div class="selectbox">Choice 1: <input type="radio" name="picked" value="one"/></div> <div class="selectbox">Choice 2: <input type="radio" name="picked" value="two"/></div> <div class="selectbox">Choice 3: <input type="radio" name="picked" value="three"/></div> <input type="submit" /> </form> </body> </html> PHP Code: <?php $con = mysql_connect("localhost","mattsol1_user","user"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mattsol1_pollthree", $con); $theVote = $_POST[picked]; if(isset($_COOKIE['ip']) && !empty($_COOKIE['ip'])) { if(isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR'])) { if($_COOKIE['ip'] == $_SERVER['REMOTE_ADDR']) { $ipAddr = $_COOKIE['ip']; } else { setcookie("ip", FALSE); setcookie("ip", $_SERVER['REMOTE_ADDR']); $ipAddr = $_SERVER['REMOTE_ADDR']; die("It appears your IP has changed since you last voted or you played around with your cookies. Either way, you're getting a new cookie. If you don't know why this is happening, do not worry - you did nothing wrong. <a href='path/to/your/poll/mainpage/'>Return home</a>"); } } else { die("It appears I cannot grab your IP address and thus cannot tell if you voted already."); } } else { $ipAddr = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']); setcookie("ip", $ipAddr); } $ipCheck = mysql_query("SELECT * FROM `vote` WHERE `ip` = '" . mysql_real_escape_string($ipAddr) . "'"); if(mysql_num_rows($ipCheck) > 0) { die("You have already voted in this poll. Good day!"); } else { $sql= "INSERT INTO `vote` (`ip`, `picked`) VALUES ('$ipAddr', '$theVote')"; } if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "Thank You"; mysql_close($con); ?> 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.