Computing.Net > Forums > Web Development > If statement code in PHP

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

If statement code in PHP

Reply to Message Icon

Name: rsasalm
Date: September 20, 2005 at 13:27:36 Pacific
OS: Win XP
CPU/Ram: P4
Comment:

Hi all,

I am new to PHP and web programming.
I have a very simple question,
I have following simple code:
****************************
<?php
if (isset($submit)) {
echo "If is executed";

} else {

echo "Else is executed";
?>
<HTML>
<BODY>
<FORM enctype="multipart/form-data" method="post" action="upload.php">
Upload image

Select image: <INPUT name="file" type="file">

<INPUT type="submit" name= "submit" value="submit">
</FORM>

</BODY>
</HTML>

<?php
}
?>
***********************
A form appears on the screen and when I click on submit button I should be in "If statement" and the code line
echo "If is executed"; should be executed
but instead
echo "Else is executed"; statement is executing all the time.
What I am missing so that when I click on
submit button if statement should be executed.
thanks
regards
/rsasalm



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: September 20, 2005 at 14:59:11 Pacific
Reply:

There are a couple problems with what you have above. First of all you are trying to reference a locally defined variable called $submit. Since you never defined it, it doesn't exist. You need to reference the $submit variable that was posted to the page from the form, like this:

if (isset($_POST["submit"])) {

Also, you are echo'ing text to the page before the headers have been written, so the text might not even show up depending upon the browser you use.


0

Response Number 2
Name: SN
Date: September 21, 2005 at 07:05:29 Pacific
Reply:

Michael's explanation is right on...I'm just clarifying a couple of things.

If you have register_globals on, then what you have above should work ($_POST['submit'] is put into a variable named $submit). But in recent versions of PHP, it is off be default (mostly because of security considerations.)

Secondly, I think PHP will output the http headers before you echo anything. But Michael may be talking about the HTML 'header' tags (HTML, HEAD, TITLE, STYLE, etc.)

-SN


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: If statement code in PHP

post HTML page in PHP www.computing.net/answers/webdevel/post-html-page-in-php/1756.html

Date in php www.computing.net/answers/webdevel/date-in-php-/506.html

RSS parser code problem in PHP www.computing.net/answers/webdevel/rss-parser-code-problem-in-php/754.html