简体   繁体   English

在javascript / ajax内的页面上调用php var

[英]Calling php var on page inside javascript/ajax

I have the following code for a checkbox 我有以下代码的复选框

<div class="switch switch-lg switch-primary">
<input type="checkbox" name="switch" data-plugin-ios-switch checked="checked" value="usersname:1" />
</div>

When I change the checkbox, the following event is triggered 当我更改复选框时,将触发以下事件

 <script> $('input[name="switch"]').on('change', function(){ $.ajax({ type: 'POST', url: 'page.php', data: { example: $('#example').val() } }).always(function(data, textStatus, jqXHR) { if (data.response == 'success') { // Success } else { // Error } }); }); </script> 

I am wondering how I get the value of "usersname:1" in my page.php? 我想知道如何在page.php中获取“ usersname:1”的值? I'm guessing it's around example: $('#example').val() ? 我猜这是周围的example: $('#example').val()吗?

Basically the page.php will update a mysql database with the value 1, but specific to only that user, so i need to somehow pull up the username:1 in that script portion or the page.php but don't know how to call it. 基本上,page.php将使用值1更新mysql数据库,但仅特定于该用户,因此我需要以某种方式在该脚本部分或page.php中拉起用户名:1​​,但不知道如何调用它。

Thanks in advance! 提前致谢!

In order to send data to php your have to modify your code line: data: { example: $('#example').val() } 为了将数据发送到php,您必须修改代码行: data: { example: $('#example').val() }

to: 至:

    data: {
        example: $('#example').val(),
        switch: $(this).val()
    }

The code finally becomes: 该代码最终变为:

 $('input[name="switch"]').on('change', function(){ console.log($(this).val()); $.ajax({ type: 'POST', url: 'page.php', data: { example: $('#example').val(), switch: $(this).val() } }).always(function(data, textStatus, jqXHR) { if (data.response == 'success') { // Success } else { // Error } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="switch switch-lg switch-primary"> <input type="checkbox" name="switch" data-plugin-ios-switch checked="checked" value="usersname:1" /> </div> 

In PHP you can get value using $_REQUEST['switch'] 在PHP中,您可以使用$ _REQUEST ['switch']获得价值

Add id attributes in your checkbox. 在您的复选框中添加id属性。 You can't define id attributes in checkbox . 您不能在checkbox中定义id属性。

<div class="switch switch-lg switch-primary">
<input type="checkbox" id="example" name="switch" data-plugin-ios-switch checked="checked" value="usersname:1" />
</div>

Otherwise You can change example: $('#example').val() to example: $(this).val() 否则,您可以将example: $('#example').val()更改为example: $(this).val()

您可以通过$_POST['example']在page.php中获取example

Ok, this is a tricky one. 好的,这是一个棘手的问题。

With example: $('#example').val() you will pass the VALUE of an input with ÌD example as a post var (so you can catch it with $_POST["example"] in the end script. Thing is, anyone can put anything into that and edit other username. 使用example: $('#example').val()您将传递带有ÌD example的输入值作为后置变量(因此,您可以在最终脚本中使用$ _POST [“ example”]进行捕获。 ,任何人都可以在其中添加任何内容并修改其他用户名。

You should be doing this by calling a $_SESSION variable with the userid or something alike so people don't tamper with the data or make some XSS. 您应该通过使用$ userid或类似名称调用$ _SESSION变量来做到这一点,以使人们不会篡改数据或创建一些XSS。

So, if you want to go with that, just put the ID example into your input and it will work (or edit it to match your fields), but i suggest to find another way. 因此,如果您想这样做,只需将ID示例放入您的输入中即可使用(或对其进行编辑以匹配您的字段),但是我建议您找到另一种方法。

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

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