rhock_95 Posted September 22, 2007 Share Posted September 22, 2007 Is there a simple way to prevent an html form from being submitted (without javascript) if no text is entered? <form action="peop_search.php" method="post"> <input type="text" name="query" /> <input type="submit"> </form> If an invalid query is entered it prints the message below but if nothing is enterd it prints all the records if ($numrows == 0){ echo("No results found matching your query - $query"); Is there any way to amend this to echo the above if nothing is entered? Quote Link to comment Share on other sites More sharing options...
dsaba Posted September 22, 2007 Share Posted September 22, 2007 if (isset($_POST)) { //do any kind of manipulation and output with the posted form data //like checking for correct/empty form fields } else { //if nothing is being posted, the form is then displayed } PHP is server side, you can only "change" things once a page has been loaded/reloaded, so once the form has been posted you can then decide what to do with the data, Javascript on the other hand can stop the form from being submitted in real time, but i assum ethat is really now what u want, u can still have the form submitted, but just give the impression of it not being submitted if incorrect data has been submitted and u can just simply redisplay the form like it was never submitted perception is the keyword Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 22, 2007 Author Share Posted September 22, 2007 Thanks for the reply... //like checking for correct/empty form fields Ho do I do this? this is actually the crux of what I am seeking Quote Link to comment Share on other sites More sharing options...
dsaba Posted September 22, 2007 Share Posted September 22, 2007 $var = ""; if (empty($var)) { echo 'its empty'; } if ($var == "") { echo 'its empty'; } if (strlen($var) == 0) { echo 'its empty'; } if ($var != "correct data") { echo 'u get it'; } Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 22, 2007 Author Share Posted September 22, 2007 Thanks again for the efforts... but the script still retrieves all the records...it also prints out ALL the messages... any ideas? Quote Link to comment Share on other sites More sharing options...
rarebit Posted September 22, 2007 Share Posted September 22, 2007 can u show code? Quote Link to comment Share on other sites More sharing options...
MmmVomit Posted September 22, 2007 Share Posted September 22, 2007 It would be helpful to see more code, especially the query you're using. You might try using something like this. if(!isset($_POST['query']) || $_POST['query'] == "") { echo "No results found matching your query - $query"; } else { // process query using $_POST['query'] } Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 22, 2007 Share Posted September 22, 2007 <?php if ($_POST[variable]=="") { $error[variable]="Please enter the variable";} else { unset($error[variable]);} if ($error){ ?> html form <?php }else{ insert record ;} ?> Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 22, 2007 Author Share Posted September 22, 2007 Thanks for the reply... with that I get nothing but line errors (like a missing curly bracket) but I can't seem to resolve the errors exactly how would I incorporate this into my script? Quote Link to comment Share on other sites More sharing options...
rarebit Posted September 22, 2007 Share Posted September 22, 2007 Would help to see the script! Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 22, 2007 Author Share Posted September 22, 2007 It would be helpful to see more code, especially the query you're using. You might try using something like this. if(!isset($_POST['query']) || $_POST['query'] == "") { echo "No results found matching your query - $query"; } else { // process query using $_POST['query'] } This Almost works... what it does is print the "No results found matching your query" above each record...but it is still printing all the records Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted September 22, 2007 Share Posted September 22, 2007 post all your code mate... we most see the posts you are making too.. Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 22, 2007 Author Share Posted September 22, 2007 I would like to oblige but the script is so loaded with my own personal comments it would take me half an hour just to strip all the comments... is there a particula part of the script that would help specifically? the script queries a database for a single column but has pagination results... Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 22, 2007 Share Posted September 22, 2007 did u use my code? please list the errors wrong with it. Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 23, 2007 Author Share Posted September 23, 2007 did u use my code? please list the errors wrong with it. Yes thanks for the efforts... it throws numorious line errors starting with }else{ insert record ;} Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 23, 2007 Share Posted September 23, 2007 lol show me your code only way we can help. Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 23, 2007 Author Share Posted September 23, 2007 what part of the script? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 23, 2007 Share Posted September 23, 2007 show me everything before the html form php wise Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 23, 2007 Author Share Posted September 23, 2007 sorry I can't do that...without stripping all the comments the html form is a different page/file the action of the form(page) calls the script some of the suggestions in already posted print the error message but the script still returns all the records Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 23, 2007 Share Posted September 23, 2007 what do you want it to error? like if you dont fill in the form or what? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 23, 2007 Share Posted September 23, 2007 try <?php 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 exactly if the form is submitted with no entry in the form field display message Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 23, 2007 Share Posted September 23, 2007 try my above code ^ Quote Link to comment Share on other sites More sharing options...
rhock_95 Posted September 23, 2007 Author Share Posted September 23, 2007 try <?php if empty($_POST[variable]||$_POST[variable2]) { echo "sorry you must fill in the form";} ?> that throws a line error regardless of where in the script I set it Parse error: parse error, expecting `'('' Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 23, 2007 Share Posted September 23, 2007 please specify the line of the error and paste that line here 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.