简体   繁体   English

尝试使用XMLHttpRequest()将值从jquery传递到php文件

[英]Trying to pass values from jquery to php file using XMLHttpRequest()

I am trying to pass values in an HTML textarea to a PHP file using jQuery. 我正在尝试使用jQuery将HTML文本区域中的值传递给PHP文件。 This is what I have so far: 这是我到目前为止:

JQuery JQuery的

var jq = $.noConflict();
jq("#addUsers").click(function () {
    jq("#addModal").modal("show");
});

jq("#addUser").click(function () {
    var text = jq("textarea#emails").val();
    if (text.length == 0)
    {
        alert("Please enter new user e-mails line by line.");
    }
    else
    {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200)
            {
                jq("textarea#emails").innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET", "add_users.php?emails=" + text + "&list=1", true);
        xmlhttp.send(null);
    }
});

add_users.php add_users.php

<?php
//error_reporting(E_ALL);
ini_set('display_errors', '1');

$list = $_REQUEST["list"];
$emails = trim($_REQUEST["emails"]);

// This is where I will parse out the text line-by-line
// .....
?>

So, this is all activated by the click function on line 6. When I do this however, nothing happens . 所以,这一切都是由第6行的点击功能激活的。但是当我这样做时, 没有任何反应 I tried putting an echo statement pretty much everywhere inside the function and it echoes out fine. 我尝试将echo语句几乎放到函数内的任何地方,并且它可以很好地进行呼应。 Additionally: 另外:

  • The php file is in the same directory as the file I'm working with. php文件与我正在使用的文件位于同一目录中。
  • I've tried echoing out in the php file; 我试过在php文件中回声; the php file is never accessed. 永远不会访问php文件。
  • I've tried echoing out the response text using alert(); 我尝试使用alert()回显响应文本; nothing happens. 什么都没发生。

Not sure what I'm doing wrong. 不知道我做错了什么。 Help please!! 请帮助!!

For one, jq("textarea#emails").innerHTML is not going to work because you're dealing with a jQuery wrapped element now, and not a raw HTML element. 首先, jq("textarea#emails").innerHTML不会起作用,因为你现在正处理一个jQuery包装元素,而不是原始的HTML元素。 If you use jQuery you want to set the contents with val() or html() (depending on the element). 如果你使用jQuery,你想用val()html()设置内容(取决于元素)。

So it's possible you are calling your script successfully, and just not outputting the results on your page. 因此,您可以成功调用脚本,而不是在页面上输出结果。

You can simplify your ajax tremendously by using jQuery. 您可以使用jQuery极大地简化您的ajax。 I'd use load() specifically, since your intent is to stick the results into an element. 我将特别使用load() ,因为您的意图是将结果粘贴到元素中。

I would start with this: 我将从这个开始:

jq("#addUser").click(function () {
    var text = jq("textarea#emails").val();
    if (text.length == 0)
    {
        alert("Please enter new user e-mails line by line.");
    }
    else
    {
        jq("textarea#emails").load("add_users.php?emails=" + text + "&list=1");
    }
});

Reference: http://api.jquery.com/load/ 参考: http//api.jquery.com/load/

Once you have your ajax query working successfully, check your Dev Tools Network tab (assuming Chrome here) to watch the ajax call and see how your server is responding. 一旦您的ajax查询成功运行,请检查您的开发工具网络选项卡(假设此处为Chrome)以观看ajax调用并查看服务器的响应方式。

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

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