PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 8, 2022

[FIXED] How to make radio button in ajax - php?

 September 08, 2022     ajax, forms, php, radio-button     No comments   

Issue

What's wrong with the code of radio button? When I've tried SELECT/OPTION it's working.

I'm trying to make two groups of radio button, get their values and sum up. but I can't move through since the radio buttons don't make response at all.

These are inputs to my form:

<input type='radio' id='1' name='r' onclick='pieces('this.value')' 
       autocomplete='off' value='yes'>Yes
<input type='radio' id='2' name='r' onclick='pieces('this.value')' 
       autocomplete='off' value='no'>No

My script:

<script type='text/javascript'>

    function pieces(str)
    { 

        if (str=='')
        {
            document.getElementById(abc).innerHTML='';
            return;
        }

        if (window.XMLHttpRequest)
        {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            // code for IE6, IE5
            xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
        }

        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById(abc).innerHTML=xmlhttp.responseText;
            }
        }

        xmlhttp.open('GET','price.php?q='+str,true);
        xmlhttp.send();

    }
</script>

Solution

Here is the modified script. Inputs:

<input type='radio' id='1' name='r' onclick="pieces(this.value);" 
       autocomplete='off' value='yes'>Yes
<input type='radio' id='2' name='r' onclick="pieces(this.value);" 
       autocomplete='off' value='no'>No

Here is the js script:

 function pieces(str)
    { 

        if (str=='')
          {
          document.getElementById("abc").innerHTML='Choose something';
          return;
          }

        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
            else
              {// code for IE6, IE5
              xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
              }

        xmlhttp.onreadystatechange=function()
            {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                document.getElementById("abc").innerHTML=xmlhttp.responseText;
                }
            }

        xmlhttp.open('GET','hey.php?q='+str,true);
        xmlhttp.send();

    }

Let me know if any prob.

Jasminder



Answered By - Jasminder Pal Singh
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing