roi
roi

Reputation: 47

Cannot get POST value

Why my echo doesn't print the value 1?

<dl>
    <input type='checkbox' class='tinyField' name="informTktUpdate['hd']" value="1" /> Inform user by email
</dl>
<dl>
    <input type='checkbox' class='tinyField' name="informTktUpdate['prog']" value="1"  /> Inform programmer by email
</dl>

echo ($_POST['informTktUpdate']['prog']);
echo ($_POST['informTktUpdate'][prog]);

I tried to remove the quotes:

name="informTktUpdate[prog]"

But still nothing...

Upvotes: 0

Views: 45

Answers (4)

Riyaz
Riyaz

Reputation: 139

try to change

<input type='checkbox' class='tinyField' name="informTktUpdate['prog']" value="1" />

to

 <input type='checkbox' class='tinyField' name="informTktUpdate[prog]" checked="checked" value="1" />

let it be checked by default and then echo $_POST['informTktUpdate']['prog'];

Upvotes: 0

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146450

Your HTML fields have these names:

informTktUpdate['hd']
informTktUpdate['prog']

Thus you need to add those quotes to the key names:

$_POST["informTktUpdate"]["'hd'"]
$_POST["informTktUpdate"]["'prog'"]

Since quotes in name attribute only add unnecessary verbosity, I suggest you simply get rid of them to begin with. Remember HTML is not PHP.

Also, please note you can use any regular dump function to inspect your variables, there's no need to guess:

var_dump($_POST);
array(1) {
  ["informTktUpdate"]=>
  array(2) {
    ["'hd'"]=>
    string(1) "1"
    ["'prog'"]=>
    string(1) "1"
  }
}

Last but not least, this code:

echo ($_POST['informTktUpdate'][prog]);

... should be triggering a notice. The fact that you don't see it suggests that you haven't configured your PHP development box to display error messages. If you fix that, further coding should be more focused.

Upvotes: 1

Girish
Girish

Reputation: 12127

in HTML, No need to single quote (') inside [] params in input name attribute

change

<input type='checkbox' class='tinyField' name="informTktUpdate['hd']" value="1" />

to

<input type='checkbox' class='tinyField' name="informTktUpdate[hd]" value="1" />

And try on server side

echo $_POST['informTktUpdate']['prog'];

Upvotes: 0

Quentin
Quentin

Reputation: 943569

It isn't entirely clear from your question it appears that it is because:

  1. You are trying to get the results of the form submission in the page that sends the form to the browser, and not the page to which the form is submitted and/or
  2. The checkbox is not checked (it certainly isn't by default, but you could change that in the UI). You only get the value if it is checked, which is how you can tell if it is checked or not.

Upvotes: 1

Related Questions