2

In PHP, I want to check if the radio buttons are checked; I do this with isset, but something isn't working.

html:

 <form action="test.php" method="post" name="form_contribute" id="form_contribute">
            <fieldset>
                <legend>Required information</legend> 
                <p>Type of publication</p>
                <div id="radio_buttons">
                    <label for="p_book">Book</label>
                    <input type="radio" name="p_radio" id="p_book" value="p_book">

                    <label for="p_article">Article</label>
                    <input type="radio" name="p_radio" id="p_article" value="p_article">

                    <label for="journal">Journal</label>
                    <input type="radio" name="p_radio" id="p_journal" value="p_journal">


                </div>

                <div id="contribute">

                    Title <input type="text" name="quote_title" id="quote_title">
                    Author(s)* <input type="text" name="author" id="author">
                    Title <input type="text" name="title" id="title">
                    ISBN <input type="text" name="isbn" id="isbn">
                    Publisher <input type="text" name="publisher" id="publisher">
                    Source <input type="text" name="source" id="source">
                    Addendum <textarea id="addendum"></textarea>
                    <div id="instructions">
                        <ul>
                            <li>Add only published non-fiction works; </li>
                            <li>Fill in all the required fields (designated by an asterix at the end);</li>
                            <li>Write the ISBN without space or special characters; if no ISBN, use identifier code as replacement;</li>
                            <li>Title should be written in English; exceptions are made for works unpublished in English;</li>
                            <li>Publisher should be the publisher of where your source derives from, not the original publisher of the work;</li>
                            <li>Source is to be used for verification; add chapter, section, page;</li>
                            <li>Use addendum to add other relevant info (e.g. if you've translated the material yourself; if the author is anonymous;) </li>
                        </ul>
                    </div> <!-- end #instructions -->
                </div>

            </fieldset>
            <div class="big_button">
                <input type="submit" name="edify_button" id="edify_button" value="Edify">
            </div>
        </form>

in php:

error_reporting(E_ALL); ini_set('display_errors', 'On');

    if (isset($_POST['edify_button']) && isset($_POST['p_radio'])) {

        $radio_values = $_POST['p_radio'];
        if ($radio_values) {
            echo 'checked';
        }else  {
            echo 'unchecked';
    }
}

I've also tried with !empty($_POST['my_radio']. I don't get any errors, just a blank page. In the if-statement, in a different test, I added that if $radio_values == null, then something, but that doesn't help either. Is there a value for checked, in php, that I'm unaware of?

Dan Sebastian
  • 519
  • 5
  • 19
  • 4
    *"I don't get any errors, just a blank page"* - Because you're not checking for them. Do you not have form tags and a POST method? Sure doesn't look like it. Add them, then *magic* will happen. – Funk Forty Niner May 06 '15 at 12:23
  • Try adding `} else { echo 'somethings not set';}` after your last closing curly; or `print_r($_POST);` at the start of your code. – chris85 May 06 '15 at 12:29
  • It's in a form with the method POST. I also get the blank page, so the question is how to check for them. I figured isset would suffice. – Dan Sebastian May 06 '15 at 12:33
  • @DanSebastian: in this code isn't problem, it works as expected. Please give us the rest of code. – pavel May 06 '15 at 12:34
  • This is how you check for errors: http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner May 06 '15 at 12:36
  • There's nothing really wrong with your code. Why don't you show us what you're ***really*** using by updating your question ;-) Plus, your conditionals could use a bit of work. You also want to use `OR` and not `AND`. – Funk Forty Niner May 06 '15 at 12:40
  • I expressed myself poorly. Allow me to rephrase. I'm not getting any error messages. What I want to do is target when no radio buttons are checked. As of now, I get a blank page, like you did. I tried to target this by writing,
    !isset($_POST['my_radio'])
    .
    – Dan Sebastian May 06 '15 at 12:43
  • @fred, right, but then I get an error saying, "Notice: Undefined index: p_radio in ...address.. line 8" --it's because I activated php strict; how do I go about without getting the error? – Dan Sebastian May 06 '15 at 12:51

2 Answers2

1
if (isset($_POST['my_button']) && isset($_POST['my_radio'])) {

        $radio_values = $_POST['my_radio'];
        if ($radio_values) {
            echo 'not null';
        }else {
            echo 'null';
    }}

You are getting blank page when no radio selected because you are validating isset($_POST['my_button']) && isset($_POST['my_radio']) together where as isset($_POST['my_radio'] will return true only when any of the radio button checked.

Manish Shukla
  • 1,355
  • 2
  • 8
  • 21
1

Use a double nested method, such as:

<?php

error_reporting(E_ALL);
ini_set('display_errors', 'On');

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

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

        $radio_values = $_POST['p_radio'];
            echo 'checked';
        }else  {
            echo 'unchecked';
    }
}

?>

and it'll make your problem go away, and work ;-)

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141