turbocueca Posted April 3, 2006 Share Posted April 3, 2006 When submiting information through a PHP page to a mysql database, how can I make a field required? It must have any information or the user won't be able to submit any info. Quote Link to comment Share on other sites More sharing options...
shocker-z Posted April 3, 2006 Share Posted April 3, 2006 Your looking at javascript not PHP for this mate try posting in the javascript forum :)Only thing with PHP you can do is when the page is submitted check for blank fields.. Quote Link to comment Share on other sites More sharing options...
turbocueca Posted April 3, 2006 Author Share Posted April 3, 2006 I try with an if statement, $var="", but it gave an error. How can I check for blank fields? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted April 3, 2006 Share Posted April 3, 2006 Post your error messages here along with your code then we'll be able to help you a lot more. Basically fi you want to see if certain field is emtpy the use this:[code]if(!isset($_POST['FieldName']) || empty($_POST['fileName'])){ echo "PLease fill in all fields thank you!;}[/code] Quote Link to comment Share on other sites More sharing options...
shocker-z Posted April 3, 2006 Share Posted April 3, 2006 a quick way i found to check in general isforeach($_POST[] as $post) { if ($post == '') { $error("Blank field detected"); }}Then you just do if (!$error) { all code to insert/upload the data} else {normal form to display and echo all the variables abck into the boxes to save the user som hastle}As far as i've tested on my own pages that works :) Quote Link to comment Share on other sites More sharing options...
turbocueca Posted April 3, 2006 Author Share Posted April 3, 2006 the var $post is the one that has the value of the field right?And what does the ! behind $error or isset as I sometimes see?it gives this error:Warning: Invalid argument supplied for foreach() in /home/www/infocenter.awardspace.com/inter/ps2ub/insert.php on line 21 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 3, 2006 Share Posted April 3, 2006 Please post the code that you're using.The superglobal array that contains the values from a submitted form is $_POST, not $post.Ken Quote Link to comment Share on other sites More sharing options...
turbocueca Posted April 3, 2006 Author Share Posted April 3, 2006 [code] <?php if (isset($_POST['Submit'])) { include 'db.php'; $n=$_POST['textfield3']; $s=$_POST['radiobutton1']; $w=$_POST['radiobutton']; $c=$_POST['textfield']; $p=$_POST['textfield2']; $e=$_POST['textfield22']; foreach($_POST[] as $post) { if ($post == '') { $error("Blank field detected"); } } if (!$error) { $query="INSERT INTO usbextreme (name,system,work_status,comments,postedby,email) VALUES ('$n','$s','$w','$c','$p','$e')"; $exec=mysql_query($query,$connect); } else { echo "try again"; } echo '</p><p align="center"><strong>Success</strong></p><p align="center"><a href="submit.php">Add another game</a> | <a href="index.php">Check game list '; } else { echo "<center><b>This page should only be accessed when submitting a game<b></center>"; }?>[/code] Quote Link to comment Share on other sites More sharing options...
shocker-z Posted April 3, 2006 Share Posted April 3, 2006 if ($error) {}means if there is a value in $error then do what ever is between the brakcets! means NOT.. thereforeif (!$error) {}means if there is NO value to $error then do somthingi think i did that wrong and should have used $_POST instead of $_POST[] in the foreach loop.. Quote Link to comment Share on other sites More sharing options...
turbocueca Posted April 3, 2006 Author Share Posted April 3, 2006 What does that $post and isset does?I can't test the code right now, the server is having some probs.. Quote Link to comment Share on other sites More sharing options...
shocker-z Posted April 3, 2006 Share Posted April 3, 2006 if (isset($_POST['Submit'])) {that is saying if the variable $_POST['Submit'] is set then do the following.. which $_POST stores everything from the boxes in your form$post is just a temporary value to see if a value inside the form is blank Quote Link to comment Share on other sites More sharing options...
turbocueca Posted April 3, 2006 Author Share Posted April 3, 2006 Fatal error: Call to undefined function: () in /home/www/infocenter.awardspace.com/inter/ps2ub/insert.php on line 23i removed those []anyway, what's foreach? Quote Link to comment Share on other sites More sharing options...
turbocueca Posted April 3, 2006 Author Share Posted April 3, 2006 [a href=\"http://infocenter.awardspace.com/inter/ps2ub/submit2.php\" target=\"_blank\"]http://infocenter.awardspace.com/inter/ps2ub/submit2.php[/a][a href=\"http://infocenter.awardspace.com/inter/ps2ub/insert.php\" target=\"_blank\"]http://infocenter.awardspace.com/inter/ps2ub/insert.php[/a]insert.php:[code]<?php if (isset($_POST['Submit'])) { include 'db.php'; $n=$_POST['textfield3']; $s=$_POST['radiobutton1']; $w=$_POST['radiobutton']; $c=$_POST['textfield']; $p=$_POST['textfield2']; $e=$_POST['textfield22']; foreach($_POST as $post) { if ($post == '') { $error("Blank field detected"); //line 23 } } if (!$error) { $query="INSERT INTO usbextreme (name,system,work_status,comments,postedby,email) VALUES ('$n','$s','$w','$c','$p','$e')"; $exec=mysql_query($query,$connect); } else { echo "try again"; } echo '</p><p align="center"><strong>Success</strong></p><p align="center"><a href="submit.php">Add another game</a> | <a href="index.php">Check game list '; } else { echo "<center><b>This page should only be accessed when submitting a game<b></center>"; }?>[/code]Whats the problem? Quote Link to comment Share on other sites More sharing options...
shocker-z Posted April 3, 2006 Share Posted April 3, 2006 when submitting NO data i get error:Fatal error: Call to undefined function: () in /home/www/infocenter.awardspace.com/inter/ps2ub/insert.php on line 23well im confused on this one because error line is in red below yet nothing at all wrong with it! } else {[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--] echo "try again";[!--colorc--][/span][!--/colorc--] }But if i submit data i get success.. Quote Link to comment Share on other sites More sharing options...
turbocueca Posted April 3, 2006 Author Share Posted April 3, 2006 wow, i fixed it. i changed from $error("...") to $error="..." and then tested with different data, on different boxes and works fine, now the problem is that if there is one form to fill up it says try again, but I only want it to try again if one of the required fields arent filled up, they are the ones with stars *. How can i do that?Ignore the success message as it always appear if the submit button is clicked.One more thing, is there any code to make the browser return to a previous page without deleting the values written on the fields? Quote Link to comment Share on other sites More sharing options...
turbocueca Posted April 3, 2006 Author Share Posted April 3, 2006 C'mon, how can I make a field NOT required? Quote Link to comment 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.