pythonic
pythonic

Reputation: 21625

Passing of variable from Jquery to PHP

I am trying to get the server show the client's date and time, but that is not showing the correct output. Here is the relevant part of the code:

<script type="text/javascript">
  $(function(){
    var d = new Date();
    var dateStr = d.toString()
    $.post(window.location, {
        dateStr: dateStr
    });
    alert(dateStr);
});
</script>

<div id="divMessage">
<?php 
    $dstr = 'nothing!';
    if(isset($_POST["dateStr"]) && strlen(trim($_POST["dateStr"])) > 0)
        $dstr = $_POST["dateStr"];
    $v = 'current date/time is '.$dstr;
    echo "<span style=\"color:green\">$v</span>";
?> 
</div>

If the code is correct, I should see "current date time is <client's date/time>", but instead I see "current date time is nothing!". What mistake am I doing here?

Upvotes: 0

Views: 58

Answers (3)

Sunil
Sunil

Reputation: 155

Hope this helps, there were several things you needed to add to make it work.

Checking if the page was submitted via post and then parsing the response for the message to redisplay it.

<script type="text/javascript">
  $(function(){
    var d = new Date();
    var dateStr = d.toString();
    $.post(window.location, {
        dateStr: dateStr
    }).success(function(data){
        var res = $(data).filter('#divMessage').text();
        //console.log(res);
        $('#divMessage').replaceWith(res);
    });

});
</script>


<div id="divMessage">
<?php
    //include "simple_html_dom.php";
    //$html = new simple_html_dom();
    //$html->load_file('index2.php');
    //$v = $html->find('div[id=box]', 0)->plaintext;

    if (!empty($_POST['dateStr'])) {
        $dstr = 'nothing!';
        if (isset($_POST["dateStr"]) && strlen(trim($_POST["dateStr"])) > 0) {
            $dstr = $_POST["dateStr"];
        }
        $v2       = 'current date/time is ' . $dstr;
        echo "<span style=\"color:green\">$v2</span>";
    }
?>
</div>

Upvotes: 1

Marko Mackic
Marko Mackic

Reputation: 2333

Here is what you're missing, see what's data returned from server :)

<script type="text/javascript">
$(document).ready(function(){
        var d = new Date();
        var returnFromServer = 
        var dateStr = d.toString()
        $.post(window.location, {
            dateStr: dateStr
        }).success(function(data){
          //this is return from server
          alert(data);
    });
        alert(dateStr);
});

</script>

<div id="divMessage">
<?php 
    $dstr = 'nothing!';
    if(isset($_POST["dateStr"]) && strlen(trim($_POST["dateStr"])) > 0)
        $dstr = $_POST["dateStr"];
    $v = 'current date/time is '.$dstr;
    echo "<span style=\"color:green\">$v</span>";
?> 
</div>

Anyways you need to split files up , because I think this will return the whole page, and the span with your color :)

Upvotes: 1

Daebak
Daebak

Reputation: 419

As other said your have to use 2 file one with the js and other for php, in file.php you can make what you want for example to save in db. I tried to comment the code if you don't understand something feel free to ask.

check the path where you save the file php

file.php

<?php

$result = Array();

if (isset($_POST['action'])) {
    $client_date = new Date();

    // HERE YOU CAN USE FOR SAVE IN DB
    $result ['result_msg'] = 'success';
    $result ['client_date'] = $client_date;
} else {
    $result ['result_msg'] = 'error';
}
echo json_encode($result);

?>

html

$(document).ready(function(){
    $.ajax({        
        type: "POST",
        url: "file.php",
        data: { 
            action : 'what you want'
            // IF YOU WANNA SAVE CLIENT INFORMATION HAVE TO MAKE A FORM AND PASS DATA HERE FOR EXAMPLE CLIENT ID ....
        },
        dataType: 'json'
    }).done (function(result) {
        result = JSON.parse(JSON.stringify(result));
        if (result.result_msg === 'success') {
            console.log(result.client_date); // YOU CAN SHOW IN HTML DIV
        } else {
            console.log('ERROR');
        }

    }).fail(function(result) {

        console.log('ERROR');

    });
});

Cheers!!!

Upvotes: 1

Related Questions