0

I am having issues with php and form handling. I want to be able to click a dropdown box(like you can do with a form in html, with the different options tags) in php, and allow the selection to take the designated value.

Here is what I have :

<input value="<?php echo isset($results['example']) ? $results['example']: ''; ?>" class="form-control" name="data[example]" placeholder="Example">

That populates with a place you can type in input text, which is accepted perfectly in post, but I do not want the user typing in data, only selecting from options, such as this html form does :

<form action="example.php">
<option value="11">apples</option>
<option value="12">bananas</option>
<option value="13">oranges</option>
</form>

Here is an edit using the data given :

<form>
                            <select name="example">
                                <option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>">apples</option>
                                <option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>">oranges</option>
                                <option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>">bananas</option>
                            </select>
                        </form>

I did not add the action because it will be posted with other data, but did try both ways(with and without action="post.php") and it does not work...

3
  • I'm a little confused. Is this a front-end or back-end problem? Commented Jul 8, 2015 at 14:41
  • I don't think this question is clear at all. What happens when you just echo $results["example"]? Your code will currently give you three drop downs with the same value, with just different names for them. Posting them will always have the result of whatever $results["example"] is Commented Jul 8, 2015 at 15:05
  • Sorry to be ~that~ guy but I do not believe you have explained what you want very well. We may be of more help of you could provide more source code or at the very least a var_dump of $_POST, $_GET and $results. From the code snippets provided by you and various answer you will end up with a drop down whose visible text is apples, oranges, bananas but whose value will all be the same because they all take it form the same source ($results['example']). Commented Jul 8, 2015 at 15:15

6 Answers 6

1

Defining your options inside a select tag

<select>
<option value="11">apples</option>
<option value="12">bananas</option>
<option value="13">oranges</option>
</select>

Should give you in your post the position (value) of the selected item. Then in your php pass value to the selected items *11,12,13 = apples,bananas,oranges**

Hope it help

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

Comments

0

You need a <select name="name"> </select> around your options

Comments

0

I think you need something like that

<form action="example.php">
    <select name="fruit">
        <option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>">apples</option>
    </select>
</form>

Comments

0

I'd say the issue is not PHP, I think you just need to use the SELECT and OPTION form tags:

You are specifying an INPUT tag, which by definition allows the user to input data.

<form action="example.php">
 <select class="form-control" name="data[example]">
    <option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>"">
</select>
</form>

Comments

0

So you are looking for a dropdown I guess:

<form action="example.php">
    <select name="fruit">
        <option value="1">Apple</option>
        <option value="2">Banana</option>
        <option value="3">Orange</option>
    </select>
</form>

On submiting the form you can get the value like: $_POST['fruit'] (in this case).

Comments

0

use type="checkbox", so you can select one more

<input type="checkbox" name="vehicle[]" value="Bike"> I have a bike<br>
<input type="checkbox" name="vehicle[]" value="Car"> I have a car<br>
<input type="checkbox" name="vehicle[]" value="Boat" checked> I have a boat<br>

in php

<?php 
$vehicleArray='';
foreach($_POST['vehicle'] as $value) {
  $vehicleArray.=$value.',';
}
//output 'Bike,Car,Boat'
?>

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.