How to properly pass form data in php?

2020-04-21 01:10发布

I'm using php to build a small form that requires passing 2 variables for processing.

I got the example off w3schools and although the info gets passed in the URL, the php form doesn't process it in any way (let alone extract it).

I'm just wondering if there might be anything wrong with my WAMP server.

    <html>
    <body>
    welcome
    Welcome <?php echo $_GET["fname"]; ?>.<br />
     You are <?php echo $_GET["age"]; ?> years old!
    </body>
    </html> 

HTML form:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://        www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html;                 charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body>
    <form action="welcome.php" method="get">
     Name: <input type="text" name="fname" />
     Age: <input type="text" name="age" />
     <input type="submit" />
     </form>
    </body>
    </html>

6条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-04-21 01:49

Your code seems correct. Does PHP work? Does

echo phpinfo();

work? If yes, what does

var_dump($_SERVER); 

give you? Do the GET parameters appear there?

查看更多
▲ chillily
3楼-- · 2020-04-21 01:50

This script should work as far as I can see. What is your WAMP-setup?

Try to upload the script on a free server that uses php and run it there.

查看更多
不美不萌又怎样
4楼-- · 2020-04-21 01:59

Just add <?php echo phpinfo(); ?> after that run your page for example page http://localhost/yourpage.php again, now just see whether error_reporting = E_ALL and display_errors = 1 is set or not if not then you have to configure your php.ini,

查看更多
Emotional °昔
5楼-- · 2020-04-21 02:04

This should work fine code-wise. Did you save the php file in the same directory as 'welcome.php'?

查看更多
聊天终结者
6楼-- · 2020-04-21 02:08

Indeed as already noted by various posts, this code should work. In my experience the php tags are output directly if they're not parsed. Do you see anything on the page at all? You could also check the sourcecode of the page and check if you can spot anything wrong there.

Also when copying from examples on the internet, sometimes you get weird characters that interrupt the parser. Usually this results in an error (which is not the case here), there's no harm in checking though.

Try outputing something simple and see if that works:

<?php echo "Hello World"; ?>

Can't think of anything else at the moment...

查看更多
时光不老,我们不散
7楼-- · 2020-04-21 02:09

Check URL if you can view values in url and still can't able to fetch it then try var_dump($_REQUEST); and my suggestion is to try to submit form in same page because when i was fresher i write both codes in same page but action page is different so i was confused why its not working

查看更多
登录 后发表回答