简体   繁体   中英

How to pass row name from php to ajax using jquery

I have a table in which the details are fetched from the DB.

if(mysql_num_rows($sql) > 0) 
{
    $row_count_n=1;
    while($rows=mysql_fetch_assoc($sql))
    {
        extract($rows);
        $options1 = select_data_as_options("project_resources", "name", $resource_allocated);
        $options2 = select_data_as_options("project_roles", "name", $role);
        echo "<tr>";
        echo "<td><select name='ra_$row_count_n'><option value=''>-- Select --$options1</option></select></td>";
        echo "<td><select name='role_$row_count_n'><option value=''>-- Select --$options2</option></select></td>";
        echo "<td><input type='text' name='start_date_tentative_$row_count_n' class='date_one' value=$tentatively_starts_on /></td>";
        echo "</tr>";
        $row_count_n++;
    }
}

I wanted to update the table when required, am doing this using Ajax by collecting data from the form using Jquery and saving it on button click.

$("#save_changes_id").click(function() 
{
    //  To retrieve the current TAB and assign it to a variable ...
    var curTab = $('.ui-tabs-active'); // in NEWER jQueryUI, this is now ui-tabs-active
    var curTabPanelId = curTab.find("a").attr("href");

    if(curTabPanelId == "#tab_dia")
    {
        var curTab = $('#sub_tabs .ui-tabs-active');
        var curTabPanelId = curTab.find("a").attr("href");
    }
    responseData = doAjaxCall($(curTabPanelId + " form"));

    if(responseData == 1) 
        showMessage('status_msg', 'Project details updated successfully', 'green');
    else
        showMessage('status_msg', 'Error: Please check all the fields', 'red');

}); 

function doAjaxCall(objForm) 
{
    var values = objForm.serialize();
    $.ajax({
        url: ajaxURL,
        type: "post",
        data: values,
        async: false,
        success: function(data)
        {
            responseData = data;
        },
        error:function()
        {
            alert('Connection error. Please contact administrator. Thanks.');
        }
    });

    return responseData;
}

Ajax code is as below:

case "allocate_ba_details":

for($i=1; $i<=$row_count; $i++) 
{
    $resource = $_REQUEST["ra_$i"];
    $role = $_REQUEST["role_$i"];
    $start_date_tentative = $_REQUEST["start_date_tentative_$i"];
    $already_available_check = mysql_num_rows(mysql_query("select * from project_allocate_ba where project_id = $pdid"));

    if($already_available_check > 0) 
    {
        $sql = ("UPDATE project_allocate_ba SET resource_allocated='$resource', role='$role', tentatively_starts_on='$start_date_tentative' WHERE project_id=$pdid");   
    }
}

echo $sql;
break;

As I am new to this am not sure how to pass the row name in order to update a particular row. Please suggest a solution. Thanks in advance.

firstly use PDO or some php framework that has nice API to work with mysql. Second don't use success/error callback in jquery is too deprecated. Instanted use done/fail.always. I understand that you want update row in html table data from the server ? In success callback simply update the table using jquery text method for jquery object. You don't paste all code so i write example:

in server.php

<?php
[...]
$already_available_check = mysql_num_rows(mysql_query("select * from project_allocate_ba where project_id =" . intval($pdid)));
[...]
echo $already_available_check; 
?>

This code return the integer, so in doAjaxCall:

function doAjaxCall(objForm) 
{
    var values = objForm.serialize();
    $.ajax({
        url: ajaxURL,
        type: "post",
        data: values,
        async: false,
        success: function(data)
        {
            if(typeof data !== 'undefined' && $.isNumeric(data)) {//check that server send correct anserw
               $('whereIsData').text(data);
            }
        },
        error:function()
        {
            alert('Connection error. Please contact administrator. Thanks.');
        }
    });

}

Now in success method you populate some DOM element using text method. You cannot simply return data from ajaxCall method because $.ajax is asynchronized method and responseData has value only when ajax request ends, so always return undefined in you example. You must present responseData to the user in success callback method.

For one thing...

$sql = ("UPDATE project_allocate_ba SET resource_allocated='$resource', role='$role', tentatively_starts_on='$start_date_tentative' WHERE project_id=$pdid")

needs single quotes around $pdid

Also don't echo the $sql. Instead do your inspection and form a response.

$response = array();

if(EVERYTHING_IS_GOOD){
  $response['status'] = 'good to go';
}else{
  $response['status'] = 'it went horribly wrong';
}

echo json_encode($response);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM