简体   繁体   中英

How to send and receive hidden value using Ajax

This is my job id which is in php.

<td id="JobId"><?php echo $JobResults['id_job']; ?></td>

This is my reinvite button when i clicks this button i have to send hidden value that is job id using ajax:

<button id="ReInvite">Reinvite</button>

And this my ajax call:

$('#ReInvite').click(function() {
    JobId = $('#JobId').val();
    $.ajax({
        url: "job-controller.php",
        method: "POST",
        data: {'action':'reinvite','JobId' : + JobId},
        dataType: "json",
        success: function (response) {
                console.log(response);
                $("#showMessage").html(response['message']);
        },
        error: function (request, status, error) {
            $("#showMessage").html("OOPS! Something Went Wrong Please Try After Sometime!");
        }
    });
    return false;
});

this is my controller page to call the hidden value:

if($_POST['action']=='reinvite'){ 
    $Jobid = trim($_GET['JobId']);
    echo $JobId;
    exit;
});

My Error is job id value is coming as zero.

You need to change your,

data: {'action':'reinvite','JobId' : + JobId},

as,

{'action':'reinvite','JobId' : + $('#JobId').text()},

Hope this helps!

Change this line:

JobId = $('#JobId').val();

to:

JobId = $('#JobId').text();

val() in jQuery is used to pull data from textareas and inputs. You can use text() to extract text from html elements, or html() to extract the entire html within that particular element.

它应该是$_POST['JobId']而不是$_GET['JobId']

你必须从$ _POST而不是$ _GET获得它。

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