3

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?

0

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

"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.

Comments

0

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

Comments

0

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">

`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.