Page 1 of 1

Novice question about POST and $_SERVER['PHP_SELF']...

Posted: Sat Jul 25, 2009 8:27 pm
by Wolf_22
I'm working on a tutorial that involves database transactions. In order to set up a form I plan on using within the latter parts of the tutorial, I thought that POST'ing to PHP_SELF to return the values of the form inputs would help me along the way with seeing how things process. While I'm now unsure as to the virtue of this, I'm now wanting to understand what is going wrong with it regardless. So to that end, below is the code I'm currently working with:

Code: Select all

<body>
<form action="<?php echo basename($_SERVER['PHP_SELF']);?>" method="POST">
     <label>Store a joke of your own: <input type="text" value="" name="vis_joke" /></label><br />
     <input type="submit" value="Submit" name="submit" />
</form>
 
<?php
     //Grab user input and store into variables...
     if(!isset($_POST['vis_joke'])){
          $vis_joke = 'This form has not been used yet.';//not set (form has not been used yet)...
     }elseif($_POST['vis_joke'] = ''){
          $vis_joke = 'The submitted value was spaces. Unacceptable!';//form used but blank space submitted...
     }else{
          $vis_joke = mysql_real_escape_string($_POST['vis_joke']);//good?...
     }
     echo $vis_joke;
     ?>
</body>
Ignoring the obvious security holes and non-standards compliant code (some of this was done in a hurry), I'm curious as to why the values here within $vis_joke are not being displayed at the end when I press "submit." They WERE being displayed after each submit, however, I recently tried to make the second condition value of $vis_joke display if and when it's set value was nothing more than blank spaces (which, to me, was different than just "being set" [first condition]). The last condition, of course, is the one where the value of $vis_joke is appropriate and "normal."

I know I'm way off on this, but if anyone could shine some light here, I would really appreciate it. I think I'm over-thinking things... :banghead:

Re: Novice question about POST and $_SERVER['PHP_SELF']...

Posted: Sat Jul 25, 2009 9:14 pm
by califdon
Actually, you're not far off, and you already show that you understand some of the pitfalls that you aren't yet providing for in your code, so I'd say you're doing pretty well.

The first thing that I notice is that on Line 11 (as it's displayed in your forum post) you need to use the comparison operator == rather than the assignment operator =. When you test the True/False of a statement that assigns a value to a variable, it will always return True, therefore your if logic will never reach the line that assigns a value to $vis_joke, and consequently, outside the conditional statement, when you try to echo the value, it's empty.

Re: Novice question about POST and $_SERVER['PHP_SELF']...

Posted: Sat Jul 25, 2009 9:57 pm
by Wolf_22
10-4 on the comparison operator. I laughed when I read your response on that part. Hit me like a train!

As for the other part you speak of, I have something to follow-up on that...

If assigning something a value always equates to TRUE, then shouldn't the aforementioned condition be executed (the one where I accidentally put the assignment operator instead of the comparison)?

Re: Novice question about POST and $_SERVER['PHP_SELF']...

Posted: Sun Jul 26, 2009 9:19 am
by Eric!
Wolf_22 wrote:If assigning something a value always equates to TRUE, then shouldn't the aforementioned condition be executed (the one where I accidentally put the assignment operator instead of the comparison)?
It seems the value assigned plays a role

Code: Select all

if($a=1) echo "I am true";
if($a=0) echo "I am true too";
if($a='Hello') echo "Hello is true.";
if($a='') echo "No hello is true";
 
Only prints I am true. and Hello is true. So FALSE values like nulls or zero return false, everything else is true.

Re: Novice question about POST and $_SERVER['PHP_SELF']...

Posted: Sun Jul 26, 2009 11:55 am
by califdon
Eric! wrote:It seems the value assigned plays a role

Code: Select all

if($a=1) echo "I am true";
if($a=0) echo "I am true too";
if($a='Hello') echo "Hello is true.";
if($a='') echo "No hello is true";
 
Only prints I am true. and Hello is true. So FALSE values like nulls or zero return false, everything else is true.
Thanks for that. I guess I never realized that. No doubt because it's not something you'd ordinarily do.

Anyway, the OP learned the same lesson that probably every one of us had to learn, about the distinction between a comparison operator and an assignment operator.