marcus Posted September 23, 2007 Share Posted September 23, 2007 $var = $_POST['variable']; $var2 = $_POST['variable2']; $errors = array(); if(!$var){ $errors[] = "Var one is missing!"; } if(!$var2){ $errors[] = "Var two is missing!"; } if(count($errors) > 0){ foreach($errors AS $error){ echo "<font color=\"red\">$error</font><br>\n"; } }else { echo "both variables are set, you're ready for take off!\n"; } Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 23, 2007 Author Share Posted September 23, 2007 please specify the line of the error and paste that line here if empty($_POST[variable]||$_POST[variable2]) { echo "sorry you must fill in the form";} Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 23, 2007 Author Share Posted September 23, 2007 $var = $_POST['variable']; $var2 = $_POST['variable2']; $errors = array(); if(!$var){ $errors[] = "Var one is missing!"; } if(!$var2){ $errors[] = "Var two is missing!"; } if(count($errors) > 0){ foreach($errors AS $error){ echo "<font color=\"red\">$error</font><br>\n"; } }else { echo "both variables are set, you're ready for take off!\n"; } Can you explain how to use this snippet? do I have to declare the variables? if so how? Quote Link to comment Share on other sites More sharing options...
marcus Posted September 23, 2007 Share Posted September 23, 2007 Just define the variables that correspond with your form field names. $name = $_POST['form_field_name_one']; //etc.... To see if a value exists just implement the 5th and 6th of my example to fit your own. If you have more than two just continue adding sets of line five and six to meet your requirements Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 23, 2007 Author Share Posted September 23, 2007 there is only one variable "$query" Quote Link to comment Share on other sites More sharing options...
gromer Posted September 23, 2007 Share Posted September 23, 2007 <?php $self = $_SERVER['PHP_SELF']; $query = $_POST['query']; if (isset($query)) { // Do stuff with $query // Redirect somewhere when done. } else { echo "You must fill in the form."; } ?> <html> <head> <title>A form</title> </head> <body> <form action="<?php echo $self; ?>" method="post"> <input type="text" name="query" /> <input type="submit"> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
marcus Posted September 23, 2007 Share Posted September 23, 2007 Then just remove line five or six and replace it with your own variable/error. Using PHP_SELF can be a bad move since the user could implement his or her own code into the URL. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 23, 2007 Share Posted September 23, 2007 i agree stick with post <?php $query=$_POST['query']; if empty($query) { echo "error message here";} ?> Quote Link to comment Share on other sites More sharing options...
gromer Posted September 23, 2007 Share Posted September 23, 2007 Then just remove line five or six and replace it with your own variable/error. Using PHP_SELF can be a bad move since the user could implement his or her own code into the URL. Can y'all explain this more? How could a user put more code in the URL? What's the best alternative for PHP_SELF? Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 23, 2007 Author Share Posted September 23, 2007 i agree stick with post <?php $query=$_POST['query']; if empty($query) { echo "error message here";} ?> throws a line error Parse error: parse error, expecting `'('' if empty($query) { echo "error message here";} Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 23, 2007 Author Share Posted September 23, 2007 <?php $self = $_SERVER['PHP_SELF']; $query = $_POST['query']; if (isset($query)) { // Do stuff with $query // Redirect somewhere when done. } else { echo "You must fill in the form."; } ?> <html> <head> <title>A form</title> </head> <body> <form action="<?php echo $self; ?>" method="post"> <input type="text" name="query" /> <input type="submit"> </form> </body> </html> the form is just an html doc it calls the script from the action parameter Quote Link to comment Share on other sites More sharing options...
gromer Posted September 23, 2007 Share Posted September 23, 2007 <?php $self = $_SERVER['PHP_SELF']; $query = $_POST['query']; if (isset($query)) { // Do stuff with $query // Redirect somewhere when done. } else { echo "You must fill in the form."; } ?> <html> <head> <title>A form</title> </head> <body> <form action="<?php echo $self; ?>" method="post"> <input type="text" name="query" /> <input type="submit"> </form> </body> </html> the form is just an html doc it calls the script from the action parameter Exactly. You said you wanted it to show the form again if nothing was submitted. This checks that and then redirects to wherever you want if the data is correct. You should consider doing js data validation too... Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 23, 2007 Author Share Posted September 23, 2007 <?php $self = $_SERVER['PHP_SELF']; $query = $_POST['query']; if (isset($query)) { // Do stuff with $query // Redirect somewhere when done. } else { echo "You must fill in the form."; } ?> <html> <head> <title>A form</title> </head> <body> <form action="<?php echo $self; ?>" method="post"> <input type="text" name="query" /> <input type="submit"> </form> </body> </html> the form is just an html doc it calls the script from the action parameter Exactly. You said you wanted it to show the form again if nothing was submitted. This checks that and then redirects to wherever you want if the data is correct. You should consider doing js data validation too... so where does this code call the script? Quote Link to comment Share on other sites More sharing options...
gromer Posted September 23, 2007 Share Posted September 23, 2007 so where does this code call the script? Eh, can you tell me exactly what you need? I reread your original post and you say that you want it to display an error if nothing is entered but also append that error to the top of the form? Not really sure what you need. Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 23, 2007 Author Share Posted September 23, 2007 so where does this code call the script? Eh, can you tell me exactly what you need? I reread your original post and you say that you want it to display an error if nothing is entered but also append that error to the top of the form? Not really sure what you need. I just want the script to return a message stating the form was submitted without any data...please click the back button and try again...etc right now everything I have tried still pulls down all the records...some of the snippets I've tried print the error message but the script still pulls down all the records and some even when a valid query is entered it still prints the error message Quote Link to comment Share on other sites More sharing options...
marcus Posted September 23, 2007 Share Posted September 23, 2007 They could end the script. URL could be: file.php?inj="><h1>WTH</h1><br>DONTFAIL Making the script read: <form action="file.php"><h1>WTH</h1><br>DONTFAIL" method="post"> Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 23, 2007 Author Share Posted September 23, 2007 They could end the script. URL could be: file.php?inj="><h1>WTH</h1><br>DONTFAIL Making the script read: <form action="file.php"><h1>WTH</h1><br>DONTFAIL" method="post"> I have no idea what this is, sorry Quote Link to comment Share on other sites More sharing options...
gromer Posted September 23, 2007 Share Posted September 23, 2007 So all you would need to do to my code above is change the action to some other php file and put my php code in it, sans the $self line. Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 23, 2007 Author Share Posted September 23, 2007 So all you would need to do to my code above is change the action to some other php file and put my php code in it, sans the $self line. the (HTML) needs to be a different doc...otherwise the pagination menu/display is printed Quote Link to comment Share on other sites More sharing options...
gromer Posted September 23, 2007 Share Posted September 23, 2007 So all you would need to do to my code above is change the action to some other php file and put my php code in it, sans the $self line. the (HTML) needs to be a different doc...otherwise the pagination menu/display is printed That's what I said to do. Put the php code in a separate file where the form points to in the action field. Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 23, 2007 Author Share Posted September 23, 2007 So all you would need to do to my code above is change the action to some other php file and put my php code in it, sans the $self line. the (HTML) needs to be a different doc...otherwise the pagination menu/display is printed That's what I said to do. Put the php code in a separate file where the form points to in the action field. It does not work... it still prints all the records Quote Link to comment Share on other sites More sharing options...
gromer Posted September 23, 2007 Share Posted September 23, 2007 Then try: if ($query == "") or if ($query == null) in place of if (isset($query)) 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.