Jump to content

Script not working


Vivid Lust

Recommended Posts

When there is no value in the input, the $pixel is displayed, and i dont want it to!!

 

Problem with the IF statement?? !

 

Please help!

 

Thanks!

 

Code:

 

<?php

// variables
$rate = "2";

// set image source
$img = "http://www.phpfreaks.com/forums/Themes/default/images/on.gif";

// configure image attibutes
list($width, $height, $type, $attr) = getimagesize($img);
$pixels = $width*$height;

// echo values
if( $_POST['url'] != "")
{
echo $pixels;
}


?>
<form action="index.php" method="post">
<input type="text" name="url">
<input type="submit" value="Go!">
</form>

Link to comment
Share on other sites

You never use one '=' in a condition.

 

Never is harsh...there are some situations where it's nice to use, however, of course I do agree with you in this "if" condition case it's incorrectly used.

 

To avoid this mistake in the future, code constant values on the left side of the condition. That way PHP will generate an error in case one forgets and uses one equal sign instead of two. Example:

 

if ('value' = $_POST['url']) {}  // This will produce an error (good)

 

if ($_POST['url'] = 'value') {}  // This will NOT produce an error and will always evaluate to true (bad)

 

 

Vivid Lust, I'm not sure what you're trying to do with this code and the "url" value (it's not being used). Here is a little better way to go:

 

<?php

$strSubmitted = isset($_POST['submitted']) ? $_POST['submitted'] : '';

// Check if form has been submitted first
if (!empty($strSubmitted)) {   // or can check for specific value: Go!

    $strUrl = isset($_POST['url']) ? $_POST['url'] : '';

    // variables
    $rate = "2";

    // Instead of hard coding this url did you mean to use the url from the form?
    // i.e.  $img = $strUrl
    $img = "http://www.phpfreaks.com/forums/Themes/default/images/on.gif";

    // configure image attibutes
    list($width, $height, $type, $attr) = getimagesize($img);
    $pixels = $width*$height;

    // echo pixels when url value is not empty
    if (!empty($strUrl)) {
        echo $pixels;
    }
}

?>

<form action="index.php" method="post">
<input type="text" name="url">
<input type="submit" value="Go!" name="submitted">
</form>

 

Link to comment
Share on other sites

toplays point is that php will stop with an error, cuz ya can't assigned a variable to a constant.

thus having the constant on the left hand side of the comparisons.

 

if($val=123)

will not generate an error, as $val is assigned 123, and continue with whatever code is in the if statement

 

if(123=$val)

will generate an error, giving u a chance to fix it :)

Link to comment
Share on other sites

I find this hard to believe, it would be just the same if you used '=='

 

I don't understand your comment/point. Post an example of what you're referring to.

 

 

 

whats with all the confusion? why doesnt he just use double '='?

 

Woudl

 

<?php
if( $_POST['url'] = "")
{ }else{
echo $pixels;
}?>

 

make a difference with

 

<?php
if( $_POST['url'] == "")
{ }else{
echo $pixels;
}?>

 

or

 

<?php
if('value' == $_POST['value'])
{ }else{
echo $pixels;
}?>

Link to comment
Share on other sites

whats with all the confusion? why doesnt he just use double '='?

 

LOL...yes he can use double equal signs. The confusion is your post with the comment I already indicated. I still don't understand what you meant by: "I find this hard to believe, it would be just the same if you used '=='"

 

What is hard for you to believe?  and what would be the same if you used '=='?

 

Why post such a thing?

 

 

 

Link to comment
Share on other sites

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.