Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting a form using AJAX

Tags:

ajax

post

forms

do u have an idea of how to send my radio button name for

myAjaxPostrequest.send(parameters);

can parameters be like this:

var answername=document.getElementById('option1').name;

var parameters=answername;

? this code is for using ajax to post a form

and my php page needs the name of the radiobutton clicked

I tried this code and it works as I want except for inserting in the database. It means parameters is the problem

what I want to insert is the number located between brakects of the radiobutton name.

I could do it when I post form without AJAX but now I can't send the name of the radiobutton

any clue about what I can send as parameter for function myAjaxPostrequest.send(parameters); ?

<form id="Question1" method="post"> 
<br />
<P> The sky color is..
</P><img border="0" src="images/wonder.png" width="94" height="134"/><br /><br /><br />
<input type="radio" id="option1" name="answer[1]" value="correct!" onclick="submitFormWithAjax();"/> blue
<br />
<input type="radio" id="option1" name="answer[1]" value="false!" onclick="submitFormWithAjax();"/> red
<br />
<input type="radio" id="option1" name="answer[1]" value="false!" onclick="submitFormWithAjax();"/> green
<br />
<input type="radio" id="option1" name="answer[1]" value="false!" onclick="submitFormWithAjax();"/> white
</form>
like image 923
user356719 Avatar asked Nov 30 '25 04:11

user356719


1 Answers

By following way you can pass parameter to ajax call:

var url = "get_data.php";
var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.send(params);
like image 68
Pranay Rana Avatar answered Dec 07 '25 14:12

Pranay Rana