从php发送空值到文本文件

时间:2022-09-17 13:32:02

Im trying to send a "rating" value from a php file to a text file. When im checking the console log it says that the value has been sent. Though when im checking the text file nothing has been added. But the text file itself said it was recently updated.

我试图从php文件发送一个“评级”值到文本文件。当我检查控制台日志时,它表示该值已被发送。虽然我在检查文本文件时没有添加任何内容。但是文本文件本身说它最近更新了。

Anyway, here's the code:

无论如何,这是代码:

<form name="Star" id="Star">    
    <div id="rating-area" class="shadow">   

    <img src="star-icon.png" id="thumb1" value="1" onclick="postStar()"/>
    <img src="star-icon.png" id="thumb2" value="2" onclick="postStar()"/>
    <img src="star-icon.png" id="thumb3" value="3" onclick="postStar()"/>
    <img src="star-icon.png" id="thumb4" value="4" onclick="postStar()"/>
    <img src="star-icon.png" id="thumb5" value="5" onclick="postStar()"/>

    </div>


</form>
<script>
function postStar() {
    var StarInput = $("#Star").val();   

    $.post("post.php", 
    {
        Star:StarInput
    },
    function(data, status){
        console.log("Data: " + data + " - Status: " + status);
    }
    );
}

</script>

And the post.php file:

和post.php文件:

<?php
echo "Hittar php-fil";  
if(isset($_POST['Star'])){

    $Star = $_POST['Star'];



    file_put_contents('textfile.txt', $Star . "\n", FILE_APPEND);

echo "Lgger in i txt-fil";  
}
?>

I can't really tell where the problems occurs.

我无法确定问题出在哪里。

1 个解决方案

#1


1  

<form name="Star" id="Star">    
    <div id="rating-area" class="shadow">   

    <img src="star-icon.png" id="thumb1" data-value="1" />
    <img src="star-icon.png" id="thumb2" data-value="2" />
    <img src="star-icon.png" id="thumb3" data-value="3" />
    <img src="star-icon.png" id="thumb4" data-value="4" />
    <img src="star-icon.png" id="thumb5" data-value="5" />

    </div>


</form>
<script>

    // jQuery(document).on('click','div#rating-area img',function(e){
   jQuery('div#rating-area img').click(function(e){
        var val = jQuery(this).data('value') ;
        console.log(val) ;
        jQuery.post('post.php',{ Star : val },function(data,status){
            console.log('data:'+data+'/status'+status) ;
        }) ;
    }) ;

</script>

#1


1  

<form name="Star" id="Star">    
    <div id="rating-area" class="shadow">   

    <img src="star-icon.png" id="thumb1" data-value="1" />
    <img src="star-icon.png" id="thumb2" data-value="2" />
    <img src="star-icon.png" id="thumb3" data-value="3" />
    <img src="star-icon.png" id="thumb4" data-value="4" />
    <img src="star-icon.png" id="thumb5" data-value="5" />

    </div>


</form>
<script>

    // jQuery(document).on('click','div#rating-area img',function(e){
   jQuery('div#rating-area img').click(function(e){
        var val = jQuery(this).data('value') ;
        console.log(val) ;
        jQuery.post('post.php',{ Star : val },function(data,status){
            console.log('data:'+data+'/status'+status) ;
        }) ;
    }) ;

</script>