Jump to content

Simple Php Beginner Question


Go to solution Solved by Christian F.,

Recommended Posts

Hello.

 

I've recently started learning PHP, and this has been irking me since:

 

<?php
$myTextbox = $_POST['myTextbox'];
if(isset($myTextbox)&&!empty($myTextbox)){
echo 'You Typed: '.$myTextbox;
}
?>


<html>
<head>
<title>My 39 Site!</title>
</head>
<body>
<form name = 'myForm' action = 'my39file.php' method = 'post'>
<input type = 'text' name = 'myTextbox' value = '<?php echo $myTextbox; ?>'/>
<input type = 'submit' name = 'submit'/></br></br>
</form>
</body>
</html>

 

It's a simple code I wrote. It has no errors, and it works just fine. The problem is that I don't understand WHY it works. I've been told numerously that the compiler reads code from top the bottom, but I'm failing to see how the rule applies in this case.

 

For instance, at the beginning of the code, I declared the variable $myTextbox and set it equal to the value of the textbox I created in HTML below. This boggles me. The textbox is created AFTER I declare the variable, so how does the computer know what form element I'm talking about?

 

I'm probably just omitting something. Could anybody enlighten me?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/271432-simple-php-beginner-question/
Share on other sites

  • Solution

As Pika mentioned you should be getting a notice there, because you're assigning the variable before checking if the source exists. Also, after setting a variable it will always be set, so checking it with isset () is redundant.

What you should have done, is test whether or not $_POST['myTextbox'] was set (and not empty), as that's the variable (or rather, array index) which may or may not be set.

 

Now, for the reason it works:

As noted above the first time you view the page you are indeed getting the result you expect, you just don't see it and it's not an error. While the $myTextbox variable is set, it's still empty because the $_POST index wasn't set. That's why the PHP parser skips the output inside the IF-block.

Once it's done with the test, you've closed the PHP parsing. Meaning that the server will send the rest as pure text straight to the browser. After which the script will die, as there is nothing more to parse.

 

However, when you submit the form you're sending a request for the same page to be viewed again. This time with some extra data POSTed back, from the form. So this time around the $_POST index is set, contains a value, and thus can pass this on to the $myTextbox variable. Which then isn't empty any more, so that the code inside the IF-block is read and executed.

After which the script continues on, just like in the above scenario. With closing the PHP parsing and sending the text (in this case formatted as HTML) to the browser.

 

What is important here is to be able to distinguish between what happens on the server, the actual PHP code, and what gets sent to the client (read: browser) for it to parse. You are dealing with two different languages, which are parsed by two different applications on two different computers at two different times. They only thing they have in common, is the coincidence that the first language (PHP) is told to generate output in the form of the second (HTML).

PHP knows nothing of what happens on the client side, unless the client explicitly sends it said data. As in the case with the form in your code.

Edited by Christian F.

I declared the variable $myTextbox and set it equal to the value of the textbox I created in HTML below

 

You don't define it until you click submit then the value is POSTED to $myTextbox = $_POST['myTextbox'];

 

Also put your php code in

 

if (isset($_POST['submit'])) { // this name must match your form input name at <input type = 'submit' name = 'submit'/>

$myTextbox = $_POST['myTextbox'];

if(isset($myTextbox)&&!empty($myTextbox)){

echo 'You Typed: '.$myTextbox;

}

 

}

 

as you play more put the footer under the echo and under the footer place exit() to stop the script.

Edited by floridaflatlander
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.