php 将值从ajax传递给同一页面中的php变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16933515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:54:30  来源:igfitidea点击:

Passing value from ajax to php variable in same page

phpjquery

提问by user2454281

Since 3days i am trying my best to get the solution from Ajax & PHP, i tried all tutorial but i am unable to get the solution, i am new to Ajax,Jquery but my question is really simple to you all.

自从 3 天以来,我一直在尽力从 Ajax 和 PHP 中获得解决方案,我尝试了所有教程,但无法获得解决方案,我是 Ajax、Jquery 的新手,但我的问题对你们来说真的很简单。

i have developed website using jquery & PHP, i have created menu using HTML (ul, li) so what i want is, if i click on menu item ajax should send value to php variable and then execute php function, but all this should happen in same page,..

我已经使用 jquery 和 PHP 开发了网站,我已经使用 HTML (ul, li) 创建了菜单,所以我想要的是,如果我点击菜单项 ajax 应该将值发送到 php 变量,然后执行 php 函数,但这一切都应该发生在同一页面,..

Please help me to resolve the issues.

请帮我解决这些问题。

So far, I have tried the following:

到目前为止,我已经尝试了以下方法:

JavaScript:

JavaScript:

<script type="text/javascript">
    $("#btn").click(function() {
        var val = "Hi";
        $.ajax ({
            url: "oldindex.php",
            data: val,
            success: function() {
                alert("Hi, testing");
            }
        });
    });
</script>

PHP and HTML:

PHP 和 HTML:

<input type="submit" id="btn" value="submit">
<form name="numbers" id="numbers">
    <input type="text" name="number" id="number">
</form>

<div id="number_filters">
    <a href="abc">1</a>
    <a href="def">2</a>
    <a href="ghi">3</a>
</div>

so if i click on href, i should get the value to php variable it should happen in same page only

所以如果我点击href,我应该得到php变量的值它应该只发生在同一页面

采纳答案by som

index.phppage

index.php

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#btn").click(function() {
        var val = "Hi";
        $.ajax ({
            url: "ajax.php",
            data: { val : val },
            success: function( result ) {
                alert("Hi, testing");
                alert( result );
            }
        });
    });
});
</script>

<input type="submit" id="btn" value="submit">
<form name="numbers" id="numbers">
    <input type="text" name="number" id="number">
</form>

<div id="number_filters">
    <a href="abc">1</a>
    <a href="def">2</a>
    <a href="ghi">3</a>
</div>

ajax.phppage

ajax.php

<?php
echo ( $_GET['val'] );

回答by Naryl

Let's see:

让我们来看看:

1- If you are doing a AJAX call, your page won't be refreshed. So if you try to send variables to the same page that makes the AJAX call it won't work, here's why. When you are able to see the page and execute the AJAX call, the code is already on the client side (your web explorer), there no PHP will be seen or executed (PHP is executed on the server only), so it's imposible for the same page to capture and process variables you pass to it using AJAX (since AJAX WON'T refresh the page, that's the point of AJAX).

1- 如果您正在进行 AJAX 调用,您的页面将不会刷新。因此,如果您尝试将变量发送到进行 AJAX 调用的同一页面,它将无法工作,原因如下。当您能够看到页面并执行 AJAX 调用时,代码已经在客户端(您的 Web 浏览器)上,不会看到或执行 PHP(PHP 仅在服务器上执行),因此不可能使用 AJAX 捕获和处理传递给它的变量的同一页面(因为 AJAX 不会刷新页面,这就是 AJAX 的重点)。

2- If you are using AJAX you don't have to call to the same page. Call to another PHP, it will make the server side work for you, then return the result:

2- 如果您使用 AJAX,则不必调用同一页面。调用另一个PHP,它会让服务器端为你工作,然后返回结果:

success: function(data) {
   alert("Hi, server returned this: "+data);
}

3- When you pass variables using AJAX you have to assign the variable a name, so it can be read in the PHP side:

3- 当您使用 AJAX 传递变量时,您必须为变量分配一个名称,以便可以在 PHP 端读取它:

data: {value: val},

4- For what you have in your question, you don't start the AJAX call clicking a href, you have the AJAX function linked to a input type=submit, it also is outside a form.. so let's do this better:

4-对于您的问题,您不会通过单击 href 启动 AJAX 调用,而是将 AJAX 函数链接到 a input type=submit,它也在表单之外..所以让我们做得更好:

<button id="btn">submit</button>

回答by Neeraj Singh

Here is your solution as given sample code:

这是给定示例代码的解决方案:

<?php if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    echo $_GET['q'];
    exit;
} ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(function(){    
        $("#btn").click(function(e) {
            e.preventDefault();
            var val = "Hi";
            $.ajax ({
                url: "test8.php",
                // wrong query. you are not passing key , so here q is key
                data: 'q=' + val,
                success: function(returnResponseData) {
                alert('Ajax return data is: ' + returnResponseData);
                }
            });
        });
    });
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
</head>

<body>
    <form name="numbers" id="numbers">    
        <input type="text" name="number" id="number">
        <input type="submit" name='button' id="btn" value="submit">
    </form>
</body>
</html>