简体   繁体   English

在ajax php mysql中使用全局javascript变量

[英]use a global javascript variable in ajax php mysql

I have a variable 'user_cache' stored in local storage: Now I am doing a $.getJSON call 我在本地存储中存储了一个变量'user_cache':现在我正在执行$ .getJSON调用

<script>

    user_data = JSON.parse(localStorage.getItem("user_cache"));

    $.getJSON("query.php",  { productId: productId}, function(data) { /*do something*/});

</script>

Query.php does a php_mysql database query: Query.php执行php_mysql数据库查询:

<? php
if( $_REQUEST["productId"] )
{
    $productid = $_REQUEST['productId'];

    /*Database connection*/
    include_once("php_includes/db_conx.php");

    $sql = "DELETE FROM USERPRODUCTCOLLECTION WHERE  Product_ID = $productid";


    if ($db_conx->query($sql) === TRUE) {
        echo "Success";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $db_conx->close();

}
?>

My question is that I also want to use the object 'user_data' in the query.php file to do that database query but I dont want to pass 'user_data' in $.getJSON. 我的问题是我也想使用query.php文件中的对象'us​​er_data'进行数据库查询,但是我不想在$ .getJSON中传递'user_data'。 Is it possible to use ' user_data ' object inside 'query.php' file without having to pass it in the $.getJSON ? 是否可以在'query.php'文件中使用' user_data '对象而不必在$.getJSON传递它?

Unfortunately that will not be possible. 不幸的是,这是不可能的。 You must pass the data to the php file in order to utilize it. 您必须将数据传递到php文件才能使用它。 Are you leaning away from passing the user data because it is passing it as a GET variable (in the URL)? 您是否希望传递用户数据,因为它是作为GET变量(在U​​RL中)传递的? If so, consider using a jQuery.post() call instead. 如果是这样,请考虑改用jQuery.post()调用。 This way the data will be posted instead of in the URL. 这样,数据将被发布,而不是发布在URL中。

See : http://api.jquery.com/jquery.post/ 参见: http : //api.jquery.com/jquery.post/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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