How To Submit A Form Using AJAX - php

I have a quiz website. I want to redesign my question form to submit the answer given by the user through AJAX, verify the answer on the server and display the result along with the next answer to the user.
Please guide me how to do this. The codes I am already using are:
<?php
$a = $_REQUEST['ad'];
include("connection.php");
if (isset($_REQUEST['ad']))
{
if ($_REQUEST['ad'] == $a)
{
$q1 = "select * from question WHERE q_id= '$a' AND cat_id='General Knowledge'";
$rw = mysql_query($q1);
if ($row = mysql_fetch_assoc($rw))
{
if ($a % 10 == 0) {
$qno = 10;
} else {
$qno = substr($a, -1, 1);
}
?>
<b><?php echo "Q" . $qno . ". ";
echo $row['q_desc']; ?></b><br/><br/>
<div class="quizimage">
<img src="images/<?php echo $a; ?>.jpg" alt="General Knowledge Quiz"/>
</div>
<font class="common">
<table align="center">
<form action="general-knowledge.php?ad=<?php echo $a; ?>" method="post">
<tr align="center">
<input type="radio" name="ans"
value="<?php echo $row['ans1']; ?>" <?php echo($_POST['ans'] == $row['ans1'] ? 'checked' : '') ?>/>
<?php echo $row['ans1']; ?>
<br/>
<input type="radio" name="ans"
value="<?php echo $row['ans2']; ?>" <?php echo($_POST['ans'] == $row['ans2'] ? 'checked' : '') ?>/>
<?php echo $row['ans2']; ?><br/>
<input type="radio" name="ans"
value="<?php echo $row['ans3']; ?>" <?php echo($_POST['ans'] == $row['ans3'] ? 'checked' : '') ?>/>
<?php echo $row['ans3']; ?><br/>
<input type="radio" name="ans"
value="<?php echo $row['ans4']; ?>" <?php echo($_POST['ans'] == $row['ans4'] ? 'checked' : '') ?>/>
<?php echo $row['ans4']; ?><br/>
</font>
<tr>
<td><input type=submit name=sub value=Submit_Answer></td>
</tr></form></table>
<table border="1" align="center">
<div class="adunit3">
<?php
include "adunit3.php";
?>
</div>
<?php
}
$_SESSION['quiz_visited'] = $a;
if (isset($_POST['sub'])) {
$a_value = $a;
$answer = $_POST['ans'];
$q2 = "select * from question where q_id=$a_value";
$r2 = mysql_query($q2);
if ($row = mysql_fetch_array($r2))
$trueans = $row['true_ans'];
if ($answer == $trueans) {
$score = $_SESSION['score'];
$score = ++$score;
$_SESSION['score'] = $score;
?>
<div class="resultdisplay">
Your answer is correct. <h3>General Knowledge Trivia</h3><?php echo $row['trivia']; ?> <br/> <?php
if ($a % 10 == 0) {
$a = ++$a;
?>
<b>Click Here to view your result.</b>
<?php
} else {
$a = ++$a;
?>
<b>Click Here for next question.</b>
<?php
}
?>
</div>
<?php
} else {
?>
<div class="resultdisplay">
Your answer is wrong. The correct answer is <i>'<?php echo $trueans; ?>'</i>.
<h3>General Knowledge Trivia</h3><?php echo $row['trivia']; ?> <br/>
<?php $a = ++$a; ?>
<b>Click Here for next question.</b>
</div>
<?php
}
}
++$a;
$a = ++$a;
}
}
?>
</table>

You can do that with following structure;
First put $ad variable in hidden element in your form;
<input type="hidden" name="ad" value="<?php echo $a?>"/>
And then
$.ajax({
type: "POST",
url: 'general-knowledge.php',
data: $(".common form").serialize(),
success: function(data) {
if (data.result == true) {
alert("It is correct");
window.location = "next_question.html"
} else {
alert("Incorrrect result");
}
}
});
Check form results by using form variables and question id, and return result on server side

Use this javascript (Jquery code to submit you form).
// frm_id is the id of the form
$("#frm_id").submit(function() {
var url = "path/to/your/script.php"; // the script where you handle the form input and save to database.
$.ajax({
type: "POST",
url: url,
data: $("#frm_id").serialize(), //serializes the form's elements.
success: function(data){
alert(data); // show response from the php script.
}
});
return false; // prevent to execute the actual submit of the form.
});
In response (data) you can bring details of next question.

try something like this
$("#form_id").submit(function() {
$.ajax({
type: "POST",
url: this.action,
data: $(this).serialize(), //Serialize a form to a query string.
success: function(response){
alert(response); //response from server.
}
});
return false; // prevent form to submit.
});
Reference
jQuery Ajax() => http://api.jquery.com/jquery.ajax/
Serialize() => http://api.jquery.com/serialize/
jQuery Submit() => http://api.jquery.com/submit/
jQuery post => http://api.jquery.com/jquery.post/
Example
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
Submitting HTML form using Jquery AJAX
http://hayageek.com/jquery-ajax-form-submit/
http://www.phpeveryday.com/articles/jQuery-AJAX-Form-Submission-P973.html

1- Set form id = general-knowledge-form
2- load jquery lib on your head tag as following
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
3- create new hidden input <input type="hidden" id="q_id" value="<?php echo $a; ?>" />
4- create new php file for ex: ajax.php to verify user answer then return the appropriate html outbut this file contain the following code:
<?php
mysql_connect('localhost', 'root', 'root');
mysql_select_db('test');
$a = filter_var($_POST['q_id'], FILTER_SANITIZE_NUMBER_INT);
$ans = filter_var($_POST['ans'], FILTER_SANITIZE_STRING);
$q1 = "select * from question WHERE q_id=" . $a;
$rw = mysql_query($q1);
$row = mysql_fetch_object($rw);
?>
<?php if ($ans == $row->true_ans): ?>
<div class="resultdisplay">
Your answer is correct. <h3>General Knowledge Trivia</h3><?php echo $row->trivia; ?> <br/> <?php
if ($a % 10 == 0) {
$a = ++$a;
?>
<b>Click Here to view your result.</b>
<?php
} else {
$a = ++$a;
?>
<b>Click Here for next question.</b>
<?php
}
?>
</div>
<?php else: ?>
<div class="resultdisplay">
Your answer is wrong. The correct answer is <i>'<?php echo $row->true_ans; ?>'</i>.
<h3>General Knowledge Trivia</h3><?php echo $row->trivia; ?> <br/>
<?php $a = ++$a; ?>
<b>Click Here for next question.</b>
</div>
<?php endif;
5- add the following script on end of your page before body tag to handle your ajax request to ajax.php, then append appropriate html to your page
<script>
$(document).ready(function() {
$("#general-knowledge-form").submit(function(event) {
var ans = $('input[name=ans]:checked').val();
if (ans !== undefined) {
var q_id = $("#q_id").val();
$.ajax({
type: "POST",
url: "ajax.php",
data: {q_id: q_id, ans: ans}
})
.done(function(html) {
$("#resultdisplay").empty();
$("#resultdisplay").append(html);
});
} else {
alert('Plz select your answer.');
}
event.preventDefault();
});
});
</script>

Related

To fetch value of particular textbox

Here, I am using session to store multiple textbox value.
But when I am going to fetch from session, I am getting the same value for all textbox which is in session.
My code:
if ($order_list) {
$i = $start +1;
foreach ($order_list as $row)
{
?>
<input type="text" name="<?php echo $row['id']; ?>" class="txt" autocomplete="off" id="txtid_<?php echo $row['id']; ?>" value="<?php if(isset($_SESSION['txtval'])) { echo $_SESSION['txtval'];} ?>">
<?php } ?>
In javascript:
$(document).on('blur','.txt',function(){
var myVar = $(this).val();
//alert(myVar);
$.ajax({
type: "GET",
url: "view_orders_checked_array.php",
data: {account: myVar, task: 'alltxt'},
async: false
});
});
In view_orders_checked_array.php :
$task = $_GET['task'];
if($task == "alltxt")
{
$_SESSION['txtval'] = $account;
}
Here, I am not getting the value of particular textbox. I am getting the value which I have last inserted.
Where I am going wrong?
Check below working code, if you just pass the id with your txtval and create session for each id key and value . Now when you print session array you will get all key values in session array. Please ask if difficult to understand.
Javascript
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<?php
session_start();
$_SESSION['txtval'] = '';
$order_list[0] = array('id'=>'1');
$order_list[1] = array('id'=>'2');
$order_list[2] = array('id'=>'3');
$order_list[3] = array('id'=>'4');
$start = '';
if ($order_list) {
$i = $start + 1;
foreach ($order_list as $row) {
?>
<input type="text" name="<?php echo $row['id']; ?>" class="txt" autocomplete="off"
id="txtid_<?php echo $row['id']; ?>" value="<?php if (isset($_SESSION['txtval'])) {
echo $_SESSION['txtval'];
} ?>">
<?php }
}?>
<script type="text/javascript">
$(document).on('blur','.txt',function(){
var myVar = $(this).val();
var myVarid = this.id;
$.ajax({
type: "GET",
url: "view_orders_checked_array.php",
data: {account: myVar, task: 'alltxt', id: myVarid },
async: false,
success:function(data){
console.log(data);
}
});
});
</script>
PHP file view_orders_checked_array.php
<?php
session_start();
$task = $_GET['task'];
if ($task == "alltxt") {
$_SESSION['txtval'][$_REQUEST['id']] = $_REQUEST['account'];
}
echo '<pre>';print_r($_SESSION['txtval'] );echo '</pre>';
die('Call');
you have to maintain array in session also so that you can do with the help of ids
var id=your loop id;
data: {account: myVar, task: 'alltxt',id:id },
and in your view_orders_checked_array page
$task = $_GET['task'];
$id=$_GET['id'];
if($task == "alltxt")
{
$_SESSION['txtval'][$id] = $account;
}
and in your code
<input type="text" name="<?php echo $row['id']; ?>" class="txt" autocomplete="off" id="txtid_<?php echo $row['id']; ?>" value="<?php if(isset($_SESSION['txtval'])) { echo $_SESSION['txtval'][$row['id']];} ?>">
i suggest you to use POST method for passing values
Problem is that you are putting the same value in all text fields
$_SESSION['txtval']
in your loop is always the same.
Edit
And also I think you getting same last inserted value because instead to store all text fields in array $_SESSION['txtval']['another_id_key'] you storing it in $_SESSION['txtval'] which is only one value

Display Table based on dropdownlist selection in PHP

My table is not displayed when i select any project name. I guess the onchange function is not working properly but i couldn't figure out the problem.
Code is as follows:
<div class="span6">
<?php $sql = "SELECT * from login_projects WHERE login_id='".$record['login_id']."'";
$res_sql = mysql_query($sql); ?>
<label>Project Name <span class="f_req">*</span></label>
<!--<input type="text" name="city" class="span8" />-->
<select name="project_name" onchange="get_list_onnet()" id="project_name" class="span8">
<option value="">--</option>
<?php while($rec_sql = mysql_fetch_array($res_sql)){ ?>
<option value="<?php echo $rec_sql['project_id']; ?>">
<?php echo $rec_sql['project_name']; ?></option>
<?php } ?>
</select>
</div>
Function:
<script>
function get_list_onnet(){
var project_name=$("#project_name").val();
$.ajax
({
type: "POST",
url: "ajax.php",
data: {action: 'get_list_onnet',list_onnet:project_name},
success: function()
{
document.getElementById("dt_a").style="block";
$("#dt_a").html(html);
}
});
};
</script>
<script>
$(document).ready(function() {
//* show all elements & remove preloader
setTimeout('$("html").removeClass("js")',1000);
});
</script>
Ajax.Php Page:
function get_list_onnet(){
$list_onnet=$_POST['list_onnet'];
$sql_list_onnet=mysql_query("SELECT * from projects,project_wise_on_net_codes
where projects.project_id = project_wise_on_net_codes.project_id AND
project_wise_on_net_codes.project_id='$list_onnet'");
$row1 = mysql_num_rows($sql_list_onnet);
if($row1>0)
{
echo "<tr><th>id</th><th>Project Name</th><th>Country Code</th><th>On-net prefix</th>
<th>Action</th></tr>";
$k = 1; while($row_list_onnet=mysql_fetch_array($sql_list_onnet))
{
$project3 = $row_list_onnet['project_name'];
$countrycode1 = $row_list_onnet['country_code'];
$prefix1 = $row_list_onnet['on_net_prefix'];
$id_proj = $row_list_onnet['project_id'];
$on_prefix = $row_list_onnet['on_net_prefix'];
echo "<tr><td>".$k."</td><td>".$project3."</td><td>".$countrycode1."</td>
<td>".$prefix1."</td><td><a href='process/update_process_onnet.php?ID=".$id_proj."&Onnet=".$on_prefix."'>Delete</a></td>
</tr>";
$k++;
}
}
else
{
echo "<script>alert('No Record Found')</script>";
}
}
The problem is that it is always going in the else condition and nothing is displayed in the table.

How to delete a database record by unchecking a checkbox

<form action="" method="post">
<?php
$sql = "select * from tb_transport";
$query = mysql_query($sql);
$a=1;
while($row=mysql_fetch_array($query))
{
$sql_2="select * from transport_two where transport_id='$row[name]'";
$query_2=mysql_query($sql_2);
$row_2=mysql_fetch_array($query_2);
?>
<input type="checkbox" name="courses[<?php echo $a; ?>][crs]" value="<?php echo $row["name"]; ?>" <?php if($row_2["transport_id"]=="$row[name]") { ?> checked="checked" <?php } ?> />< ?php echo $row["name"]; ?>
<?php
$a=$a+1;
}
?>
<input type="submit" name="save" />
</form>
Here is my form and I'm having a number of checkboxes dependent on my database records.
Here, also I have applied code that if a checkbox value exists in database then it is shown checked.
If I have checked a checkbox, then a row gets inserted with the checkbox value.
Now what I am looking for is that if I uncheck the checked checbox then that database row gets deleted.
How can I do this?
Here is my insertion code:
<?php
if(isset($_POST["save"])) {
$arr = $_POST["courses"];
for ($i = 0; $i < 20; $i++) {
if (!empty($arr[$i]['crs'])) {
$a = $arr[$i]['crs'];
mysql_query("insert into transport_two set transport_id='$a'");
}
}
}
?>
If you want to delete from a database at the moment when a checkbox is marked/unmarked, you need to use JavaScript or jQuery.
If you want to delete from a database at the moment a user clicks on the submit button, you have to send the value to your php - script which updates/deletes from a database.
just give a simple class to checkbox.
<form action="" method="post">
<?php
$sql="select * from tb_transport";
$query=mysql_query($sql);
$a=1;
while($row=mysql_fetch_array($query))
{
$sql_2="select * from transport_two where transport_id='$row[name]'";
$query_2=mysql_query($sql_2);
$row_2=mysql_fetch_array($query_2);
?>
<input type="checkbox" class="deleteFromDb" name="courses[<?php echo $a; ?>][crs]" value="<?php echo $row["name"]; ?>" <?php if($row_2["transport_id"]=="$row[name]") { ?> checked="checked" <?php } ?> /><?php echo $row["name"]; ?>
<?php
$a=$a+1;
}
?>
<input type="submit" name="save" />
</form>
Here click on any check box and jquery check it is check or uncheck. if it uncheck then ajax send request to delete this record from db.
$(document).ready(function(){
$(".deleteFromDb").change(function(e){
if ($(this).prop('checked')==false){
$.ajax({type: "POST",
url: "deleterecord.php",
data: { id:$(this).val().trim() },
success:function(result){
var response = JSON.parse(result);
if(response.error){
//here provide a error msg to user.
alert(response.error);
}
if(response.success){
alert("record deleted");
}
}});
});
}
});
Delete.php
Here perform your delete action.
$id = $_POST['id'];
$strSQL = mysql_query("DELETE FROM `table` WHERE id = ".$id);
if($strSQL){
$result['success'] = "success";
}
else
{
$result['error'] = "try again";
}
echo json_encode($result); die;
Now when you click any checkbox and if it ubcheck, the record will delete from db.

Php Ajax form submit in colorbox

I have a form with some php to validate and insert in the database on submit and the form opens in colorbox.
So far so good. What I'm trying to do is to close colorbox and refresh a div on success.
I guess I need to pass a response to ajax from php if everything OK, close the colorbox with something like setTimeout($.fn.colorbox.close,1000); and refresh the div, but I'm stuck because I'm new in ajax.
I'll appreciate any help here.
Here is my ajax:
jQuery(function(){
jQuery('.cbox-form').colorbox({maxWidth: '75%', onComplete: function(){
cbox_submit();
}});
});
function cbox_submit()
{
jQuery("#pre-process").submit(function(){
jQuery.post(
jQuery(this).attr('action'),
jQuery(this).serialize(),
function(data){
jQuery().colorbox({html: data, onComplete: function(){
cbox_submit();
}});
}
);
return false;
});
}
form php code:
<?php
error_reporting(-1);
include "conf/config.php";
if(isset($_REQUEST['rid'])){$rid=safe($_REQUEST['rid']);}
if(isset($_REQUEST['pid'])){$pid=safe($_REQUEST['pid']);}
$msg = '';
if (!$_SESSION['rest_id']) $_SESSION['rest_id']=$rid; //change to redirect
$session_id=session_id();
if(isset($_REQUEST['submit'])){
if(isset($_POST['opta'])){
$opta=safe($_POST['opta']);
$extraso = implode(',',array_values( array_filter($_POST['opta']) ));
}
if (array_search("", $_POST['opt']) !== false)
{
$msg = "Please select all accessories!";
}else{
$extrasm = implode(',',array_values( array_filter($_POST['opt']) ));
if ($_POST['opt'] && isset($_POST['opta'])) {$extras= $extrasm .",". $extraso;}
if ($_POST['opt'] && !isset($_POST['opta'])) {$extras= $extrasm;}
if (!$_POST['opt'] && isset($_POST['opta'])) {$extras= $extraso;}
$sql['session_id'] = $session_id;
$sql['rest_id'] = $_POST['rid'];
$sql['prod_id'] = $_POST['pid'];
$sql['extras'] = $extras;
$sql['added_date'] = Date("Y-m-d H:i:s");
$newId=insert_sql("cart",$sql);
}
}
?>
<form id="pre-process" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div style="background-color:#FFF; padding:20px;">
<?=$msg;?>
<?php
$name = getSqlField("SELECT name FROM products WHERE resid=".$_SESSION['rest_id']." and id=".$pid."","name");
echo "<div style='color:#fff; background-color:#F00;padding:10px;' align='center'><h2>".$name."</h2></div><div style='background-color:#FFF; padding: 20px 70px 30px 70px; '>Please select accessories.<br><br>";
$getRss = mysql_query("SELECT * FROM optional_groups_product where prodid=".$pid." order by id asc");
while ($rsrw = #mysql_fetch_array($getRss)) {
$goptionals = getSqlField("SELECT goptionals FROM optionals_groups WHERE resid=".$_SESSION['rest_id']." and id=".$rsrw['goptid']."","goptionals");
$goptionals=explode(', ',($goptionals));
echo "<select name='opt[]' id='opt[]' style='width:220px;'>";
echo "<option value='' >Select Options</option>";
foreach($goptionals as $v)
{
$vname = mysql_query("SELECT * FROM optionals where id=".$v." LIMIT 0,1");
while ($rsgb = #mysql_fetch_array($vname)) {
$aa=$rsgb['optional'];
}
echo "<option value=".$v." >".$aa."</option>";
}
echo "</select>(required)<br>";
//}
}
$getRss = mysql_query("SELECT * FROM optional_product where prodid=".$pid."");
?>
<br><br>
<table border="0" cellpadding="0" cellspacing="0" >
<tr>
<td bgcolor="#EAFFEC">
<div style="width:440px; ">
<?php
while ($rssp = #mysql_fetch_array($getRss)) {
$optional=getSqlField("SELECT optional FROM optionals WHERE id=".$rssp['optid']."","optional");
$price=getSqlField("SELECT price FROM optionals WHERE id=".$rssp['optid']."","price");
?>
<div style="width:180px;background-color:#EAFFEC; float:left;padding:10px;""><input type="checkbox" name="opta[]" id="opta[]" value="<?=$rssp['optid']?>" /> <i><?=$optional?> [<?=CURRENCY?><?=$price?> ]</i> </div>
<?php } ?>
</div>
</td>
</tr></table>
<input type="hidden" name="rid" value="<?=$rid?>" />
<input type="hidden" name="pid" value="<?=$pid?>"/>
</div><input type="hidden" name="submit" /><input id='submit' class="CSSButton" style="width:120px; float:right;" name='submit' type='submit' value=' Continue ' /><br />
<br /><br />
</div>
</form>
I don't know colobox, but if I understand well what you are trying to do,
I would say your javascript should more look like this
function cbox_submit()
{
jQuery("#pre-process").submit(function(e) {
e.preventDefault(); // prevents the form to reload the page
jQuery.post(
jQuery(this).attr('action')
, jQuery(this).serialize()
, function(data) {
if (data['ok']) { // ok variable received in json
jQuery('#my_colorbox').colorbox.close(); // close the box
}
}
);
return false;
});
}
jQuery(function() {
jQuery('#my_colorbox').colorbox({
maxWidth: '75%'
, onComplete: cbox_submit // Bind the submit event when colorbox is loaded
});
});
You should separate at least your php script that does the post part.
And this php (called with jQuery(this).attr('action')) should return a json ok variable if successfull. Example:
<?php
# ... post part ...
# if success
ob_clean();
header('Content-type: application/json');
echo json_encode(array('ok' => true));
?>

How to make this J Query/Ajax script work with more than one row record?

I have a table with records from a database which is dynamically filled.
<table>
<tr>
<td>name
</td>
<td>surname
</td>
...
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_Recordset1['Name'];?>
</td>
<td><?php echo $row_Recordset1['Surname'];?>
</td>
...
<td align="center">
<form name="form" action="novi.php">
<input name="check" type="radio" value="da">Да
<input name="check" type="radio" value="ne">Не
<input type="hidden" id="id" value="<?php echo $row_Recordset1['id_korisnici'];?>">
<input class="button1"type="button" id="klik" value="Испрати"/>
</form>
</td>
</tr>
<?php } while($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
And than I'm using J Query to create an AJAX call to another page which will run the query.
<script>
$('#klik').click(function() {
var check = $("input[name='check']:checked").val();
var id = $('#id').val();
if (check == "da")
{
$.post('sluzbaIncludes/n.php', {check:check, id:id}, function(data) {
$('#klik').prop('value', (data));
document.location.reload();
});
}
else if (check == "ne")
{
$.post('sluzbaIncludes/n.php', {check:check, id:id}, function(data) {
$('#klik').prop('value', (data));
document.location.reload();
});
}
else
{
$('#klik').prop('value', 'Грешка');
setTimeout("$('#klik').prop('value', '.')",500);
setTimeout("$('#klik').prop('value', '..')",1000);
setTimeout("$('#klik').prop('value', '...')",1500);
setTimeout("$('#klik').prop('value', 'Испрати')",2000);
}
});
This is working OK if there is only one row in the table.
What I don't know how to do is, to make the script store all the generated CHECKED radio buttons in an array or something, plus all the ID's of the records from the database and send them to another page where according to their ID's a query will update the database..
<?php
if ($_POST['id'] != NULL) {
if ($_POST['check'] == "da") {
$id = mysql_real_escape_string($_POST['id']);
$update = mysql_query("update korisnici set validacija = 1 where id_korisnici= '$id'");
if ($update === true) {
echo 'OK';
}else if ($update === false){
echo 'Error!!!';
}
}
elseif ($_POST['check'] == "ne")
{
$id = mysql_real_escape_string($_POST['id']);
$update = mysql_query("update korisnici set validacija = 2 where id_korisnici= '$id'");
if ($update === true) {
echo 'OK';
}
else if ($update === false){
echo 'Error!!!';
}
}
} else {
echo 'Error!!!';
}
?>
Thanks!
P.S. I'm a noob in JQuery and a beginner in PHP...
UPDATE:
I did change some things. And now the values are shown as I want, when I click the button without a selection I'm getting the error message in the ELSE part of the JQuery code in the proper button. But no matter which button I click (if the table is populated with 3 records from the database, there are 3 buttons) only the first row from the table is updated in the database.
<form name="form" action="novi.php">
<input name="check<?php echo $row_Recordset1['id_korisnici'];?>" type="radio" value="da">Да
<input name="check<?php echo $row_Recordset1['id_korisnici'];?>" type="radio" value="ne">Не
<input type="hidden" id="id" value="<?php echo $row_Recordset1['id_korisnici'];?>">
<input class="button1"type="button" id="klik<?php echo $row_Recordset1['id_korisnici'];?>" value="Испрати"/>
</form>
The JQuery:
$('#klik<?php echo $row_Recordset1['id_korisnici'];?>').click(function() {
var check = $("input[name='check<?php echo $row_Recordset1['id_korisnici'];?>']:checked").val();
var id = $('#id').val();
if (check == "da")
{
$.post('sluzbaIncludes/n.php', {check:check, id:id}, function(data) {
$('#klik<?php echo $row_Recordset1['id_korisnici'];?>').prop('value', (data));
document.location.reload();
});
}
else if (check == "ne")
{
$.post('sluzbaIncludes/n.php', {check:check, id:id}, function(data) {
$('#klik<?php echo $row_Recordset1['id_korisnici'];?>').prop('value', (data));
document.location.reload();
});
}
else
{
$('#klik<?php echo $row_Recordset1['id_korisnici'];?>').prop('value', 'Error');
setTimeout("$('#klik<?php echo $row_Recordset1['id_korisnici'];?>').prop('value', '.')",500);
setTimeout("$('#klik<?php echo $row_Recordset1['id_korisnici'];?>').prop('value', '..')",1000);
setTimeout("$('#klik<?php echo $row_Recordset1['id_korisnici'];?>').prop('value', '...')",1500);
setTimeout("$('#klik<?php echo $row_Recordset1['id_korisnici'];?>').prop('value', 'Send')",2000);
}
});
Make each group of radio buttons have a unique name but the same class. Then you can iterate through them using jQuery:
var radioVals = new Array();
var i = 0;
$(".radioButtonClass:checked").each(function() {
radioVals[i] = $(this).val();
});
Then just pass the array in your ajax call.

Categories