-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

7
  • how does form will send request to your php file until you haven't define action="" ? Commented Jan 13, 2018 at 5:20
  • 1
    replace isset($_POST) with isset($_POST['lamp']) Commented Jan 13, 2018 at 5: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: stackoverflow.com/questions/48219386/… Commented Jan 13, 2018 at 5:23
  • action is same page only Commented Jan 13, 2018 at 5: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']) Commented Jan 13, 2018 at 5:27

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.