how to use ajax to send a javascript variable to PHP?

青春壹個敷衍的年華 提交于 2021-01-20 13:55:41

问题


I am using this javascript code to object a data of a td when pressing a button in an onclick event

  function rfk(element) {
    var element = element.parentElement.parentElement;

      var id = parseInt(element.children[0].innerText);

      $.ajax({
            url: './queryuser.php',
            type: "POST",
            dataType:'text',
            data: {'id': id},

        });
  }

when using an alert instead of var = id shows the value of the td

but this is failing the sentence of ajax when sending to php I do not know how to solve it

PHP code queryuser.php :

  <?php
    require("conextion.php");
    require("sql.php");

   $connect=start();

    $display= show($connect,$_POST['id']);

    disconnect=($connect);



   header("Location:index.php");
 ?>

Query sql:

    function show($con, $id){
    $query="    SELECT  user_name FROM  user 
    WHERE $id='?'";
    $call = $con->prepare($query);
    $call ->execute();

    return $call;
    }

回答1:


Pass the data like this to the ajax call (http://api.jquery.com/jQuery.ajax/): and select elements perfectly to get text

function rfk(element) {
  var element = element.parentElement.parentElement;
   var id = parseInt(element.children[0].innerText);
 $.ajax({
           url: '/queryuser.php',
            type: "POST",
            data: {id: id}    
        });
  }


来源:https://stackoverflow.com/questions/53489168/how-to-use-ajax-to-send-a-javascript-variable-to-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!