簡體   English   中英

從Javascript向PHP發送變量,無法正常工作

[英]Sending variable to php from Javascript, not working

我試圖通過單擊使圖像更改為使用PHP從文本文件中收集的值(文件名)。 這是通過將文件收集為數組( data )並在計數器( counter )的幫助下回顯數組中的正確項來完成的。 使用Javascript每次單擊時,此計數器都會增加,但是我想將此增加的變量返回給PHP,以便能夠更改圖像。 有沒有辦法做到這一點? 到目前為止,這是我的嘗試,並嘗試發布變量(請參閱下面的完整代碼):

<script>
var variableToSend = JSON.parse(localStorage.getItem('counter'));
$.post('lan7_old.php', {variable: variableToSend});
</script>

<?php $counter = $_POST['variable'];
echo $counter; ?

我認為應該在單擊圖像時刷新頁面來發布,但是在這里我可能會誤會。 還是我的代碼還有其他問題? 所顯示的圖像仍然是文本文件中的第一個圖像,但這僅是因為我在PHP echo添加了整數來counter -PHP counter變量沒有設置任何值。

<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" href="styles.css">
<script src="jquery-3.1.1.min.js"></script>
</head>
<body>

<script>
// Saves counter variable although the page is refreshed
$(document).ready(function(){
    if (localStorage.getItem('counter') === null) {
        localStorage.setItem('counter', JSON.stringify(0));
    }
    // Incrementing counter until next page is needed
    $("img").click(function(){
        var counter = JSON.parse(localStorage.getItem('counter'));
        if(counter < 60) {
            counter += 4;
            localStorage.setItem('counter', JSON.stringify(counter));
        }
        else {
            localStorage.setItem('counter', JSON.stringify(0));
            window.location.href = '8.php';
            return false;
        }
        $("#test").text(localStorage.getItem('counter')); //for testing

        // sending js variable to be obtained by PHP
        var variableToSend = JSON.parse(localStorage.getItem('counter'));
        $.post('lan7_old.php', {variable: variableToSend});
    });
    $("#test").text(localStorage.getItem('counter'));
});
</script>

<div class="main">
    <h1>Heading</h1>
    <p>Heading 2</p>
    <div id="test">hello</div>

    <?php $data = getData($subtest_nr);
    // retrieving counter variable
    $counter = $_POST['variable'];
    echo $counter; //for testing ?> 

    <form id="myform" method="post">
    <div class="four_images">
            <div class="flex-item">
                <input type="radio" name="image" value="7.11" id="alt1" class="hidden">
                <label for="alt1"><img src="images/<?php echo $data[$counter+0]; ?>"></label>
            </div>
            <div class="flex-item">
                <input type="radio" name="image" value="7.12" id="alt2" class="hidden">
                <label for="alt2"><img src="images/<?php echo $data[$counter + 1]; ?>"></label>
            </div>
            <div class="flex-item">
                <input type="radio" name="image" value="7.13" id="alt3" class="hidden">
                <label for="alt3"><img src="images/<?php echo $data[$counter+2]; ?>"></label>
            </div>
            <div class="flex-item">
                <input type="radio" name="image" value="7.14" id="alt4" class="hidden">
                <label for="alt4"><img src="images/<?php echo $data[$counter+3]; ?>"></label>
            </div>
            <div>
                <input type="submit" name="save" value="Save Image Selection">
            </div>
    </div>
    </form>
</div>

</body>
</html>

僅當計數器達到60或更高(window.location.href ='8.php¨)時,頁面才會刷新。

Php僅呈現服務器端,現在您正在執行javascript發布請求,但忽略了響應,因此此操作無濟於事。

您要么需要使php返回一個響應,然后您就可以通過javascript使用它來更新頁面(DOM樹),或者您應該使用例如window.location.href =“ index.php?counter =” +計數器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM