-2

I need how to get form POST value on the same page. Below is my script to try and extract the value as well as the form html:

<form action="" id ='list' name= "list" class="form-inline" method="POST">
    <div class="select">
        <select name="lamp" id ="lamp" onchange="gender(this)" style="background:transparent">
            <option id ='gender' hidden="hidden">Gender</option>
            <option value="0">Boy's</option>
            <option value="1">Girl's</option>
        </select>
    </div>
</form> 

My Php code:

if (isset($_POST)) {  
    $pg_type =$_POST['lamp'];
    echo $pg_type;die;
}

I have echo the my variabe it is showing like:

Notice: Undefined index: lamp in C:\xampp\www\htdocs\rentozy\assets\includes\list-header.php on line 20

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
rajkumar
  • 27
  • 5
  • how does form will send request to your php file until you haven't define `action=""` ? – Ravi Jan 13 '18 at 05:20
  • 1
    replace `isset($_POST)` with `isset($_POST['lamp'])` – Iłya Bursov Jan 13 '18 at 05:20
  • I imagine it has something to do with your inline javascript `gender(this)` sending the wrong value since there is no submit on this form. Probably related to your other question: https://stackoverflow.com/questions/48219386/jquery-onchange-issue-in-php – Rasclatt Jan 13 '18 at 05:23
  • action is same page only – rajkumar Jan 13 '18 at 05:24
  • As `$_POST` is a superglobal, I would believe `isset($_POST)` will always return true, even if it is empty (ie on initial page load). So you may want to check the for the actual field `isset($_POST['lamp'])` – Sean Jan 13 '18 at 05:27
  • i checked actual field like this code but not getting POST value sir isset($_POST['lamp']) .showing issue for :Notice: Undefined index: lamp in C:\xampp\w – rajkumar Jan 13 '18 at 05:33
  • What does the javascript function `gender(this)`? – ino Jan 13 '18 at 05:51

1 Answers1

1

The form:
1. be consistent - use " or ' but not both for the HTML attributes;
2. set the name of select to gender and remove the first option since it is nonsense in this example;

<form action="" id="list" name="list" class="form-inline" method="POST">
    <div class="select">
        <select name="gender" id="gender" style="background:transparent">
            <option value="0">Boy's</option>
            <option value="1">Girl's</option>
        </select>
    </div>
</form>

PHP:
1. change the if check to the exact key such as gender not to the whole POST array 2. To check what was actually send via POST you can print the whole array:

if (isset($_POST['gender'])) {  
    $pg_type=$_POST['gender'];
    die('Gender value in $pg_type is: ' . $pg_type); // for debugging
}
else {
     // for debugging
     print_r($_POST);
}
ino
  • 2,345
  • 1
  • 15
  • 27