brassfid Posted January 25, 2012 Share Posted January 25, 2012 Been about a year since i played around with websites. My database is not being updated, and after submitting form it goes to the location of the action rather than running the action. Could someone please help! I am working in wordpress. Thanks the php form is <?php $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("", $con); $username = mysql_real_escape_string($_POST['username']); $betText = mysql_real_escape_string($_POST['betText']); $betMember1 = mysql_real_escape_string($_POST['betMember1']); //$sql adds bet to bet table $sql = "INSERT INTO betText (bettor, betText) VALUES ('$username', '$betText')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } $newBetID = mysql_insert_id(); $updatebetMember = "INSERT INTO betMember (betID, betMember) VALUES ('$newBetID','$betMember1')"; if (!mysql_query($updatebetMember,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); header("Location:") ?> the form is <head> <script type="text/javascript"> function validateForm() { var x=document.forms["newbet"]["betText"].value if (x==null || x=="") { alert("You gotta have bet!"); return false; } var x=document.forms["newbet"]["betMember1"].value if (x==null || x=="") { alert("Who you betting against?"); return false; } } </script> </head> <form name="newbet" action="/wp-content/pbbbet.php" onsubmit="return validateForm()" method="post"> <label for="username"><strong>Username: </strong></label> <input id="username" name="username" type="text" readonly="readonly" value="<?php global $current_user; get_currentuserinfo(); echo $current_user->user_login; ?>"/> <strong>Peanut Butter Bet: <textarea cols="50" rows="4" name="betText"></textarea> <table width="75%" border="4"> <tr> <strong>Who is your bet against: </strong> </tr> <tr> <th> 1. </th> <th> <input name="betMember1" type="text" /> </th> </tr> </table> <script type="text/javascript"> function show_alert() { alert("Thank you!"); } </script> <input name="submitbet" input type="SUBMIT" onclick="show_alert()" value="SUBMIT" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/255749-simple-form-stumping-rusty/ Share on other sites More sharing options...
Drongo_III Posted January 25, 2012 Share Posted January 25, 2012 Don't forms always redirect to the location of the action? If you don't want it to redirect you could simply run it from that page with a simple if statement to check if the form was submitted. i.e. <?php if(issset($_POST['submit'])){ Run code to process the form - call functions required etc. } else { include('YourForm.php') } Or You could set your head location to return the use to a thank you page. Quote Link to comment https://forums.phpfreaks.com/topic/255749-simple-form-stumping-rusty/#findComment-1311049 Share on other sites More sharing options...
brassfid Posted January 25, 2012 Author Share Posted January 25, 2012 I did not know that about the forms. so that means that my error is in the .php not the .html. If I want it to return to the same page that it is submitted from (basically refresh the page) wouldn't I just put in the same url or would I want to actually make the button refresh? Quote Link to comment https://forums.phpfreaks.com/topic/255749-simple-form-stumping-rusty/#findComment-1311052 Share on other sites More sharing options...
Drongo_III Posted January 25, 2012 Share Posted January 25, 2012 You could either do action="#" or action="http://www.YourContactPage.com" So the action just targets the current page and the php would then check to see if POST submt is set (you need to give submit a name btw), if it is then it'll process and you can display a thank you message. Your php code isn't necessarily incorrect because you could just set the header('Location:') to direct to a thank you page. But the issue I can foresee (at a glance) would be if you throw an exception because once that outputs the head location won't be able to redirect and your user would end up on a blank page with an error message. I did not know that about the forms. so that means that my error is in the .php not the .html. If I want it to return to the same page that it is submitted from (basically refresh the page) wouldn't I just put in the same url or would I want to actually make the button refresh? Quote Link to comment https://forums.phpfreaks.com/topic/255749-simple-form-stumping-rusty/#findComment-1311056 Share on other sites More sharing options...
brassfid Posted January 25, 2012 Author Share Posted January 25, 2012 Well I feel like an idiot, I wasn't even running the .php, was mapped wrong in the action field in my .html form. Whoops. Thanks for the help though, it gave me a lot to think about! Quote Link to comment https://forums.phpfreaks.com/topic/255749-simple-form-stumping-rusty/#findComment-1311101 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.