Forum Moderators: coopster

Message Too Old, No Replies

Total rookie needs help with required fields

Required form fields

         

bigriver

4:27 am on May 2, 2011 (gmt 0)

10+ Year Member



I've tried to figure this out on my own as I assume it's pretty simple to do, but I know next to nothing about coding. I simply want to make two text fields required and a third field required as a numeric value.

The html:
<form name="bidform" action="insert.php" method="post">
<p>First Name: <input type="text" name="First" /></p>
<p>Last Name: <input type="text" name="Last" /></p>
<p>Bid: $<input type="decimal" name="Bid" /> Do not enter the dollar sign. Whole dollars only.</p>
<input type="submit" value="Submit Bid"/>
</form>


And the php:
<?php
$con = mysql_connect('#*$!x.#*$!x.com', '#*$!x', '#*$!x'); ;
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db(#*$!x);

$sql="INSERT INTO Bids(First, Last, Bid)
VALUES
('$_POST[First]','$_POST[Last]','$_POST[Bid]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}

//take user to thank you page after submission//
header("Location: position_auction_ty.shtml");

mysql_close($con)
?>

What do I need to do? Thanks for the help.

Matthew1980

8:24 am on May 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there bigriver,

Welcome to the forums :)

Your HTML:-


<form name="bidform" action="insert.php" method="post">
<p>First Name: <input type="text" name="First" /></p>
<p>Last Name: <input type="text" name="Last" /></p>
<p>Bid: $<input type="text" name="Bid" /> Do not enter the dollar sign. Whole dollars only.</p>
<input type="submit" name="submit" value="Submit Bid"/>
</form>


Just change those bits highlighted, form data is string by default, manipulate it after form submission.

Your php :-


<?php
//turn on error reporting and make sure this is removed when putting into the public domain
error_reporting(E_ALL);

//check for form submission
if (isset($_POST['submit']) && !empty($_POST['submit'])){

//set up connection
$con = mysql_connect('#*$!x.#*$!x.com', '#*$!x', '#*$!x') or die("Could not connect: ".mysql_error())
mysql_select_db("#*$!x", $con);


//clean your data as your using it directly into DB
$_POST = array_map('trim', $_POST);
$_POST = array_map('mysql_real_escape_string', $_POST);

//convert the bid into integer (if you wish) ensure that the col in the DB is set up to handle this...
$_POST['Bid'] = (int)$_POST['Bid'];

$sql="INSERT INTO `Bids` (`First`, `Last`, `Bid`) VALUES ('".$_POST['First']."','".$_POST['Last']."','".$_POST['Bid']."')";

//issue the query
if (mysql_query($sql,$con)){
//take user to thank you page after submission//
header("Location: position_auction_ty.shtml");
exit;
}
else{
//the way your doing this, you need to have the else here so that you can handle any failure.
die('Error: ' . mysql_error());
exit;
}

//no need to use mysql_close as this is the default action when the script has been excecuted

}
else{
//failed, so redirect user back to form
header("location: YOUR_FORM_FILENAME_HERE.php");
exit;
}

?>



Hopefully this makes sense, though typed on the fly, main contain errors.

Haope that helps anyway.

Happy coding.

Cheers,
MRb

bigriver

9:16 pm on May 2, 2011 (gmt 0)

10+ Year Member



Thanks...I gave it a try and broke it pretty good.

Can you tell me what you mean by:
//turn on error reporting and make sure this is removed when putting into the public domain
error_reporting(E_ALL);

It seemed it wasn't adding records to the database. Just gave me a blank screen when I hit submit. I messed with a bunch of different things and couldn't seem to make it work.

Matthew1980

8:09 am on May 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there bigriver.

>>it broke pretty good

Ah, the joys of programming!

error_reporting() [uk2.php.net] is the function you use when you want to see all the error's that are stopping your script from executing as expected.

Error messages will be a great help debugging.

Cheers,
MRb