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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

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

Post 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:
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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.
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

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

Post 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)?
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

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

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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.
Post Reply