dpkpaxhzffixp8426 2013-12-07 11:22
浏览 93
已采纳

调用ajax函数时找不到请求的URL

//I want to insert a text in the database (MySql) by calling a js function then another js //scripts retrieves the data from the database and displays it in a div on my html page //from //where i posted the text ....

//HTML from where i submit the text to be saved in the database

<html>
<head>
</head>
<body>
<div id = "change">Naruto</div>
<form id = "foo" action = "update()">
<input type = "text" name = "text" id = "text"/>
<input type = "submit" value = "change" ></input>
</form>
</body>
</html>

// the function that is called when i click the button

<script>

function update()
{
    $("#foo").submit(function(event) {

    /* Stop form from submitting normally */
    event.preventDefault();

    /* Clear result div*/
    $("#result").html('');

    /* Get some values from elements on the page: */
    var values = $(this).serialize();

    /* Send the data using post and put the results in a div */
    $.ajax({
        url: "acition.php",
        type: "post",
        data: values,
        success: function(){
            alert("success");
            $("#result").html('Submitted successfully');
        },
        error:function(){
            alert("failure");
            $("#result").html('There is error while submit');
        }
    });
});
}
</script>

//the php file that actually updates the data

<?php

mysql_connect("localhost","root","") or die ("CANNOT CONNECT TO THE DATABASE".mysql_error());
mysql_select_db("ajax_database") or die ("CANNOT CONNECT TO THE DATABASE");

$text = $_POST['text'];
$sql = "insert into news(statement)values('$text')";
mysql_query($sql);
?>
<html>

<head></head>
<body>

// the script that gets the data from a different page to show it to the html div

<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("change").innerHTML = xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","getdata.php",true);
xmlhttp.send();
</script>

</body>

</html>

// the php file that selects the data to be retrieved from the database // getdata.php

<?php

mysql_connect("localhost","root","") or die ("CANNOT CONNECT TO THE DATABASE".mysql_error());
mysql_select_db("ajax_database") or die ("CANNOT CONNECT TO THE DATABASE");
$sql = "select * from news";
$result = mysql_query($sql);

while ($rows = mysql_fetch_array($result))
{
    echo $rows[0];
}
mysql_close();
?>

//basically just like we update the status on facebook .... i am doing the same thing .... //without refreshing the whole page it refreshes the part of the html page where the new //updated text will be shown //enter code herethanks i am really in a mess right now .... please help me out .... i am //new to ajax ...

  • 写回答

1条回答 默认 最新

  • dongshenghe1833 2013-12-07 11:36
    关注
    <form id = "foo">
      <input type = "text" name = "text" id = "text"/>
      <input type = "submit" value = "change" ></input>
    </form>
    

    remove action attribute and in javascript

    $("#foo").submit(function(event) {
        event.preventDefault();
        $("#result").html('');
        $.ajax({
            url: "action.php",
            type: "post",
            data: $(this).serialize(),
            success: function(){
                alert("success");
                $("#result").html('Submitted successfully');
            },
            error:function(){
                alert("failure");
                $("#result").html('There is error while submit');
            }
        });
    });
    

    you don't need to call any function on submit, as u are already using jquery submit

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻看一个题
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)