soyan
soyan

Reputation: 59

Why there's no output generated?

What wrong with my code? uniqid() is for generating a unique code and it will be stored to the name attribute then a condition is stated if it is clicked it will generate it's working.. Could some help me please with this one? Thanks in advance..

 <html>
    <form method="POST">


    <?php

    $attname = uniqid();

    echo "<input type='submit' name='$attname' class='btn btn-success' value='Post to Peónline'/>";
    echo $attname;
    if(isset($_POST[$attname])){
    echo 'its working';
    }
    ?>
    </form>

    <html>

Upvotes: 4

Views: 226

Answers (2)

Pupil
Pupil

Reputation: 23948

The $attname will change after page refresh.

You need to store that name somewhere.

Add another hidden element.

...
echo "<input type='hidden' name='elemname' value='<?php echo $attname;?>'/>";
...

And after submit,

if (isset($_POST['elemname'])) {
  $elemname = $_POST['elemname'];
  $val = isset($_POST[$elemname]) ? $_POST[$elemname] : '';
  echo $val;
}

Upvotes: 0

EM-Creations
EM-Creations

Reputation: 4301

This isn't going to work.

When you refresh the page the $attname value will change. This will happen when you submit the form. So the actual name you're checking on will change and not be the same as the new $attname.

Put the following after your echo $attname; line:

print_r($_POST);

Also for this to work properly you'll need to nest the <input> tag in a <form> tag e.g.:

<form method="POST">
<input>...</input>
</form>

Upvotes: 3

Related Questions