在成功时将PHP变量传递给AJAX

时间:2015-01-16 02:05:44

标签: javascript php jquery ajax

我正在使用AJAX& PHP创建一个包含从MySQL数据库查询的值的简单表。这是我第一次尝试使用AJAX和PHP,但到目前为止,我已经成功地完成了工作。我意识到这里几乎所有的东西都可以用更高效的方式完成。更好的方式,并对所有指针和建议持开放态度!

我使用的PHP脚本包含了很多用于构造表的echo

我想知道是否有一种方法允许我将变量(字符串)的值从PHP返回到AJAX,我可以在代码中的其他位置使用它?我一直在寻找StackOverflow的答案,没有任何帮助我的结果。

以下是我的PHP的相关部分(最后一行是我想要返回的部分):

translator.php

    //Creates the table structure 

    echo "<table data-toggle='table' class='table table-striped'>";
    echo "<tr>";
    echo "<th>Name</th>";
    echo "<th>Cuisine</th>";
    echo "<th>Opening Hours</th>";
    echo "<th>Price Range</th>";
    echo "<th>Student Discount</th>";
    echo "<th>Address</th>";
    echo "<th>Telephone</th>";
    echo "<th>Website</th>";
    echo "</tr>";

    // Insert a new row in the table for each restaurant returned

    //This is a loop which goes through all the rows resulted from the query
    //The loop adds to the table for each go around.
    while ($row = mysqli_fetch_array($qry_result)) {
        $addressArray[] = $row[rAddress];
        echo "<tr>";
        echo "<td>" . $row[rName] . "</td>";
        echo "<td><b>" . $row[rCuisine] . "</b></td>";
        echo "<td>" . $row[rOpeningHours] . "</td>";
        echo "<td>" . $row[rPriceRange] . "</td>";
        echo "<td>" . $row[rStudentDiscount] . "</td>";
        echo "<td>" . $row[rAddress] . "</td>";
        echo "<td>" . $row[rTelephone] . "</td>";
        echo '<td><a href="' . $row[rWebsite] . '">' . $row[rWebsite] . '</a></td>';
        echo "</tr>";           
    }

    //Closes the table.
    echo "</table>";
    $arrayString = implode("+Göteborg', '", $addressArray);
    $mapVariable = "'" . $arrayString . "+Göteborg'";
    echo json_encode($mapVariable); //The variable I would like sent back

这是AJAX代码:

AJAX

var ajaxFunction = function() {
                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;
                        }
                    }
                }

                /* Creates a function that will receive data sent from the server
                and updates the ajaxDiv found in the bottom */

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

                /* Gets the value from the user and passes it to the server script */
                /* At this stage, a few values are not being used and have been commented out */

                var rStudentDiscount = ($('input:radio[name=rStudentDiscount]:checked').val());
                var rCheckbox = [];
                $('.rCuisine:checked').each(function(i, e) {
                    rCheckbox.push($(this).val());
                });

                $.ajax({
                    url: "translator.php",
                    type: "post",
                    dataType: "json",
                    data: {
                        'rCheckbox[]': rCheckbox.join()
                    },
                    success: function(data) {                           

                    },
                    complete: function(data) {
                        //run function
                        mapFunction();
                    }
                });

                var queryString = "?rStudentDiscount=" + rStudentDiscount + "&rCheckbox=" + rCheckbox;
                ajaxRequest.open("GET", "translator.php" + queryString, true);
                ajaxRequest.send(null);
            }

1 个答案:

答案 0 :(得分:0)

成功:function(data){}携带执行服务器端响应发出的响应。我在translator.php中看到了你的代码,你正在创建一个完整的html响应,然后用Json对它进行编码。

我一直在开发使用Ajax的应用程序,但是我创建了一个JavaScript数组并将其返回给服务。也许你可以这样做。您在translator.php中不需要任何echo或html响应(如果它的唯一目的是处理ajax的数据)。

如果删除所有回声并且只有最后一个

echo json_encode($mapVariable);

然后,您可以在成功数据中使用它。