In an HTML form when I use a radio button how will the php program where the data goes to will know the correct option chosen?

in HTML the script will be like:

<form action='abc.php' method='post'>
<LABLE>YES</LABLE>
<input type='radio' name='agree'>
<LABLE>NO</LABLE>
<input type='radio' name='agree'>
</form>

Now since both options have same name'agree', how will php variable know the difference. i.e. whether the client has chosen YES or NO.

Recommended Answers

All 9 Replies

You are missing value attributes for each radio button. The chosen value (only one) will get carried over to the abc.php page through $_POST global array. Note that you are also missing a submit button to submit the form. On abc.php start with checking if $_POST['agree'] is set which means the form has been submitted, and then process it.

<form action='abc.php' method='post'>
<label>YES</label>
<input type='radio' name='agree' value='yes' />
<label>NO</label>
<input type='radio' name='agree' value='no' />
<input type='submit' name='submit' value='Submit' />    
</form>

Then on abc.php

if(isset($_POST['agree'])) {

    if($_POST['agree'] == 'yes') {

        // code for YES here


    } elseif($_POST['agree'] == 'no') {

        // code for NO here

    }
}

Yes.. broj1...thanks a lot for the quick reply.
In fact I have both the books open here the HTML black book by Holzner and the famous Murach's php book. And just after i wrote down the question....i got the hint in RADIO attributes- value=' '..that could do the trick..!

I have done the form exactly like you showed...!
However in php...just assigning the variable from $_POST['agree''] is working too.

<? php
$agree=$_POST['agree'];
?>
<html>
<label>RESULT</label><?php echo $agree; ?>
</html>

Although i very much agree that such a detailed scripting using isset($var) and IF ..ELSEIF loops is good programming practice...and finally i will use it in my uploaded form.

Ocnsider that the user types in a URL abc.php or send it by email to someone else. I these cases $_POST would not contain any values and the script would display an error message. That is why you check with isset() and handle error yourself. The more complete code would be:

if(isset($_POST['agree'])) {

    if($_POST['agree'] == 'yes') {

        // code for YES here

    } elseif($_POST['agree'] == 'no') {

    // code for NO here

    } else

    // $_POST is set but does not contain value Yer or No
    // error message "Please select Yes or No"
    // (or redirect back to the form)

} else {

    // $_POST is not set
    // error message "Please select Yes or No"
    // (or redirect back to the form)
}

Ok....broj1!
Now i have an even tougher level problem..i am trying to solve it for some hours but ..seems now i need help!

I am trying to POST multiple options selected in a form into a PHP program...but i can't get the php logic.

<form action='library.php' method='post'>
<select name='bookshelf' multiple>
<option>Alice's wonderland</option>
<option>Secret Seven</option>
<option>Jonathan Livingston Seagull</option>
</select>

What should i use in php to get more than one value. Should it be a for-while loop or some array or anything else.

Give your select element a name that is an array (like bookshelf[]). Upon submision your post array will contain an array with selected values.

<form action='library.php' method='post'>
<select name='bookshelf[]' multiple='true'>
<option value="alice_s_wonderland">Alice's wonderland</option>
<option value="secret_seven">Secret Seven</option>
<option value="jonathan_livingston_sagull">Jonathan Livingston Seagull</option>
</select>
<input type="submit" name="submit" value="Submit books" />
</form>

And the library.php

<?php
// check if the form is submitted, the bookself array exists and is not empty
if(isset($_POST['submit']) && isset($_POST['bookshelf']) && !empty($_POST['bookshelf'])) {

    // $_POST['bookshelf'] is an array of selected options, process it the way you like
    // i.e. you can loop through it with foreach
    // I will implode it into a comma separated string and display it
    echo implode(', ', $_POST['bookshelf']);

} else {

    echo 'No titles were selected. Go back and select at least one title.';
}
?>

Ok..let me try. Thanks.

Oh..ok..got you!
Ya its working for me when i use the array [] and echo statements with
foreach($_POST['bookshelf'] as $books)
echo $books."\n";

I will finally be passing the values via email output and/or input in a database...! Let's see how it goes. But if i need some help...I will ask in this page itself.. Thank you.

You are welcome and come back if you need more help. If you decide it is OK mark the thread as solved.

broj1 Hello!

yup i ultimately want to pass the array into an html file and also send it over mail...so the implode( ....); function suits me. For now..all set.

So thanks millions!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.