通过JavaScript变量PHP和AJAX,结果没有显示任何东西 [英] Pass javascript variable to php with ajax and the result doesn't show anything

查看:109
本文介绍了通过JavaScript变量PHP和AJAX,结果没有显示任何东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code,我想通过JavaScript变量与AJAX到PHP当我点击提交按钮,那么结果不从JavaScript什么code是错误的显示var_data变量? 这是编辑才能的人之前大家帮我

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

 <!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>

这是源$ C ​​$ C现在

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 } ?>

这条语句,如果(使用isset($ _ GET ['var_PHP_data']))输出错误再展世界您好我应该怎么做才能做使用isset($ _ GET ['var_PHP_data'])是真的吗?

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?

推荐答案

您解决方案具有PHP的问题:你不检查数据是否存在,而且,你不这样做,结果什么。我已经修改了脚本执行以下操作:

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:

  1. 检查var_PHP_data变量被设置(在PHP中,在服务器上)。
  2. 如果是的,只是发送一个空白的文本响应包含的数据。
  3. 如果没有,再画的形式和其他一切。
  4. 在窗体,我创建了一个 #result DIV。
  5. 在Ajax响应将显示在这个DIV来。
  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 #result div.
  5. Ajax response will be shown in this div.

另外,还要确保你在本地主机托管的脚本,它被称为test.php的。为了确保这是有弹性的,可以改变的Ajax网址 &LT;?PHP的echo $ _ SERVER ['PHP_SELF'];?> ,以确保你会击中正确的脚本

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.


<?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 } ?>

这篇关于通过JavaScript变量PHP和AJAX,结果没有显示任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆