SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Enthusiast Yonko's Avatar
    Join Date
    Jun 2001
    Location
    Panama
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with PHP-generated radio buttons

    I really need help. I'm making a news update page for a client, I need to make the category appear selected in the corresponding radio button when I enter the modify news page. I'm using MySQL. I'm pasting the code:

    <?php
    while ($cat = mysql_fetch_array($cats)) {
    $cid = $cat["ID"];
    $cname = $cat["Name"];

    // Check if the news is in this category
    $result = mysql_query(
    "SELECT * FROM News " .
    "WHERE ID=$id AND CID=$cid");
    if (!$result) {
    echo("<P>Error al mostrar los detalles de la noticia: " .
    mysql_error() . "</P>");
    exit();
    }

    // mysql_num_rows gives the number of entries
    // in a result set. In this case, if the result
    // contains one or more rows, the condition
    // below will evaluate to true to indicate that
    // the news does belong to the category, and the
    // checkbox should be checked.
    if (mysql_num_rows($result)) {
    echo("<INPUT TYPE=RADIO CHECKED NAME='$cid'>$cname<BR>\n");
    } else {
    echo("<INPUT TYPE=RADIO NAME='$cid'>$cname<BR>\n");
    }
    }
    ?>

    What am I doing wrong? HELP and THANKS

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Something to try

    QUESTIONS: Where is the id in your select statement coming from? Have you echoed out all of your variables?

    You probably want to add a value="Yes" or something inside of your radio tag:

    echo("<INPUT TYPE=RADIO CHECKED VALUE='Yes' NAME='$cid'>$cname<BR>\n");
    Last edited by rgremill; Jul 5, 2001 at 08:53.

  3. #3
    SitePoint Enthusiast Yonko's Avatar
    Join Date
    Jun 2001
    Location
    Panama
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RE: Help

    It comes from a Table called News. Why?

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Another Question

    I just wanted to see if your query is coming back with records.

    Print out mysql_num_rows($result), what does it equal to?

  5. #5
    SitePoint Enthusiast Yonko's Avatar
    Join Date
    Jun 2001
    Location
    Panama
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I do that?

    Sorry, but I don't understand what you mean by 'print out mysql_num_rows($result)'

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Add

    First, let me start all over.

    What exactly is the problem?

    The radio button isn't being checked?

    If that is the case, you need to echo out the value of mysql_num_rows($result)) before your if statement.

  7. #7
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Yet another question...

    disregard
    Last edited by rgremill; Jul 5, 2001 at 09:28.

  8. #8
    SitePoint Enthusiast Yonko's Avatar
    Join Date
    Jun 2001
    Location
    Panama
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re:

    The first problem is that the button doesn't appear checked, the second thing is one can select multiple buttons and I want to restraint the selection to only one.

  9. #9
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OK, try this.

    The name of your radio button should be the same in all cases. Change it to

    if (mysql_num_rows($result)) {
    echo("<INPUT TYPE=RADIO CHECKED NAME='myradiobutton'>$cname<BR>\n");
    } else {
    echo("<INPUT TYPE=RADIO NAME='myradiobutton'>$cname<BR>\n");
    }

    and also add the value attribute...

  10. #10
    SitePoint Enthusiast Yonko's Avatar
    Join Date
    Jun 2001
    Location
    Panama
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What do you mean by "all cases"?

    I'm not using HTML radio buttons, I generated them by pulling the value from the Table. So I get three radio buttons, but none of them checked. At least one should appear checked, because every news has it category. Sorry for the lack of knowledge.

  11. #11
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Next step.

    Can you post the HTML that results from your program?

  12. #12
    SitePoint Enthusiast Yonko's Avatar
    Join Date
    Jun 2001
    Location
    Panama
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    This is the HTML generated

    <!-- editnews.php -->
    <HTML>
    <HEAD>
    <TITLE> Modificar Noticia </TITLE>
    </HEAD>
    <BODY>

    <FORM ACTION="/phpfiles/editnews.php" METHOD=POST>
    <P>Modificar la noticiaBR>
    <TEXTAREA NAME="text" ROWS=15 COLS=45 WRAP>Lo vieron. El Chupacabras ataca de nuevo en Azuero.</TEXTAREA>
    <br>
    T&iacute;tulo:
    <input type="text" name="title" size="40" value="Chupacabras">
    <br>
    Autor:
    <SELECT NAME="aid" SIZE=1>
    <OPTION SELECTED VALUE='1'>Esteban Giron
    </SELECT>
    <P>En categoríaBR>
    <INPUT TYPE=RADIO NAME='1'>Nacionales<BR>
    <INPUT TYPE=RADIO NAME='2'>Internacionales<BR>
    <INPUT TYPE=RADIO NAME='3'>Deportes<BR>
    </P>
    <INPUT TYPE=HIDDEN NAME="id" VALUE="1">
    <P>
    <INPUT TYPE=SUBMIT NAME="submit" VALUE="Enviar">
    </P>
    </FORM>


    </BODY>
    </HTML>

  13. #13
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    First problem.

    Your radio buttons should all have the same name.

    I changed the html and the radio buttons worked.

    Here's my html...

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML><HEAD><TITLE>Modificar Noticia</TITLE>
    <META http-equiv=Content-Type content="text/html; charset=windows-1252">
    <META content="MSHTML 5.50.4522.1800" name=GENERATOR></HEAD>
    <BODY>
    <FORM action=/phpfiles/editnews.php method=post>
    <P>Modificar la noticiaBR><TEXTAREA name=text rows=15 cols=45>Lo vieron. El Chupacabras ataca de nuevo en Azuero.</TEXTAREA>
    <BR>Título: <INPUT size=40 value=Chupacabras name=title> <BR>Autor: <SELECT
    size=1 name=aid> <OPTION value=1 selected>Esteban Giron</OPTION></SELECT>
    <P>En categoríaBR><INPUT type=radio name=1>Nacionales<BR><INPUT type=radio
    name=1>Internacionales<BR><INPUT type=radio name=1>Deportes<BR></P><INPUT
    type=hidden value=1 name=id>
    <P><INPUT type=submit value=Enviar name=submit> </P></FORM></BODY></HTML>
    Last edited by rgremill; Jul 5, 2001 at 12:22.

  14. #14
    SitePoint Enthusiast Yonko's Avatar
    Join Date
    Jun 2001
    Location
    Panama
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    So you changed the HTML...

    Wasn't I supposed to change the editnews.php file instead? I think I didn't understand.

  15. #15
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Yes

    I am showing you what is wrong with the html generated from your code. The radio button names are 1,2,3, when they should have the same name.

    You must fix your code so that all of the radio buttons created have the same name.

  16. #16
    SitePoint Enthusiast Yonko's Avatar
    Join Date
    Jun 2001
    Location
    Panama
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Oh, now I see...

    Thanks, let me see if I can fix it.

  17. #17
    SitePoint Enthusiast Yonko's Avatar
    Join Date
    Jun 2001
    Location
    Panama
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    But the id of the radio button...

    is what tells which is the news category. Is there any other way of returning the name of the category without using the id of the categories table?

  18. #18
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Value attribute

    That's where the value attribute comes in.

    Set value="$cid"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •