Max
Max

Reputation: 367

php file not getting my select value

So I'm trying to get the value of my select element. I've tried testing to see if my php file receives my value, but it doesn't seem to be working.

Here is 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's not giving me the value of all categories. Anyone know what I did wrong?

Upvotes: 3

Views: 2211

Answers (4)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

1.Try to put name attribute to select element like below:-

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

Now you will get the values.thanks.

2.<form action="index.html" method="post"> action must have some php file name otherwise your php code on that page will not work.

Upvotes: 4

Abey
Abey

Reputation: 1418

Your form action in incorrect. It should be to a .php file.

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

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

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

Also change the statement

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

`

Upvotes: 0

RaMeSh
RaMeSh

Reputation: 3424

just try like:

You have missed the index name i.e thats why you got the error like this.

Notice: Undefined index: allCategories1 .

Just put the name="allCategories1" in .

and here two options you have.

1.remove .html extention and put it .php file as index file.

2.just wrote php code and html code in one file and save entire 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 output just Click Here

Upvotes: 0

Guru
Guru

Reputation: 420

"name" attributes should come in tags like input, select. But you've added "name" attribute at "option". Thats the mistake you've done.
Make your html dropdown box like this one:

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

Form actions should have a file with .php extensions. But you've kept in .html file. You've to change file also to get this work.

Upvotes: 1

Related Questions