2

i want call the same ajax function for multiple buttons please help me the code is given below. this code will generate buttons and when we click on the buttons it prints the details ...please solve. This code will generate buttons in table.

<head>
<script>
$(document).ready(function()
{
$("#save").click(function()
{
var sel = $("#response").val();
var fudate=$("#fdate").val();
var cmd=$("#cm").val();
 var id=$("#id").val();
 $.ajax(
    {
        type: "POST",
        url: "autosave.php",
        data: {response:sel,fdate:fudate,cm:cmd,id:id},
       success: function(result)
        {
 alert('result');
          $a= $("#ak").html(result);
       }
    });
});
});
</script>
</head>
<body>
<div id="company_details">
<div id="ak"></div>
<table border="1" align="left">
<tr>
<th >Company</th>
<th>Contact Person</th>
<th>Contact Person's No.</th>
<th>HR</th>
<th>HR's No.</th>
<th>Current Vacancies</th>
<th>Response</th>
<th>Follow Up Date</th>
<th>Comment</th>
<th></th>
<th>Edit</th>
</tr>
<?php 
$con=mysql_connect("localhost","root") or die("Error In Connection:-".mysql_error());
mysql_select_db("ttcs",$con) or die("Error In DB Selection:-".mysql_error());
$sql="select * from company_details";
$rs=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($rs))
{
?>
<tr>
<td>
<input type="text" name="name" value= <?php $e=$row[0];echo $row[1];?>></td>                              
<td><input type="text" name="cp" value= <?php echo $row[7]; ?>></td>
<td><input type="text" name="cpn" value= <?php echo $row[9]; ?>></td>
<td><input type="text" name="hr" value= <?php echo $row[10]; ?>></td>
<td><input type="text" name="hrn" value= <?php echo $row[12]; ?>></td>
<td>
<?php 
$sql2="select * from vacancies where company_id='$e'";
$rs2=mysql_query($sql2);
if($rs2)
{
while($row2=mysql_fetch_array($rs2))
{?>
<textarea  name="cv" value=<?php echo $row2[1];?>></textarea>
<?php }}?>
</td>
<td>
<select name="response" id="response">
<option value="Not interested">Not Interested</option>
<option value="Not responding">Not Responding</option>                                  
<option value="follow up">Follow up</option>
<option value="Interested">Interested</option>
</select></td>
<?php
$sql1="select * from call_details_company where company_id=$row[0]";
$rs1=mysql_query($sql1);
$row1=mysql_fetch_array($rs1);
?>
<input type="date" name="fdate" id="fdate" value=<?php echo $row1[3];?>>              
<input type="text" name="cm" id="cm" value=<?php  echo $row1[5];?>>
<button  id="save" >save</button>
</td>
</tr>
<?php 
}
?>
</body>
1
  • 2
    wrap your ajax call inside a function just like any other code Commented Jan 13, 2014 at 11:08

2 Answers 2

3

ID of an element must be unique, so within the loop don't use ID, change the id of the button to class value then use the input element's name to find the relative elements within the tr

$(document).ready(function () {
    $(".save").click(function () {
        var $tr = $(this).closest('tr')
        var sel = $tr.find("select").val();
        var fudate = $tr.find('input[name="fdate"]').val();
        var cmd = $tr.find('input[name="cm"]').val();
        //need to fix this selector #id too, not able to locate it in the given html structure
        var id = $tr.find("#id").val();
        $.ajax({
            type: "POST",
            url: "autosave.php",
            data: {
                response: sel,
                fdate: fudate,
                cm: cmd,
                id: id
            },
            success: function (result) {
                alert('result');
                $a = $("#ak").html(result);
            }
        });
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

it works but but can't acess the values. it only gets the first value of the textbox. please help to find acess the particular values for particular row
@user3189828 which value is not found
the textbox values of above code for particular row. i got the first textbox value when clicking all other buttons.....
2

use like this

<script>
 function ajaxCallDemo() {
    var sel = $("#response").val();
    var $tr = $(this).closest('tr');
    var cmd = $tr.find("input[name='cm']").val();
    var fudate = $tr.find("input[name='fdate']").val();
    var id = $tr.find("#id").val();
    $.ajax({
        type: "POST",
        url: "autosave.php",
        data: {
            response: sel,
            fdate: fudate,
            cm: cmd,
            id: id
        },
        success: function(result) {
            alert('result');
            $a = $("#ak").html(result);
        }
    });
}
$(document).ready(function() {
    $("#save").click(function() {
        ajaxCallDemo();
    });
    $("#save2").click(function() {
        ajaxCallDemo();
    });
});
</script>

2 Comments

+1 but you could use the reference to function syntax: $("#save").click(ajaxCallDemo);
@A.Wolff still the selector var cmd=$("#cm").val() won't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.