Php file not getting my select value

So I am trying to get the value of my select element. I tried testing to see if my php file is getting my value, but it doesn't seem to work.

Here's my PHP code:

<?php

if (isset($_POST['okButton'])){
    $value = $_POST['allCategories1'];
}

?>

      

And here is my HTML code:

<form action="index.html" method="post">
<select id="allCategories1">
    <option name ="All_categories" value="All_categories">All categories</option>
    <option value="No_category">No category</option>
    <option value="Shooter_games">Shooter games</option>
    <option value="Family_games">Family games</option>
    <option value="Action_games">Action games</option>
    <option value="Sport _games">Sport games</option>
</select>
<button type="submit" name="okButton" id="okButton">Ok</button>
    </form>

      

I don't know why, but it doesn't give me meaning for all categories. Does anyone know what I did wrong?

+3


source to share


4 answers


1. Try adding an attribute name

to select an element as shown below: -

<select id="allCategories1" name="allCategories1">

      



You will now get the .thanks values.

2. <form action="index.html" method="post">

action

must have some filename php

, otherwise your code php

on this page won't work.

+4


source


Form action is wrong . It should be in the file .php

.

<form action="my_file_name.php" method="post">

      

If you post to the same file, save it as .php (index.php in your case) and then the form action should be changed to

<form action= "" method="post">



Also change the operator

<select id="allCategories1">

to <select name="allCategories1">

`

0


source


just try:

You missed the ie index name, which is why you got this error.

Note: Undefined index: allCategories1.

Just put name = "allCategories1" .

and there are two options here.

1.remove the .html extention and put its .php file in the index file.

2.just wrote PHP code and html code in one file and saved the whole file as a .php file.

Code: -

<?php
if (isset($_POST['okButton'])){
    $value = $_POST['allCategories1'];
    echo $value;
}
?>

<form action="" method="post">
<select id="allCategories1" name="allCategories1">
    <option name ="All_categories" value="All_categories">All categories</option>
    <option value="No_category">No category</option>
    <option value="Shooter_games">Shooter games</option>
    <option value="Family_games">Family games</option>
    <option value="Action_games">Action games</option>
    <option value="Sport _games">Sport games</option>
</select>
<button type="submit" name="okButton" id="okButton">Ok</button>
    </form>

      

Output: -

For withdrawal just Click here

0


source


"name" attributes must come in tags such as input, select. But you added the "name" attribute to the "option". This is the mistake you made.
Create your html dropdown like this one:

<select id="allCategories1" name="allCategories1">

      

Form actions must have a file with .php extensions. But you saved the .html file. You also have to modify the file to get this working.

0


source







All Articles