LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP Post and GET method (https://www.linuxquestions.org/questions/programming-9/php-post-and-get-method-46302/)

dai 02-20-2003 07:58 AM

PHP Post and GET method
 
Hi all

Im starting to learn PHP. Im using the Beginning PHP 4 book by WROX and a couple of SAMS books.

Anyway in one book it explains the process of getting data from a form using the post/get approach by setting Action = "php script"

Thie php script is meant to output on screen the inputted data from the html form and looks like this: -

<?php
echo "thank you $nameofperson"
?>

WHen this script runs (using get method) I can see that the name is passed at the end of the url but the output in the browser is: -

thank you


Has the book Im using got its code wrong??? I have looked at another book and got the code working using $_GET(nameofperson) but I was wondering if its possible to set a script up as shown above???

DaveG 02-21-2003 02:51 PM

The HTML form needs to have a text input box named "nameofperson" and PHP "track_vars" and "register_globals" must be enabled in the PHP configuration file.
"track_vars" sets up arrays of $HTTP_GET_VARS etc. while "register_globals" makes all form fields avaiable as global variables with the same name as the form field.
Set up a page with just:
<?php phpinfo(); ?>
in it (named phpinfo.php) to see exactly what PHP thinks it's doing.

dai 02-21-2003 05:15 PM

cheers mate I thought it was a prob with setting Global_vars to off (using php 4.2.3 and its set to of by default)

Anybody able to answer this one

When using Switch (case statements) on inputs from another php page why when I use case (and no break) does the default not work. Heres the code for the input form: -


<?php
$Message1="Bugs Bunny";
$Message2="Homer Simpson";
$Message3="Ren & Stimpy";

echo "<Form method=GET action='answer.php'><BR>";
echo "<Strong>Which of the following is the best?</Strong><BR><BR>";
echo "<Input name ='Question' type=radio Value='$Message1'>$Message1<BR>";
echo "<Input name ='Question' type=radio Value='$Message2'>$Message2<BR>";
echo "<Input name ='Question' type=radio Value='$Message3'>$Message3<BR><BR>";
echo "<Input type=submit><Input type=reset>";
echo "</Form>";
?>

And the code for validation


<?php

switch ($_GET[Question])// uses the html name set in the form on page question.php

{//start parentheses for switch statement

case $_GET[Question]=="Bugs Bunny": //colon used for case
echo ("You thinks $_GET[Question] is the best<BR>");//semi-colon used for actions
echo ("Incorrect<BR><BR>");
echo ("<a href = whyu>Click here to try again</a>");
break;

case $_GET[Question]=="Homer Simpson":
echo ("You thinkz $_GET[Question] is the best<BR>");
echo ("Correct $_GET[Question] is the best<BR><BR>");
echo ("<a href = fytf</a>");
break;

case $_GET[Question]=="Ren & Stimpy":
echo ("You thinky $_GET[Question] is the best<BR>");
echo ("Incorrect<BR><BR>");
echo ("<a href = yfuyl]>Click here to try again</a>");
break;

case $_GET[Question]=="":

default:
echo ("You must choose an option");
break;

}//end parentheses for switch statement
?>

Zorglub 02-26-2003 01:31 AM

Hello:

He problem appears to be that it is ommited an html form to show how the variables are passed to php.

I think that you have to put a form in the end of your code (this is html code):

PHP Code:

<form name="test" method="post" action="my_page.php"

PHP Code:

<input type="text" name="nameofperson" value="your name here"

PHP Code:

<input type="submit"

[PHP]</form>

and it will appear a form in your page. If you fill a name -i.e. "dai" in the text box and press the "submit" buttom, the next time the page will show "thank you dai"

dai 02-26-2003 09:45 AM

Thanks for response but the form and GET method work fine.

The problem I get is with the lines

case $_GET[Question]=="":

default:
echo ("You must choose an option");
break;

If I dont select an option then it passes nothing but the output of default does not occur.


All times are GMT -5. The time now is 11:33 PM.