2015-09-25 13:09

无法将带有html的php echoed数据提取到responseText中,返回到ajax表单

  • php
  • ajax

Can anybody help me with this problem please. I've been researching for days and I can't make the php echoed array containing html code to be formatted for the responseText of javascript. I've tried to format the echoed array with json_encode($array) but doesn't work. I've tried responseText.split(" ") and then used a for loop to extract the html code inside the array but I can't either. I know the javascript code is working because I manually inserted html code like this: i.e

responseText = '<p><img src="img/moon.png">here is your image</p>'

This works perfect.But when php spits out the array all I see on the screen with the code you see below is the Array word. I know my php code is also working because I tested it before sending the text using POST method.

Your help is appreciated.

My ajax to php

    function ajaxObj( meth, url ) {

    var x = new XMLHttpRequest(); meth, url, true );

    x.setRequestHeader("Content-type", "application/x-www-form-    urlencoded");

        return x;


function ajaxReturn(x){

    if(x.readyState == 4 && x.status == 200){

    return true;





function gotext(){

      var mm = document.getElementById("todotext").value;

      var isTr=false;

      var ajaxfile = ajaxObj("POST", "env.php");

      ajaxfile.onreadystatechange = function() {

         if(ajaxReturn(ajaxfile) == true) {

               document.getElementById("outputimgs").innerHTML = ajaxfile.responseText;




$('form[name=uturn]').on('submit', function(e) {
    $('html, body').animate({
         scrollTop: $("#first").offset().top
    }, 1000);
    return false;

This is the html form

<form name="uturn" action="process.php" method="post">

<textarea id="todotext" name="text" placeholder="type or paste words here"></textarea><br/>

<input type="submit" id="arefbut" class="button scrolly" value="Convert"/>


        <!-- First -->

            <section id="first" class="main">

<div class="container">
                        <h2>Done !!</h2>
                <p id="outputimgs">end</p>

This is the dynamic construction of html in php

if(isset($_POST['todotext'])) {

$chunk = $_POST['todotext']; 

$commentArray = explode(" ",$chunk);


$commentArray[$i]='<p class="pos"><img src="imgs/blank.png" width="40" height="40"><br>

$together = implode(" ",$commentArray);    

echo $together;

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享