即使下次访问时,单击制作按钮也会消失

时间:2014-09-02 22:29:02

标签: javascript php jquery

我有一个问题,我正在制作一个带有按钮的网站。 问题是我只希望访问该网站的用户按下一次。 因此,当用户刷新或返回时,再次按下它是不可能的。

我认为我应该将PHP与AJAX结合使用,但不幸的是我还不够好。希望有人能帮助我朝着正确的方向前进:)

html代码:

    <form action="" method="POST">

    <input id="banana_button" type="submit" name="button_press" value="Add banana's!">
</form>

javascript:

$('input[name="button_press"').click(function(){

        $('#banana_counter_text').append($banana+1);
        $(this).attr('disabled',true).css('width','180px').attr('value','thanks for adding a banana');
    });

我想得到的是,当他们下次访问时按下按钮时,他们不会看到按钮。 有可能,我怎么能做到最好。我希望你们能帮助我

更新

我正在尝试最近几天来解决这个问题,我让它运转了。 我正在使用带有自定义名称的session_cookie。每次加载网站时,它会检查该cookie是否可用,如果不是它将显示该按钮。否则它将删除整个按钮。

代码如下所示:

PHP

//this is what checks everytime if the cookie is set
    if (isset($_COOKIE['banana_added'])) {
        //code for disabling the button 
    }

//this is what makes the cookie and checks if the submit button is pressed. 
//because people where wondering how the data is stored. It is just a simple XML file
    if(isset($_POST['buttonsubmit'])){
        /*when submit is pressed */
        session_name("banana_added");
        session_start();
        $xml = simplexml_load_file('../banana.xml');
        $prev_count = $xml->banana_count;
        $new_count = $prev_count +$_POST['buttonsubmit'];
        echo $prev_count;
        echo $new_count;
        $xml->banana_count = $new_count;
        file_put_contents('../banana.xml', $xml->asXML());
        header("location:../landing.php");

谢谢大家的帮助。我不知道如何关闭这个帖子? 这是针对此特定问题的解决方案

2 个答案:

答案 0 :(得分:0)

为了记住这一变化,您应该将该值放在COOKIE / SESSION或DATABASE中。第二种变体是优先的。 所以,举个例子,你可以说你有一个叫做按钮的表,在里面你有一个列&#39;已经检查过&#39;使用tinyint类型(接受值0和1),默认值为0。

在你的HTML中你应该这样做:

<button <?php ($thisButtonQuery->checked == 'checked')? 'disabled' : '';?>>button text </button>

问候,我希望我解决你的问题! :)

答案 1 :(得分:0)

我正在尝试最近几天来解决这个问题,我让它运转了。我正在使用带有自定义名称的session_cookie。每次加载网站时,它会检查该cookie是否可用,如果不是它将显示该按钮。否则它将删除整个按钮。

代码如下所示:

PHP

//this is what checks everytime if the cookie is set
    if (isset($_COOKIE['banana_added'])) {
        //code for disabling the button 
    }

//this is what makes the cookie and checks if the submit button is pressed. 
//because people where wondering how the data is stored. It is just a simple XML file
    if(isset($_POST['buttonsubmit'])){
        /*when submit is pressed */
        session_name("banana_added");
        session_start();
        $xml = simplexml_load_file('../banana.xml');
        $prev_count = $xml->banana_count;
        $new_count = $prev_count +$_POST['buttonsubmit'];
        echo $prev_count;
        echo $new_count;
        $xml->banana_count = $new_count;
        file_put_contents('../banana.xml', $xml->asXML());
        header("location:../landing.php");

谢谢大家的帮助。 这是解决这个特殊问题的方法