Jump to content

if statement from form post...probably a easy question


Alexhoward

Recommended Posts

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

 

 

 

Link to comment
Share on other sites

Hi Guys

 

Thanks the speedy replies  ;D

 

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

Link to comment
Share on other sites

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>";
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 {}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.