Jump to content

Recommended Posts

Hi everyone,

I've got another basic question.

The "isset" means you have your variable defined right?

When I use it, it seems to do the opposite of what I would expect it to do.

 

Here's my example:

 

<?php

$name = "Danielson";

 

if (isset($_POST['$name'])) {

 

echo ("<h2> Hi, my name is " .$name.".</h2> <br />");

} else {

echo ("I'm confused!");

}

 

 

 

?>

 

It echo's "I'm confused".

But with "!isset" it echo's"Hi, my name is Danielson.

 

Why? Shouldn't it be just the opposite?

Link to comment
https://forums.phpfreaks.com/topic/147096-isset/
Share on other sites

By asking if $_POST['$name'] is set you are asking for php to pull something from a form that you sent using a post variable. instead you are defining the variable on the same page. try this.

 

<?php
$name = "Danielson";

if (isset($name)) {

echo ("<h2> Hi, my name is " .$name.".</h2> <br />");
} else {
echo ("I'm confused!");
}

 

*looks like phpdragon beat em to it and explained it better*

Link to comment
https://forums.phpfreaks.com/topic/147096-isset/#findComment-772239
Share on other sites

If you send the information from a form then you need to use $_POST, it doesnt matter if the form is on the same page and it simply reloads itself when submitted, $_POST is the method for retreiving this variable

 

if you define a variable then you call it directly

 

eg

 

$name=$_POST['name'];

would set $name to the name submitted in the form, you then retreive it by just calling $name in php tags.

 

 

if you set $name='John';

 

then everywhere you echo $name John would appear

 

Link to comment
https://forums.phpfreaks.com/topic/147096-isset/#findComment-772253
Share on other sites

No. Also, I wouldn't use

<?php
if ($_POST['field']))
?>

to see if a field has a value. This test can return a false negative if the value entered is 0 (zero).

 

Run this example to see the results of the different tests:

<?php
if (isset($_POST['submit'])) {
echo '<pre>' . print_r($_POST,true) . '</pre>';
if (isset($_POST['name'])) echo '$_POST[\'name\'] is set<br>';
if ($_POST['name']) echo '$_POST[\'name\'] is TRUE<br>';
if (strlen(trim($_POST['name'])) > 0) echo "\$_POST['name'] has a value of <span style='font-weight:bold;color:red'>" . $_POST['name'] . '</span><br><br>';
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title></title>
</head>

<body>
<form method="post" action="">
Name: <input name="name" type="text"><br>
<input type="submit" name="submit" value="Test It">
</form>

</body>
</html>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/147096-isset/#findComment-772381
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.