Jump to content

Recommended Posts

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>

 

Link to comment
https://forums.phpfreaks.com/topic/255749-simple-form-stumping-rusty/
Share on other sites

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.

 

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?

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.