0

Hello I am new to js and I stuck on the problem about passing variables to php via ajax.

 <script>
            $date = "123";
            $.ajax({
                url: './record.php', 
                type: "POST",
                dataType:'text', 
                data: ({'date': date}),
                success: function(data){
                    console.log("successfully");
                }
            }); 
</script>

And below is my code in record.php file.

<?php
session_start();
if(!empty($_POST['date'])){
    //$hello = 'hh';
    $_SESSION['date'] = $_POST['date'];
    echo($_SESSION['date']);
}else{
    echo "its empty(var)";
}

?>

Page always print out "its empty(var)" and I checked console, it shows "successfully"... I already stayed on this problem for several hours and looked for a lot of similar post, but I still can't find where is the problem. Can anyone have a look at it? I also already loaded the jQuery library.

1
  • 2
    That's just a typo. You're setting a variable named $date, but are sending a variable named date. Commented Jul 20, 2017 at 8:21

4 Answers 4

2

you ar mixing js variable with php variable, js variables are declared with var and php variables are declared with $fieldname

<script>
            var date = "123";
            $.ajax({
                url: './record.php', 
                type: "POST",
                dataType:'text', 
                data: {'date': date},
                success: function(data){
                    console.log("successfully");
                }
            }); 
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

Does the var statement matter in a non-strict environment? --- Ah. It does when the second date doesn't match the first...
yes because or else how will it understand its a js variable or not
forgot the ; after it, try now
@Exprator of course it does jsbin.com/rudomihifu/edit?html,js,output
You're correct though, the OP was passing an undefined date variable, because they defined it as $date.
|
0

if you want a PHP variable insite javascript you should define php variable first then asign it into js.

<?php $date = "123"; ?>';

<script>

            var date=<?php echo $date; ?>';
            $.ajax({
                url: './record.php', 
                type: "POST",
                dataType:'text', 
                data: ({'date': date}),
                success: function(data){
                    console.log("successfully");
                }
            }); 
</script>

Hope it wil helps you.

Comments

0

You cant mix server (php) and client (js)

you might to this

<?php $var = 0?>

then in html you create

<input id="something" type="hidden" value="<?php echo $var?>">

then you get that value via

document.getElementById("something").value;

Comments

0

You are declaring $date and pass in your ajax as date.

Try this:

<script>
        var date = "123";
        $.ajax({
            url: './record.php', 
            type: "POST",
            dataType:'text', 
            data: {'date': date},
            success: function(data){
                console.log("successfully");
            }
        }); 
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.