Jump to content

When clicked, the value can not pass to input element without reload page.


kit123321
 Share

Recommended Posts

when i click button .btnStart, i would like to INPUT class="stime" to have value $resp['start'] without page reload. but not work. any help?

2022-08-11_084035.thumb.jpg.dc7b103d3d7e3b8664799774b540baaa.jpg

JS code

        $(".btnStart").click( function() {
            var tid = $(this).data("tid")
            $.get(
                "",
                {"ajax":"startBooking", "tid":tid},
                function(resp) {
                    $(".sdate[data-tid="+tid+"]").val(resp.sdate).show()
                    $(".stime[data-tid="+tid+"]").val(resp.start).show()
                    $(".bid[data-tid="+tid+"]").val(resp.bid)
                    
                },
                "JSON"
            )
            $(this).hide()
            $(".btnFinish[data-tid="+tid+"]").show()

        })

PHP Handle AJAX

##   Handle AJAX requests  
    if ($_GET['ajax'] == 'startBooking') {
        $dt = new DateTime('now');
        $start = $dt->format('Y-m-d H:i:s');
        $st = $dt->format('H:i');
        $sd = $dt->format('Y-m-d');
        $stmt = $pdo->prepare("INSERT INTO table_booking (table_id, start_time) VALUES (?, ?)");
        $stmt->execute( [ $_GET['tid'], $start ] );
        $bid = $pdo->lastInsertId();
        $resp = [ "bid" => $bid,
                  "start" => $st,
                  "sdate" => $sd
                ];
        exit( json_encode($resp));
    }

HTML

<input type='time' class='stime' value='$st' data-tid='{$r['tid']}'>

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.