Jump to content

stop if statement from working onload


wright67uk

Recommended Posts

The code belows stops sql queries from being executed when there are blank fields on my form.

Aswell as this, a message is displayed '<br> but you did not complete all of the required fields, please try again'

 

How can I tell me page to check the fields upon pressing the click button, opposed to onload?

 

<?php 
include('func.php');
include($_SERVER['DOCUMENT_ROOT'].'/include/db.php');
?>
<!--$INC_DIR = $_SERVER["DOCUMENT_ROOT"]. "/include/";-->
<!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>Chained Select Boxes using PHP, MySQL and jQuery</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
$('#wait_1').hide();
$('#drop_1').change(function(){
  $('#wait_1').show();
  $('#result_1').hide();
      $.get("func.php", {
	func: "drop_1",
	drop_var: $('#drop_1').val()
      }, function(response){
        $('#result_1').fadeOut();
        setTimeout("finishAjax('result_1', '"+escape(response)+"')", 400);
      });
    	return false;
});
});

function finishAjax(id, response) {
  $('#wait_1').hide();
  $('#'+id).html(unescape(response));
  $('#'+id).fadeIn();
}
</script>
</head>
<body>
<p>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Name: 		<input type="text" name="Name" maxlength="50"/><br />
Phone:	    <input type="text" name="Phone" maxlength="50"/><br />
Email: 		<input type="text" name="Email" maxlength="50"/><br />
Postcode: 	<input type="text" name="Postcode" maxlength="50"/><br />
Web Address: <input type="text" name="Website" maxlength="50"/><br /><br />
<select name="drop_1" id="drop_1"> 
<option value="" selected="selected" disabled="disabled">Select a Category</option>  
<?php getTierOne(); ?>
</select> 
<span id="wait_1" style="display: none;">
<img alt="Please Wait" src="ajax-loader.gif"/>
</span>
<span id="result_1" style="display: none;"></span> <br />

</form>
</p>
<p>
<?php if(isset($_POST['submit'])){
$drop = mysql_real_escape_string($_POST['drop_1']);
$tier_two = mysql_real_escape_string($_POST['Subtype']);
echo "You selected ";
echo $drop." & ".$tier_two;
}
$Name = mysql_real_escape_string($_POST["Name"]);
$Phone = mysql_real_escape_string($_POST["Phone"]);
$Email = mysql_real_escape_string($_POST["Email"]);
$Postcode = mysql_real_escape_string($_POST["Postcode"]);
$Website = mysql_real_escape_string($_POST["Website"]);
if($Name == '' || $Phone == '' || $Email == '' || $Postcode == '' || $Website == '')
{
die('<br> but you did not complete all of the required fields, please try again');

}
echo "<br>";
echo $Name;
echo "<br>";
echo $Website; 
$query = ("INSERT INTO business (`id`, `Name`,  `Type`, `Subtype`, `Phone`, `Email`, `Postcode`, `Web Address`)
		  VALUES ('NULL', '$Name', '$drop', '$tier_two' , '$Phone', '$Email', '$Postcode', '$Website')");
mysql_query($query) or die ( "<br>Query: $query<br>Error: " .mysql_error());
?>
</body>
</html>

Link to comment
Share on other sites

If you use onload, it will check the fields even before the user submits the form.  To check the fields when the user clicks submit, just add an onclick function to the submit button tag.

 

<input type='submit' value='Submit' onclick='myFunction();">

 

If your function returns false, the form will not submit.  If it returns true, it will.

Link to comment
Share on other sites

Ive been playing around with this code,

Im fairly new to php so please correct me if im wrong but i think i telling my script to...

 

If i press submit, post the values and if $name, $phone, $email or $website are empty then display;

<br> but you did not complete all of the required fields, please try again.

 

I thought that placing my die comment within the first curly brackets would mean that this text would only

display when submit has been posted.

 

 

 

<?php if(isset($_POST['submit']))
{
$drop = mysql_real_escape_string($_POST['drop_1']);
$tier_two = mysql_real_escape_string($_POST['Subtype']);
echo "You selected ";
echo $drop." & ".$tier_two;
$Name = mysql_real_escape_string($_POST["Name"]);
$Phone = mysql_real_escape_string($_POST["Phone"]);
$Email = mysql_real_escape_string($_POST["Email"]);
$Postcode = mysql_real_escape_string($_POST["Postcode"]);
$Website = mysql_real_escape_string($_POST["Website"]);
if($Name == '' || $Phone == '' || $Email == '' || $Postcode == '' || $Website == '')
{
die('<br> but you did not complete all of the required fields, please try again');
}
}

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.