wright67uk Posted April 24, 2011 Share Posted April 24, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/234566-stop-if-statement-from-working-onload/ Share on other sites More sharing options...
ShoeLace1291 Posted April 24, 2011 Share Posted April 24, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234566-stop-if-statement-from-working-onload/#findComment-1205490 Share on other sites More sharing options...
wright67uk Posted April 24, 2011 Author Share Posted April 24, 2011 But wouldn't i be calling a function defined in PHP with a Javascript event i.e. click of a mouse... I thought PHP was executed on a server side and not on a client side like JS does? Quote Link to comment https://forums.phpfreaks.com/topic/234566-stop-if-statement-from-working-onload/#findComment-1205662 Share on other sites More sharing options...
wright67uk Posted April 24, 2011 Author Share Posted April 24, 2011 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'); } } Quote Link to comment https://forums.phpfreaks.com/topic/234566-stop-if-statement-from-working-onload/#findComment-1205668 Share on other sites More sharing options...
wright67uk Posted April 24, 2011 Author Share Posted April 24, 2011 OK this code DOES work, my host just likes to take 10 minutes to update my page! Quote Link to comment https://forums.phpfreaks.com/topic/234566-stop-if-statement-from-working-onload/#findComment-1205671 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.