简体   繁体   English

无法在php中访问ajax发布请求

[英]Can't acess ajax post request in php

I've searched everywhere but can't find an answer to this problem. 我到处搜索,但找不到该问题的答案。

I'm writing a little ajax script but can't get the correct value of the POST request. 我正在编写一个小的Ajax脚本,但是无法获得POST请求的正确值。 This is the code so far: 到目前为止,这是代码:

 <textarea id="message" name="message" style="width:100%;"></textarea>
 <input value="SEND" style="border-radius: 5px 5px 5px 5px;" type = 'button' onclick = 'ajaxFunction()'/>



<script type="text/javascript">                             <!--
            //Browser Support Code
            function ajaxFunction(){
               var ajaxRequest;  // The variable that makes Ajax possible!

               try {
                  // Opera 8.0+, Firefox, Safari
                  ajaxRequest = new XMLHttpRequest();
               }catch (e) {
                  // Internet Explorer Browsers
                  try {
                     ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                  }catch (e) {
                     try{
                        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                     }catch (e){
                        // Something went wrong
                        alert("Your browser broke!");
                        return false;
                     }
                  }
               }

               // Create a function that will receive data 
               // sent from the server and will update
               // div section in the same page.

               ajaxRequest.onreadystatechange = function(){
                  if(ajaxRequest.readyState == 4){
                     var ajaxDisplay = document.getElementById('chbox');
                     ajaxDisplay.innerHTML = ajaxRequest.responseText;
                  }
               }

               // Now get the value from user and pass it to
               // server script.

               var message = document.getElementById('message').value;
               var queryString = message ;
               ajaxRequest.open("POST", 'chatdata.php', true);
               //ajaxRequest.send(null); 
               ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
               ajaxRequest.send('queryString');
            }
</script>



<?php
$message1 = $_REQUEST['message'];
echo $message;
?>

when i use print_r($message); 当我使用print_r($message); to see the content of the POST value this is what i get Array ( [queryString] => ). 要查看POST值的内容,这就是我得到的Array([queryString] =>)。 It has no values. 它没有值。 What could be wrong with my code? 我的代码有什么问题? (I would have used jQuery but i'm not well grounded in it yet.) (我本来会使用jQuery,但我还没有扎根。)

Here's how you would do it in jQuery - much simpler: 这是您在jQuery中执行的操作-简单得多:

 $('#mybutt').click(function(){ var txt = $('#message').val(); $.ajax({ type: 'post', url: 'my_ajax_processor_file.php', data: 'ta=' + txt, success: function(d){ if (d.length) alert(d); } }); }); //END mybutt.click 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <textarea id="message" name="message" style="width:100%;"></textarea> <input id="mybutt" value="SEND" style="border-radius: 5px 5px 5px 5px;" type='button' /> 

my_ajax_processor_file.php my_ajax_processor_file.php

<?php
    $txt = $_POST['ta'];
    $out = 'You sent: ' .$txt;
    echo $out;

Here are a bunch of free 5-min video tuts for jQuery 这是一堆免费的jQuery 5分钟视频教程

The trouble is with query string. 问题出在查询字符串上。 You should put ajaxRequest.send(queryString) instead of ajaxRequest.send('queryString'); 您应该放置ajaxRequest.send(queryString)而不是ajaxRequest.send('queryString'); . Don't use query string just use the name of the variable. 不要使用查询字符串,只需使用变量名即可。 It should work! 它应该工作!

I fixed some bugs and code start works: 我修复了一些错误,代码开始起作用:

1. 1。

<p id="chbox"></p> <!-- ajaxDisplay need this -->

2. 2。

ajaxRequest.send("message="+queryString); //queryString is variable so without quotes

3. 3。

var_dump($message1); //there was message without 1

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

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