Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

radiobutton onClick

  • 11-09-2008 2:07pm
    #1
    Registered Users, Registered Users 2 Posts: 8,070 ✭✭✭


    <html>
    <script type="text/javascript">
    
    function updater()
    {
     <?php 
     echo "sss";
     //$question = this.name;
     //echo $question;?>
    }
    </script>
    
    <form>
    
    
    <input type="radio" name="q1" value="yes" onClick="updater()">
    <input type="radio" name="q1" value="no" onClick="updater()">
    
    </form>
    
    
    
    </html>
    

    Technically seems fine but wont work, i cannot have it submitting,
    basically its a quiz and i store their choices, as they click so they can leave the quiz and come back and complete it any other time.


Comments

  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Technically not fine! - PHP mixed with Javascript :eek:

    Then again this could be server side? but if thats the case you'll have javascript error having just sss inside the JS update function.

    You should be storing your answers client side in a javascript variable or using AJAX to send to server side for the users session.

    I don't see the PHP closing tag either

    We'll need more code than that or more explanation of whats going on.


  • Registered Users, Registered Users 2 Posts: 8,070 ✭✭✭Placebo


    sorry closing tag was there, i just cleaned it up a bit so must have missed it.
    Whats wrong with php inside javascript? it is server side.

    basically just need to know if onclick(); on a radio button can actually trigger events and if so why wont my code work.

    Im using php as i need to store their choices/preference on the database.


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    Yes, onclick works for the radio button.

    See the HTML below for a basic example

    [html]
    <html>
    <script type="text/javascript">

    function updater()
    {
    alert(document.frm1.q1[0].checked);
    }
    </script>

    <form name="frm1">


    yes
    <input type="radio" name="q1" value="yes" onClick="updater()">
    <br/>
    no
    <input type="radio" name="q1" value="no" onClick="updater()">

    </form>



    </html>
    [/html]


  • Registered Users, Registered Users 2 Posts: 8,070 ✭✭✭Placebo


    <html>
    
    <?php
    function create()
    {
    
    echo("true");
    }
    ?>
    
    <script type="text/javascript">
    
    function updater()
    {
     alert(document.frm1.q1[0].checked);
    
     
    
    document.write("<?php create() ?>");
    
    }
    </script>
    
    
    
    
    <form name="frm1">
    
    
    yes
    <input type="radio" name="q1" value="yes" onClick="updater()">
    <br/>
    no
    <input type="radio" name="q1" value="no" onClick="updater()">
    
    </form>
    
    
    
    </html>
    

    so would this be bad practice? only way i can get it working


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    I don't know PHP, but does that script not just print out "true"?

    Can you put down step-by-step what it is you are trying to do?

    e.g.

    1) User clicks a radio button
    2) ????
    3) Profit


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 8,070 ✭✭✭Placebo


    basically you cant have php inside javascript, unless you use document.write etc, you cant pass variables between each other, without having to have the page refresh, so AJAX works well, gonna post code here for future reference.

    What this code does.
    -Forum with yes or no [no submit button]
    -On clicking either, it passes the value to javascript whicht then passes that value via httprequest to your desired php page
    -so the php page would have $_GET; , and you can inject that into the db, thus saving users preference

    [PHP]<html>
    <body>

    <script language="javascript" type="text/javascript">
    <!--
    //Browser Support Code
    function ajaxFunction(){
    var ajaxRequest; // The variable that makes Ajax possible!


    try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
    } catch (e){
    // Internet Explorer Browsers
    try{
    ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try{
    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e){
    // Something went wrong
    alert("Your browser broke!");
    return false;
    }
    }
    }


    for (var i=0; i < document.frm1.q1.length; i++)
    {
    if (document.frm1.q1.checked)
    {
    var q1 = document.frm1.q1.value;

    }
    }
    //GOLD DUST
    var queryString = "?q1=" + q1;
    ajaxRequest.open("GET", "check.php" + queryString, true);
    ajaxRequest.send(null);

    }
    </script>




    <form name=frm1>

    <table>

    <tr><td><font face="arial" size="2">Do you have a dishwasher?</font></td>
    <td><font face="arial" size="2">Yes: <input type="radio" name="q1" value="YES" onClick="ajaxFunction();">
    <br />No: <input type="radio" name="q1" value="NO" onClick="ajaxFunction();">
    </font></td></tr>

    </table>

    </form>


    </body>
    </html>

    [/PHP]


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Placebo wrote: »
    basically you cant have php inside javascript, unless you use document.write etc, you cant pass variables between each other, without having to have the page refresh, so AJAX works well, gonna post code here for future reference.

    What this code does.
    -Forum with yes or no [no submit button]
    -On clicking either, it passes the value to javascript whicht then passes that value via httprequest to your desired php page
    -so the php page would have $_GET; , and you can inject that into the db, thus saving users preference

    [PHP]<html>
    <body>

    <script language="javascript" type="text/javascript">
    <!--
    //Browser Support Code
    function ajaxFunction(){
    var ajaxRequest; // The variable that makes Ajax possible!


    try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
    } catch (e){
    // Internet Explorer Browsers
    try{
    ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try{
    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e){
    // Something went wrong
    alert("Your browser broke!");
    return false;
    }
    }
    }


    for (var i=0; i < document.frm1.q1.length; i++)
    {
    if (document.frm1.q1.checked)
    {
    var q1 = document.frm1.q1.value;

    }
    }
    //GOLD DUST
    var queryString = "?q1=" + q1;
    ajaxRequest.open("GET", "check.php" + queryString, true);
    ajaxRequest.send(null);

    }
    </script>




    <form name=frm1>

    <table>

    <tr><td><font face="arial" size="2">Do you have a dishwasher?</font></td>
    <td><font face="arial" size="2">Yes: <input type="radio" name="q1" value="YES" onClick="ajaxFunction();">
    <br />No: <input type="radio" name="q1" value="NO" onClick="ajaxFunction();">
    </font></td></tr>

    </table>

    </form>


    </body>
    </html>

    [/PHP]
    Now thats more like it. You can keep track of whats clicked by storing the values in a session

    [php]
    session_start();

    $_SESSION = 'yes';

    [/php]

    By the way the document.write is client side so if you were trying to echo PHP in the document.write you will basically place PHP code on the client side that would not get interpreted by PHP.


Advertisement