通过Ajax将JavaScript函数传递给PHP文件
Pass JavaScript function through Ajax to PHP file
我要做的事情:
我正在编写一个脚本,旨在为用户提供选择,并根据他们做出的选择,提出一个新的选择。为此,我制作了两个脚本,一个用于HTML和页面结构,另一个用于PHP(从服务器获取信息)和JavaScript(构建生成的选项)。
然后我使用AJAX在这两个脚本之间进行通信,或者至少,这是我试图做的,但我对AJAX非常陌生,我无法使其工作。
当按下3个按钮中的一个时,我正试图从页面传递或以某种方式启动函数"generateProblems",最好使用参数"id" 这是我的部分索引.html: 这是我的PHP/Javascript: 我做错了什么?在我将其拆分之前,代码运行得很好,但现在单击其中一个按钮不会产生任何结果。 <div id="testpile" class="inner cover">
<div id="buttons">
<p><a id="rat" class="btn btn-default" role="button">Rationel</a></p>
<p><a id="emo" class="btn btn-default" role="button">Emotionel</a></p>
<p><a id="per" class="btn btn-default" role="button">Personel</a></p>
</div>
</div>
<div id="testdrop" class="mastfoot">
<p>Drop numbers here</p>
</div>
<script type="text/javascript">
$("#buttons").find(".btn").click(function() {
var id = this.id;
$("#testpile").load("include/responseget.php");
$.post("include/responseget.php", {
choice: "id",
});
});
</script>
<?php include 'login.php';
//Query til at finde information til generation af question
$stmt = $conn->prepare("SELECT DISTINCT ResponseTitle, ResponseText FROM response Limit 8;");
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc())
{
$rTitle_array[] = $row['ResponseTitle'];
$rText_array[] = $row['ResponseText'];
}
json_encode($rTitle_array);
json_encode($rText_array);
// close connection
mysqli_close($conn);
?>
<script type="text/javascript">
$(function() {
var phase = 0;
var rTitle = <?php echo json_encode($rTitle_array); ?>;
var rText = <?php echo json_encode($rText_array); ?>;
function generateProblems(param) {
var problemDef = param;
$("#buttons").hide();
var numbers = [1, 2, 3, 4, 5, 6, 7, 8];
for (var i = 0; i < 8; i++) {
$('<div>' + rTitle[i] + '</div>').data('number', rTitle[i]).attr('id', 'problem' + rTitle[i]).appendTo('#testpile').draggable({
containment: '.site-wrapper',
stack: '#testpile div',
cursor: 'move',
revert: true
});
$('<div>' + rText[i] + '</div>').data('number', rText[i]).attr('id', 'problem' + rText[i]).appendTo('#testpile');
}
$('#testdrop').droppable({
drop: handleDropEvent,
accept: '#testpile div'
});
function handleDropEvent(event, ui) {
var problemNumber = ui.draggable.data('number');
var problemCombination = problemDef + problemNumber;
ui.draggable.draggable('disable');
ui.draggable.draggable('option', 'revert', false);
phase++;
alert('ProblemCombination is "' + problemCombination + '", phase is "' + phase + '" ');
$("#testpile").children().hide();
generateProblems(problemCombination);
}
}
});
</script>
这不是正确的做法。让php远离html/js。你不能从另一个没有直接包含在其中的脚本访问你的html。
- 将您的javascript部分重新添加到index.html
- 在php脚本返回中像
return json_encode($rTitle_array);
这样的结尾 - 在您的jquery/ajaxpost,获取返回值并使用它
jquery文档中的示例:
var posting = $.post( url, { s: term } );
// Put the results in a div
posting.done(function( data ) {
var content = $( data ).find( "#content" );
$( "#result" ).empty().append( content );
});
- 如何在php文件中获取$.post-ajax传递的值
- Javascript运行php文件,然后下载文件
- 通过Ajax将JavaScript函数传递给PHP文件
- 我想使用模态通过php文件发送邮件,并且我希望在提交关闭后关闭pop
- 通过ajax到php文件获取单选按钮值
- 每天用javascript包含不同的php文件
- 如何使用javascript调用Php文件
- 如何使用php文件中的GET来获取我在.js文件中声明的变量
- 在CodeIgniter视图中将Javascript或jQuery代码作为PHP文件编写可以吗
- 从表单中动态生成一个字符串,传递给通过AJAX加载的PHP文件
- 包括php文件和几个js文件
- 将JavaScript变量传递到HTML输入框和(在PHP文件中使用)
- PHP文件没有't从Javascript应用程序获取$_POST
- 如何显示由ajax调用运行的PHP文件的echo
- 如何将php变量从php文件传递到js文件
- 如何使用javascript/jquery将变量从一个PHP文件传递到另一个PHP文件
- 在同一主机上对PHP文件使用XMLHTTP请求的Javascript违反了同源策略
- 通过Wordpress中的jquery调用运行php文件
- 通过Javascript调用php文件
- 如果($_SERVER[“REQUEST_METHOD”]=“POST”)条件在ajax调用不同的php文件进行验证和提