admin管理员组

文章数量:1024582

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="pany_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 pany_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 pany_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_pany where pany_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>

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="pany_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 pany_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 pany_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_pany where pany_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>
Share Improve this question asked Jan 13, 2014 at 11:04 user3189828user3189828 1851 gold badge5 silver badges16 bronze badges 1
  • 2 wrap your ajax call inside a function just like any other code – A. Wolff Commented Jan 13, 2014 at 11:08
Add a ment  | 

2 Answers 2

Reset to default 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);
            }
        });
    });
});

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>

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="pany_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 pany_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 pany_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_pany where pany_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>

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="pany_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 pany_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 pany_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_pany where pany_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>
Share Improve this question asked Jan 13, 2014 at 11:04 user3189828user3189828 1851 gold badge5 silver badges16 bronze badges 1
  • 2 wrap your ajax call inside a function just like any other code – A. Wolff Commented Jan 13, 2014 at 11:08
Add a ment  | 

2 Answers 2

Reset to default 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);
            }
        });
    });
});

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>

本文标签: javascripthow to use the same ajax function for multiple buttonsStack Overflow