Page 1 of 1

I have problem reading posted variables from a simple form

Posted: Sun Jun 01, 2008 10:56 am
by marjun1979
Hi folks,

I am a kind of newbie to php world. I have written a simple HTML form tag which sends data to a php script. Php script is in a separate file. In php script, I am just printing out what user writes in the html form. Here is the html tag:

Code: Select all

<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
       <title>My search Engine!</title>
</head>
<body>
 <h2> Search for a fruit </h2>
 <br>
 <FORM ACTION="result.php" METHOD="POST">
 Please enter the name, or part of the name or the fruit your looking for:
 <br>
 <INPUT NAME="name" TYPE=TEXT>
 <br>
 <INPUT TYPE=SUBMIT VALUE="search">
 </FORM>
</body>
</html>
 


Here is result.php:

Code: Select all

<?php
 
 echo $name;
 
    ?>

note that the name i am printing should come from user input. But I get a blank page. Browser sets the path to php script in address bar, nothing is running.

Could anyone please help me.

Thanks in advance.

Arjun

Re: I have problem reading posted variables from a simple form

Posted: Sun Jun 01, 2008 12:43 pm
by JAB Creations
Hi Arjun and welcome to DevNetwork!

I think you're trying to echo the post data to ensure that you are referencing the correct things before you start doing more complex stuff...at least that is what I think you're saying in your post.

Here is minimal code to help you understand the HTML/PHP relation of the data...

HTML

Code: Select all

<input name="my_name1" /><input name="my_name2" />
PHP

Code: Select all

echo $_POST['my_name1'];
echo $_POST['my_name2'];
Also don't interchange references, you'll confuse people (and clear communicate is essential when you start getting in to heavier code). I think print is deprecated though still supported? So stick to using echo and referring to it as echo.

Additionally try to use [ php ] [ / php ] and [ html ] [ / html ] BB code (without the spaces) and preferably if you have a single chunk of code post it without dividing it all up if possible. This will help make your posts easier to read (and easier for us to help you.

Any way you can also check your form data as so...

Code: Select all

if ( $_POST['my_name1'] == 'bob') {echo 'hi, your name is exactly bob!';}
Hope this helps!

Re: I have problem reading posted variables from a simple form

Posted: Sun Jun 01, 2008 3:58 pm
by califdon
marjun1979 wrote:Here is result.php:

Code: Select all

<?php
 
 echo $name;
 
    ?>
Welcome to the forum, Arjun. What you're missing is providing some value to the variable $name. It is not automatic. Since there is nothing to echo to the browser, it displays a blank page. Here is what you need to do:

Code: Select all

<?php
$name=$_POST['name'];
echo $name;
?>

Re: I have problem reading posted variables from a simple form

Posted: Mon Jun 02, 2008 9:03 am
by marjun1979
Thank you guys!

I could fix this error soon after I posted this question. But, what made me puzzled was the Author of some php video tutorial used the code exactly how I specified above and it worked for him. But, I guess, he might have used earlier version. Cool!! this forum is awesome. You guys are great!! thanks a lot for immediate reply.

I have one more question. How do you debug php code if something goes wrong. I am using PHP Coder. But I get only blank page in browser if something wrong in the code. I know, HTML page will be blank if php can't be executed. But, How do you guys handle that?

Thanks again.

-Arjun

Re: I have problem reading posted variables from a simple form

Posted: Mon Jun 02, 2008 10:39 am
by JAB Creations
It depends on what breaks. If it's PHP or MySQL you'll want to copy the error message (except the path on your server), paste it in to a search engine, stick quotes around it, and do a search.

(X)HTML should be validated as well as CSS and I typically use Chris Pederick's Web Developer Toolbar tools menu for that (and there are tons of other exceptionally useful options as well).

Re: I have problem reading posted variables from a simple form

Posted: Mon Jun 02, 2008 12:58 pm
by califdon
marjun1979 wrote:But, what made me puzzled was the Author of some php video tutorial used the code exactly how I specified above and it worked for him. But, I guess, he might have used earlier version.
Nope. That's always been true. Variables never just magically acquire values, you have to assign them. He must have assigned a value to $name earlier in his script, which he didn't show.
I have one more question. How do you debug php code if something goes wrong. I am using PHP Coder. But I get only blank page in browser if something wrong in the code. I know, HTML page will be blank if php can't be executed. But, How do you guys handle that?
This is always a problem with PHP, due to the way it is interpreted by the web server. You're depending on the web server to find errors, but it's not set up to issue much in the way of debugging info. If it can't interpret the PHP, it merely stops interpreting it, and then if there's no more plain HTML, there's nothing to send to the browser. You can turn on PHP's error reporting temporarily, but this won't always give you usable errors, either. See http://us.php.net/error_reporting.

We adopt defensive programming techniques, such as echoing (printing on the screen) key variables, when and where we have problems. These debugging lines can be removed later, when everything is running correctly. It helps, also, to avoid writing a lengthy and complicated script all at once, before testing a greatly simplified version, with "stubs" for a lot of the content. If the simplified version runs, then as you add more logic, you test again, so that you realize when it doesn't run, that you have just added something that's wrong. I sometimes do the reverse, too: when a lengthy script fails, I start commenting out whole blocks of code, until it works again, then I can home in on which block of code is faulty.

It also helps to use "best practice" coding and formatting techniques, which make it easier to spot silly mistakes--we all make 'em! Here's a bad and a good example of what I mean. Bad:

Code: Select all

$result=mysql_query("select ordernum,orderdate,ordtotal,name,address,city,state,zip,phone from customers as c,orders as o where o.custid=c.custid and custid=$id order by state,city");
Better:

Code: Select all

$sql="SELECT ordernum, orderdate, ordtotal, name, address, city, state, zip, phone ";
$sql.="FROM customers AS c, orders AS o ";
$sql.="WHERE o.custid=c.custid AND custid=$id ";
$sql.="ORDER BY state, city";
      echo "SQL = $sql";  // remove this line after debugging!
$result=mysql_query($sql);
Now you can see if your SQL query is being formed the way you expected it to, for example, does it have a value for the $id parameter?

Re: I have problem reading posted variables from a simple form

Posted: Tue Jun 03, 2008 9:00 am
by marjun1979
Thank you guys!!

I am very glad to see your quick responses. I will keep in mind your tips and hints. Enjoy yourself.

-Arjun