Alexhoward Posted April 27, 2008 Share Posted April 27, 2008 Good Evening i'm a bit stumped as was wondering if anyone could help me... what i'm trying to do is look at the contents of the form, once the submit button has been pressed, and say if this field is there do this, and if not do this. i'm trying things like: if ($_GET['example'] == "boris") ; { .... if ($_POST['example'] == "boris") ; { .... am i going about it all the wrong way...? also i'm using php5, so does == still work...? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/ Share on other sites More sharing options...
DeanWhitehouse Posted April 27, 2008 Share Posted April 27, 2008 yer, your on the right track, and i dunno about php5 Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528089 Share on other sites More sharing options...
Fadion Posted April 27, 2008 Share Posted April 27, 2008 if($_GET['example'] == "boris"){ //removed the semicolon Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528090 Share on other sites More sharing options...
Alexhoward Posted April 27, 2008 Author Share Posted April 27, 2008 Hi Guys Thanks the speedy replies i'm trying if($_GET['example'] == "boris"){ to work but it's coming back with: Undefined index: example the post fields name is definatly the same... however i'm posting from a drop down, populated from mysql, is this a problem...? <?php include("config.php"); // connect to the mysql server $link = mysql_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysql_error()); // select the database mysql_select_db($database) or die ("Could not select database because ".mysql_error()); echo'<select name="example" style="width:327px">'; $res=mysql_query("select examp from examples"); if(mysql_num_rows($res)==0) echo "there is no data in table.."; else for($i=0;$i<mysql_num_rows($res);$i++) { $row=mysql_fetch_assoc($res); echo"<option>$row[cat]</option>"; } ?> Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528099 Share on other sites More sharing options...
DeanWhitehouse Posted April 27, 2008 Share Posted April 27, 2008 you need a space after == numbers need to have spaces between them eg. $word == number not $word ==number same with all numbers they need a space before and after Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528100 Share on other sites More sharing options...
Alexhoward Posted April 27, 2008 Author Share Posted April 27, 2008 hi, the post form works fine. it's the getting it before i insert it into the table that's the problem, is this what you're pointing at...? cheers Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528105 Share on other sites More sharing options...
DeanWhitehouse Posted April 27, 2008 Share Posted April 27, 2008 this is wrong if(mysql_num_rows($res)==0) echo "there is no data in table.."; else for($i=0;$i<mysql_num_rows($res);$i++) { $row=mysql_fetch_assoc($res); echo"<option>$row[cat]</option>"; } ?> it should be if(mysql_num_rows($res)==0) { echo "there is no data in table.."; } else { for($i=0;$i<mysql_num_rows($res);$i++) { $row=mysql_fetch_assoc($res); echo"<option>$row[cat]</option>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528106 Share on other sites More sharing options...
Alexhoward Posted April 27, 2008 Author Share Posted April 27, 2008 ok, but that bit works fine. i get the results, and it doesn't say "there is no data in table.." so don't worry about that it good practice to do that thou Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528110 Share on other sites More sharing options...
DeanWhitehouse Posted April 27, 2008 Share Posted April 27, 2008 so what part of this code doesn't work? Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528112 Share on other sites More sharing options...
Fadion Posted April 27, 2008 Share Posted April 27, 2008 Blade, try the code before posting, as soon as ure not experienced with php. In your two last posts, ure wrong, not the OP. $this==$that will work and so will do the if-else statement that he wrote. Alexhoward, Undefined index: example means that the index 'example' does not exist in $_GET array. I dont see a <form> in your code so i guess thats the problem. Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528114 Share on other sites More sharing options...
Alexhoward Posted April 27, 2008 Author Share Posted April 27, 2008 Hi GuiltyGear thanks for the info i only cut out that section, so that's why there's no form. it's quite big, and everything else posts fine. what i'm trying to do is post the form, and if this the criteria says "whatever", post like this, and if not post like this but it's the picking up before i insert into the database that i'm sruggling with Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528118 Share on other sites More sharing options...
DeanWhitehouse Posted April 27, 2008 Share Posted April 27, 2008 soz, i thought that ==0 did't work, Blade, try the code before posting, as soon as ure not experienced with php. In your two last posts, ure wrong, not the OP. $this==$that will work and so will do the if-else statement that he wrote. Alexhoward, Undefined index: example means that the index 'example' does not exist in $_GET array. I dont see a <form> in your code so i guess thats the problem. it's my fault i was wrong, i thought that didn't work, and i wasn't going to try the code as i am just looking and saying what i thought was wrong. And i never said the if=else statement wouldn't work, i said it was wrong ,the correct way to write it is with the {} Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528122 Share on other sites More sharing options...
Fadion Posted April 27, 2008 Share Posted April 27, 2008 Blade, wrong=not working, but im not starting flames, i just gave an advice. I know that good scripting is a long way to learn (definitely including myself), so im not blaming on you. Im just saying to be more careful on code and advices u give. Alexhoward, pls post the form code, only the <form> tag not with all its childs. An example of a form would be: <form id="myForm" name="myForm" method="get" action="mypage.php"> Id suggest to use post as the method, so u dont pollute the url with variables that the users arent interested in. Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528127 Share on other sites More sharing options...
Alexhoward Posted April 27, 2008 Author Share Posted April 27, 2008 ah, it's working with $_REQUEST is this the correct one to be using then..? Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528128 Share on other sites More sharing options...
Fadion Posted April 27, 2008 Share Posted April 27, 2008 Probably ure using post as a method, and by using get before didnt work. $_REQUEST works for get, post and cookie, thats why it worked with it. Instead use $_POST to keep track of specific variables, as $_REQUEST is not recommanded. Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528132 Share on other sites More sharing options...
Alexhoward Posted April 27, 2008 Author Share Posted April 27, 2008 Excellent, so are you suggesting having a seperate form for every input box, rather than one form around the entire table...? Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528135 Share on other sites More sharing options...
Fadion Posted April 27, 2008 Share Posted April 27, 2008 No, no absolutely. A form with several inputs is perfect. I was just trying to tell to not use $_REQUEST, but $_POST indead. Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528138 Share on other sites More sharing options...
Alexhoward Posted April 27, 2008 Author Share Posted April 27, 2008 ah, but $_POST didn't work...? Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528140 Share on other sites More sharing options...
Fadion Posted April 27, 2008 Share Posted April 27, 2008 It works with request, doesnt work with get, and also doesnt work with post? It makes no sense. Please post the <form>. Quote Link to comment https://forums.phpfreaks.com/topic/103094-if-statement-from-form-postprobably-a-easy-question/#findComment-528141 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.