F
F
ftp142020-05-24 13:21:56
PHP
ftp14, 2020-05-24 13:21:56

PHP and AJAX issue. How to change value in AJAX using PHP?

There is a test script created in AJAX and PHP so that the page does not reload. In AJAX we have the following code:

function send() {
    var text = $('...').val()
        $.ajax({
            type: "POST",
            url: "...",
            data: "...="+text,
            success: function(html) {
            $("...").empty();
            $("...").append(html);
        }
    });
}

It turns out that we call the send function, which sends the value of the variable and sends the PHP response to the screen. The question is: how in a line instead of an ellipsis (in PHP there is a variable that should be displayed), how to change the value using PHP? For example, at first we have:var text = $('...').val()
function send() {
    var text = $('input_1').val()
        $.ajax({
            type: "POST",
            url: "test.php",
            data: "answer="+text,
            success: function(html) {
            $("text").empty();
            $("text").append(html);
        }
    });
}

After executing PHP:
function send() {
    var text = $('input_2').val()
        $.ajax({
            type: "POST",
            url: "test.php",
            data: "answer="+text,
            success: function(html) {
            $("text").empty();
            $("text").append(html);
        }
    });
}

The value has changed because the value of the variable in PHP too. How to implement it? var text = $('...').val()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2020-05-24
@ftp14

return json with response and variable name

var inputName = '<?=$phpVar?>';
function send() {
        $.post(
            "test.php",
            {answer: $('input[name="'+inputName+'"]').val()},
           function (json) {
               $(".text").html(json.text);
               inputName = json.inputName;
           }
       );
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question