gpt4 book ai didi

javascript - 无法在 php 中使用 jQuery 和 AJAX 将数据插入数据库

转载 作者:行者123 更新时间:2023-11-30 22:17:32 26 4
gpt4 key购买 nike

我可以使用纯 php 和 mysql 将数据插入数据库。但是无法在 PHP 中使用 jQuery 和 AJAX 将数据插入数据库。 AJAX 错误仅显示一个错误:即请检查您的网络连接。请帮我解决这个问题。

我的 jQuery AJAX 代码、PHP 代码、HTML 代码如下。

HTML --- news.php

    <div id="error"></div>
<form method="post" class="form-horizontal" role="form" name="newsForm">

<div class="form-group">
<label class="control-label col-sm-2">News Heading</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="newsHeading" name="newsHeading" placeholder="Enter News Heading">
</div>
</div> <!--News Heading-->

<div class="form-group">
<label class="control-label col-sm-2">News Source URL</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="newsSource" name="newsSource" placeholder="Enter News Source URL">
</div>
</div> <!--News Source-->

<div class="form-group">
<label class="control-label col-sm-2">News Content</label>
<div class="col-sm-10">
<textarea class="form-control" rows="3" id="newsContent" name="newsContent" placeholder="News Content"></textarea>
</div>
</div><!--News Data-->

<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" id="postNews" name="postNews" class="btn bg-red">Submit</button>
</div>
</div> <!--Submit Button-->


</form>

If I write Simple PHP Codes in news.php it works & inserts data into database.

<?php
if(isset($_POST['postNews'])===true)
{
$heading = $_POST['newsHeading'];
$source = $_POST['newsSource'];
$content = $_POST['newsContent'];
$query = mysql_query("INSERT INTO `news`(`news_heading`,`news_source`,`news_content`) VALUES('$heading','$source','$content')");

}
?>

以下脚本仅显示 AJAX 网络连接错误。无法向数据库中插入数据。

jQuery & AJAX

<script>
$(document).ready(function(){

$("#postNews").click(function(){
var newsHeading = $("#newsHeading").val();
var newsSource = $("#newsSource").val();
var newsContent = $("#newsContent").val();
var dataString = 'newsHeading='+newsHeading+'&newsSource='+newsSource+'&newsContent='+newsContent;

alert(dataString); //
if($.trim(newsHeading).length>0 && $.trim(newsSource).length>0 && $.trim(newsContent).length>0)
{
$.ajax({
type: "POST",
url: "core/news-post-process.php",
data: dataString,
cache: false,
beforeSend: function(){
$("#error").html("<div class='alert alert-primary bg-primary'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Please Wait, We are processing.</strong> </div>");

$("#postNews").html("<i class='fa fa-spinner fa-pulse'></i><span class='sr-only'>Loading...</span> Posting....");
},
success: function(data){
if(data=="success"){
$("#error").html("<div class='alert alert-success bg-green'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Successfully Inserted</strong> </div>");
}
else{
$("#error").html("<div class='alert bg-red'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Can't Insert</strong> </div>");
}
},

error: function(XMLHttpRequest, textStatus, errorThrown) {
if (XMLHttpRequest.readyState == 4) {
// HTTP error (can be checked by XMLHttpRequest.status and XMLHttpRequest.statusText)

$("#error").html("<div class='alert bg-yellow'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>HTTP Request Error</strong> </div>");

}
else if (XMLHttpRequest.readyState == 0) {
// Network error (i.e. connection refused, access denied due to CORS, etc.)
$("#error").html("<div class='alert bg-yellow'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Please Check Your Network Connection</strong> </div> ");

}
else {
// something weird is happening
$("#error").html("<div class='alert bg-yellow'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Some Error Occured</strong> </div>");
}

}
});
}

});

});
</script>

core/dbconnect.php

<?php
error_reporting( E_ALL & ~E_DEPRECATED & ~E_NOTICE );
if(!mysql_connect("localhost","root",""))
{
die('oops connection problem ! --> '.mysql_error());
}
if(!mysql_select_db("admin"))
{
die('oops database selection problem ! --> '.mysql_error());
}

?>

core/news-post-process.php

<?php
session_start();
include_once 'dbconnect.php';

if(isset($_POST['newsHeading']) && isset($_POST['newsSource']) && isset($_POST['newsContent']))
{
$newsHeading = mysql_real_escape_string($_POST['newsHeading']);
$newsSource = mysql_real_escape_string($_POST['newsSource']);
$newsContent = mysql_real_escape_string($_POST['newsContent']);

/*$newsHeading = trim($newsHeading);
$newsSource = trim($newsSource);
$newsContent = trim($newsContent);*/

$query = mysql_query("INSERT INTO `news`(`news_heading`,`news_source`,`news_content`) VALUES('".$newsHeading."','".$newsSource."','".$newsContent."')");


if ($query) {
echo "Sucess";
}
else{
echo "Failed";

}
}


?>

为什么 jQuery 和 AJAX 不能正常工作??

最佳答案

将您的表单标签更改为此....

     <form class="form-horizontal" role="form" name="newsForm">
</form>

当谈到使用 ajax 发布数据时,您注意到您在 ajax 调用本身中编写方法,例如:

 $.ajax({
method:"post",

});

所以你不需要在表单标签中提到post方法

关于javascript - 无法在 php 中使用 jQuery 和 AJAX 将数据插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37771077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com