gpt4 book ai didi

php - 如何使用 jQuery 和 MySQL 在同一页面上提交和显示?

转载 作者:行者123 更新时间:2023-11-30 06:43:37 26 4
gpt4 key购买 nike

目标:

创建一个问答脚本(使用 PHP、JavaScript 和 jQuery),使用户能够提出问题并提交所述问题的答案。如果用户提交了新答案,该答案将被插入到数据库中,并且包含答案的 div 将自动刷新以包含/查看新提交的答案。

问题:

提交答案后,提交过程不工作

这是我的代码:

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script language="JavaScript">
$(document).ready(function ()
{
/*Function # 4:
Hide the AnswerForm and show Answers where the div will be automatically refreshed upon answer submission. <>>>> REVIEW!!! */
function addAnswer(i,qID)
{
//alert("newanswer-q"+i);

//$("newanswer-q"+i).style.display("none");
//$("Answers-q"+i).style.display("block");
changeDiv("Answers-q"+i, "block");

//step # 1: define posted data to insert into database
var name = $("input#name").val();;
var answer = $("input#answer").val();;

alert(name+","+answer);

//step # 2: submit form to be processed by CHANGE.PHP to insert into DB
$.ajax({
type:"POST",
url:"change.php",
data: "questionID="+qID+"&count="+i+"&name="+name+"&answer="+answer,
success: function(data)
{
if(data==0)
{
alert("YEEEEEEEEEESSSS!!!!!! :DDDDDD");
$("#Answer-q"+i).html("Finally!");
}
else
{
$("#Answer-q"+i).html("?!?!");
}
}
});

//Step # 3: refresh Answers div
//changeDiv('Answers-q'+i, 'block');

$("#Answers-q"+i).load("printAnswers.php");
}//end addAnswer


$("#refreshAnswers").click(function(evt){
$("#refreshAnswers").load("printAnswers.php");
evt.preventDefault();
});
}
</script>


<style type="text/css">

.answers
{
background-color: red;
position: relative;
display: block;
left: 1in;
}

.answerform
{
background-color: yellow;
position: relative;
display: block;
left: 1in;
}
.error
{
color: red;
display:none;
}
</style>

</head>
<body>
<?php
mysql_connect("#", "#", "#") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

$q1 = "SELECT *
FROM questions";
$allQ = mysql_query($q1);
while($q = mysql_fetch_array($allQ))
{
$i = $q['qID'];

echo '<div id="questions" style="background-color: blue;">';
echo 'Question: '.$q['Question'].'<br><br>';
echo 'posted by '.$q['userName'].'<br><br>';
echo 'posted on '.$q['addDate'].'<br><br>';
echo '</div>';?>

<input type="button" id="viewAnswers" name="viewAnswers" value="View Answers" onClick="changeDiv('Answers-q<?=$i?>', 'block');">
<input type="button" id="addAnswer" name="addAnswer" value="Answer Question" onClick="changeDiv('newanswer-q<?=$i?>', 'block');">

<div id="Answers-q<?=$i?>" class="answers">
<? include("printAnswers.php"); // display all answers to question # i
?>
</div>

<? echo '<div id="newanswer-q'.$i.'" class="answerform">';
include("addAnswerForm.php"); // display add new answer to question # i
echo '</div>';
} ?>
<br>-------------------------<br>
Go back to <a href="index.php">index.php</a>
</body>
</html>

Change.php

<?php

mysql_connect('#', '#', '#') or die(mysql_error());

mysql_select_db('test') or die(mysql_error());

// Get values from form
$name=$_POST['name'];

$answer=$_POST['answer'];

$qID = $_POST['qID'];

// Insert data into mysql

$sql="INSERT INTO answers(Answer, userName, qID)
VALUES('$answer', '$name','$qID')";

$result=mysql_query($sql);

?>

由于我在 PHP 和 jQuery 方面的初级技能,我已经坚持了几个小时,但运气不佳。

谁能给我一条救生索之类的东西?

最佳答案

数据的值(value)是什么?在成功函数中尝试 console.log(data)。在我看来,change.php 不会产生任何输出,那么为什么数据应该等于零?

关于php - 如何使用 jQuery 和 MySQL 在同一页面上提交和显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8692788/

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