php 使用ajax将javascript变量传递给php,结果没有显示任何内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16434482/
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:07:45  来源:igfitidea点击:

Pass javascript variable to php with ajax and the result doesn't show anything

phpjavascriptajax

提问by Monogot

This is my code and i want to pass javascript variable with ajax to php when i click submit button then the result doesn't show var_data variable from javascript What code is wrong? This is edit order one before everybody help me

这是我的代码,当我单击提交按钮时,我想将带有 ajax 的 javascript 变量传递给 php,然后结果没有显示来自 javascript 的 var_data 变量 什么代码错了?这是大家帮我之前的编辑顺序

 <!DOCTYPE html>
<html>
<head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
        $(document).ready(function() {
            $('#sub').click(function() {
                var var_data = "Hello World";
                $.ajax({
                    url: 'http://localhost/ajax/PassVariable.php',
                    type: 'GET',
                     data: { var_PHP_data: var_data },
                     success: function(data) {
                         // do something;

                     }
                 });
             });
         });
 </script>

 </head>
 <body>

 <input type="submit" value="Submit" id="sub"/>

 <?php 
   $test = $_GET['var_PHP_data'];
     echo $test;
 ?>
 </body>
 </html>

and this is source code now

这是现在的源代码

 <?php
     if (isset($_GET['var_PHP_data'])) {
       echo $_GET['var_PHP_data'];
     } else {
     ?>
     <!DOCTYPE html>
     <html>
       <head>
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
            <script src="http://malsup.github.com/jquery.form.js"></script> 
         <script>
             $(document).ready(function() {
                 $('#sub').click(function() {
                     var var_data = "Hello World";
                     $.ajax({
                         url: 'http://localhost/test.php',
                         type: 'GET',
                          data: { var_PHP_data: var_data },
                          success: function(data) {
                              // do something;
                             $('#result').html(data)
                          }
                      });
                  });
              });
         </script>
       </head>
       <body>
         <input type="submit" value="Submit" id="sub"/>
         <div id="result">
       </body>
     </html>
    <?php } ?>

this statement if(isset($_GET['var_PHP_data'])) output false and then show Hello World What should i do to do for isset($_GET['var_PHP_data']) is true?

这个语句 if(isset($_GET['var_PHP_data'])) 输出假然后显示 Hello World 我应该怎么做 isset($_GET['var_PHP_data']) 是真的?

采纳答案by Zlatko

Your solution has PHP issues: you don't check if the data exists, and also, you don't do anything with the result. I've modified the script to do the following:

您的解决方案存在 PHP 问题:您不检查数据是否存在,而且您不对结果执行任何操作。我修改了脚本以执行以下操作:

  1. Check if the var_PHP_data var is set (in PHP, on the server).
  2. If yes, just send a blank text response containing that data.
  3. If no, then draw the form and everything else.
  4. In the form, I've created a #resultdiv.
  5. Ajax response will be shown in this div.
  1. 检查 var_PHP_data 变量是否已设置(在 PHP 中,在服务器上)。
  2. 如果是,只需发送包含该数据的空白文本响应。
  3. 如果没有,则绘制表格和其他所有内容。
  4. 在表单中,我创建了一个#resultdiv。
  5. Ajax 响应将显示在此 div 中。

Also make sure that you host the script at localhost and that it is called test.php. To make sure this is resilient, you can change the Ajax URL to <?php echo $_SERVER['PHP_SELF'];?>to make sure that you'll hit the correct script.

还要确保您将脚本托管在 localhost 并且它被称为 test.php。为了确保这是有弹性的,您可以将 Ajax URL 更改为 <?php echo $_SERVER['PHP_SELF'];?>以确保您将命中正确的脚本。

<?php
    if (isset($_GET['var_PHP_data'])) {
      echo $_GET['var_PHP_data'];
    } else {
    ?>
    <!DOCTYPE html>
    <html>
      <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js">
        <script>
            $(document).ready(function() {
                $('#sub').click(function() {
                    var var_data = "Hello World";
                    $.ajax({
                        url: 'http://localhost/test.php',
                        type: 'GET',
                         data: { var_PHP_data: var_data },
                         success: function(data) {
                             // do something;
                            $('#result').html(data)
                         }
                     });
                 });
             });
        </script>
      </head>
      <body>
        <input type="submit" value="Submit" id="sub"/>
        <div id="result">
      </body>
    </html>
    <?php } ?>

回答by Vitmais

Try jQuery Formits this will help to solve many problems.

尝试使用jQuery Form这将有助于解决许多问题。

For you question: try url without domain name, add tags 'form', change event click to submit, add data type

对于您的问题:尝试不带域名的网址,添加标签“表单”,更改事件点击提交,添加数据类型

回答by user2361197

what are the contents of PassVariable.php ? if is the same where you have they jquery bit wont work coz php will print all the page again, if the file is different try

PassVariable.php 的内容是什么?如果是相同的地方,他们 jquery 位将无法工作,因为 php 将再次打印所有页面,如果文件不同,请尝试

success: function(data) {
                     alert('databack = '+ data);

                 }

回答by Dimitar K

Try placing your input into a form and attaching the ajax call to the form onsubmit event. The way it happens in the provided happen is when you click in the field, in which case it submits before you can write anything really.

尝试将您的输入放入表单并将 ajax 调用附加到表单 onsubmit 事件。它在提供的情况下发生的方式是当您单击该字段时,在这种情况下,它会在您真正编写任何内容之前提交。

回答by Robert

$(document).ready(function() {
        $('#brn').click(function() {
            var var_data = "Hello World";
            alert("click works");
            $.ajax({
                url: 'http://localhost/ajax/PassVariable.php',
                type: 'GET',
                 data: { x: var_data },
                 success: function(data) {
                     alert(data);

                 }
             });
         });
     });

change it to this code

将其更改为此代码

then in PassVariable.php put

然后在 PassVariable.php 中放入

make button

制作按钮

<input type="button" id="btn"  value="click me" />

it should work because it is very basic example. If it doesn't work check your console if there are any JavaScript errors and remove them.

它应该有效,因为它是非常基本的示例。如果它不起作用,请检查您的控制台是否有任何 JavaScript 错误并将其删除。