简体   繁体   English

我如何使用jquery ajax方法访问php页面中动态变化的变量到其他html页面?

[英]How can i access a dynamically changing variable in php page to other html page using jquery ajax method?

This is php page which echoes variable $correct . 这是php页面,它回显变量$correct Its initial value is 0 . 其初始值为0 It is changing with increment in its value but when i load this value after echoing it to other page using jquery ajax method it returns only 0 not the increment value. 它随着其值的增量而变化,但是当我使用jquery ajax方法将其回显到其他页面后加载此值时,它仅返回0而不是增量值。

<?php

$i=1;
while($i<=10) {

    $answer="answer_".$i;
    ${"answer_$i"}=$_POST[$answer];
    $i++;
}

$correct=0;

if($answer_1=="a")
$correct=$correct+10;
else {
    if($answer_1=="b" || $answer_1=="c" || $answer_1=="d")
        $correct=$correct-10;

}
if($answer_2=="a")
$correct=$correct+10;
else {
if($answer_2=="b" || $answer_2=="c" || $answer_2=="d")
            $correct=$correct-10;
}
if($answer_3=="a")
$correct=$correct+10;
else {
    if($answer_3=="b" || $answer_3=="c" || $answer_3=="d")
        $correct=$correct-10;
}
if($answer_4=="a")
$correct=$correct+10;
else
{   if($answer_4=="b" || $answer_4=="c" || $answer_4=="d")
        $correct=$correct-10;
}
if($answer_5=="a")
$correct=$correct+10;
else
{   if($answer_5=="b" || $answer_5=="c" || $answer_5=="d")
        $correct=$correct-10;
}
if($answer_6=="a")
$correct=$correct+10;
else
{   if($answer_6=="b" || $answer_6=="c" || $answer_6=="d")
        $correct=$correct-10;
}
if($answer_7=="a")
$correct=$correct+10;
else
{   if($answer_7=="b" || $answer_7=="c" || $answer_7=="d")
        $correct=$correct-10;
}
if($answer_8=="a")
$correct=$correct+10;
else {
    if($answer_8=="b" || $answer_8=="c" || $answer_8=="d")
        $correct=$correct-10;
}
if($answer_9=="a")
$correct=$correct+10;
else
{
    if($answer_9=="b" || $answer_9=="c" || $answer_9=="d")
        $correct=$correct-10;
}
if($answer_10=="a")
$correct=$correct+10;
else
{
    if($answer_10=="b" || $answer_10=="c" || $answer_10=="d")
        $correct=$correct-10;
}
echo "$correct";
?>

This is script on other page which access this variable $correct but it always result in 0 ,though the variable value is changing on the php page? 这是其他页面上的脚本,访问此变量$correct但始终会导致0 ,尽管变量值在php页面上正在更改? How can i correct this? 我该如何纠正?

<script>
$(function(){
$('form').submit(function(event){
event.preventDefault();
$.ajax({
url:"results.php",
type:"POST",
success:function(result){
console.log(result);
$('#results').html("<p>"+result+"</p>");
}
});
});
});
</script>               

You're not passing any answers from AJAX to PHP. 您没有将任何答案从AJAX传递到PHP。
Therefore none of your conditions in the PHP are met. 因此,您的PHP条件都不满足。
Thus it returns 0. 因此它返回0。

To pass data with the post request consult the ajax documentation (http://api.jquery.com/jQuery.ajax/). 要通过发布请求传递数据,请查阅ajax文档(http://api.jquery.com/jQuery.ajax/)。 You'll want to look at the data attribute. 您将要查看data属性。

You'll want to have something like this: 您将需要这样的东西:

$.ajax({
    url:"results.php",
    type:"POST",
    data: {
        answer_1: 'blue'    // what colour is the sky?
    },
    success:function(result){
        console.log(result);
        $('#results').html("<p>"+result+"</p>");
    }
});

Then your PHP needs to collect the data but I imagine you already have that setup: 然后,您的PHP需要收集数据,但我想您已经有了该设置:

$answer1 = $_POST['answer1'];


Update: to include example information from user comment. 更新:包括来自用户评论的示例信息。

 <input type="radio" name="answer_1" value="a" id="question_1_answer_a"/> <label for="question_1_answer_a">anirudh</label> <input type="radio" name="answer_1" value="b" id="question_1_answer_b"/> <label for="question_1_answer_b">anirudh</label> 

So you'd want something like this: 所以你想要这样的东西:

 data: { answer_1: $('input[name="answer_1"]').val(), // from HTML example answer_2: $('input[name="answer_2"]').val(), answer_3: $('input[name="answer_3"]').val() // etc }, 

If you're not planning on modifying any of the form data with jQuery you could always just serialize it and post that instead: http://api.jquery.com/serialize/ 如果您不打算使用jQuery修改任何表单数据,则始终可以序列化并将其发布: http : //api.jquery.com/serialize/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用ajax jquery将变量传递到php页面? - pass variable to php page using ajax jquery? 如何使用jquery ajax进程访问单独的PHP页面中的函数? - How to access function in separate PHP page using jquery ajax process? 我可以在使用 jquery ajax 的表单提交上同时调用同一 php 页面上的方法,也可以同时调用另一个 php 代码中的方法吗? - Can i call a method on the same php page also a method in another php code simultaneously on form submit using jquery ajax? WooCommerce - 如何使用 ajax 动态加载结账页面? - WooCommerce - how can I dynamically load checkout page using ajax? 使用jquery ajax将php页面输出加载到html div中 - load php page page output into html div using jquery ajax 如何从html / jquery页面的页脚中的会话中打印php变量? - How can I print a php variable from a session in the footer of an html/jquery page? 如何使用ajax将参数从page1.html传递到server.php并将结果返回给page2.php? - How can I pass parameters from page1.html to server.php and return results to page2.php using ajax? 如何使用PHP动态生成HTML页面? - How to generate a HTML page dynamically using PHP? 如何使用php访问不同页面中的会话变量? - How can I access my session variable in a different page with php? 如何使用PHP AJAX Jquery将javascript警报和消息传递到主页? - How can I pass the javascript alert and message into the main page using PHP AJAX Jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM