duanmianzhou5353 2016-10-16 21:29
浏览 22

AJAX没有重新加载页面

I am tasked to use JavaScript and JSON to return and display the data on browser. I am very new to AJAX and I am having trouble getting AJAX fired by button click.The Main issue is getting AJAX fired up.

I am trying to load the data on the same screen every time an user enters a new data.

PHP Code:

<h2>Submit Recipes</h2>
<form id="addRecipes" name="addRecipes" action="Welcome.php"method="POST">
    <fieldset>
        <legend>Submit Recipes</legend>
        <textarea id="recipesId" name="recipes" rows="6" cols="70"></textarea>
        <br/>
        <input type="button" value="Submit" name="submit" onclick="submitRecipes()"/>
    </fieldset>
</form>
<br/>
<table id="recipesTbl" border="1" >
    <tr>
        <th class="row-datetime" name="datetime">DateTime </th>
        <th class ="row-recipes" name="recipes">  Recipes </th>
    </tr>
</table>

Ajax Code:

function submitRecipes() {

    var recipes = document.getElementById("recipesId").value;   
    if (recipes == null || recipes == "") {
        alert("Please enter the recipes");
        return false;
    }

    var request = new XMLHttpRequest();

    request.onreadystatechange = function() {

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

             var text = request.responseText;
             var json = JSON.parse(text);

            var table = document.getElementById("recipesTbl");

            var newRow = table.insertRow(1);

            var newCell = newRow.insertCell(0);
            newCell.innerHTML = json.datetime;

            newCell = newRow.insertCell(1);
            newCell.innerHTML = json.recipes;

        }
    }
    request.open("POST", "SubmitRecipes.php", true);
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");    
    request.send("recipes=" + recipes);

    document.getElementById("recipesId").value = "";
}

PHP File Snippet:

<?php
$recipes= $_POST["recipes"];
date_default_timezone_get('UTC');
$datetime = date("Y-m-d H:i:s");

storeRecipes($recipes, $datetime);

echo ('{ "datetime" : "'. $datetime. '",'.
        '"recipes": "'.recipes .'"}');
?>
  • 写回答

1条回答 默认 最新

  • duanaoshu1989 2016-10-17 01:24
    关注

    The function storeRecipes does not exist. To make JSON.parse work, single quotation mark (') must be outside and double quotation mark (") must be inside.

    For example :

    var string = '{"a": "1", "b": "2"}';
    var res = JSON.parse(string);
    console.log(res);

    With

    <?php
    
    $recipes = $_POST["recipes"];
    date_default_timezone_get('UTC');
    $datetime = date("Y-m-d H:i:s");
    
    // storeRecipes($recipes, $datetime);
    
    $data = '{"datetime": ' . '"' . $datetime . '",';
    $data .= '"recipes": ' . '"' . $recipes . '"}';
    
    echo $data;
    ?>
    

    (and with the script file in the <head> section) it should work!

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画