0

i'm using a onchange function to post value to ajax.

function viewTest() {
    jQuery("#empy_card").hide()
    var test_type= jQuery('#test_type').val();
    var myvals = jQuery("#table").html('');                                 
    jQuery.ajax({
        type: "POST",
        url: "test.php?smt=command",
        dataType: 'json',
        data: {test_type:test_type},
        success: function(data){
            //alert(data.date);
            //alert(data.book);
        <?php 
        $date = data.date;
        $book = data.book;
        ?>
        }
    });
}

its work successfully. i got in alert form data.date

"18-Dec-10","20-Apr-11"

i got in alert form data.book

"book A","book C"

test.php

<?php
if(isset($_GET['smt'])=='command'){
    $test= $_POST['book_name'];
    include('connection.php');
    $data=array();
    $result ="SELECT * FROM books WHERE Type_of_books='$test'";
    $ftc = mysqli_query($conn,$result)or die(mysqli_error($conn));
    while($row = mysqli_fetch_array($ftc)) {    
        $data['date'][]  ='"'.$row['date'].'"';
        $data['book'][]  = $row['name'];
    }
    echo json_encode($data);
}
?>

how can i get this data to php variable like $dates = data.date; And $book = data.book

executable
  • 3,365
  • 6
  • 24
  • 52
kealaxshan
  • 338
  • 2
  • 14
  • 1
    You already got them in your `test.php`?! – brombeer Oct 08 '18 at 12:04
  • 2
    I'm really not sure what you're trying to do here, or how this code even does anything in response to the ajax. (Or fails to throw a PHP error, because `$date = ?` looks like invalid PHP to me.) But the PHP runs on the server side, either when the initial page loads, or in response to the Ajax call (this is when your `test.php` runs). To give `test.php` the values submitted in `data`, you just use the appropriate `$_POST` properties in `test.php`. – Robin Zigmond Oct 08 '18 at 12:05
  • i want it in index.php – kealaxshan Oct 08 '18 at 12:06

2 Answers2

-1

Remember that your PHP code is evaluated by the server, not the client. That means that you can access your $date and $year in various ways.

  1. Server side (PHP)

You have to delete your Ajax request and write it using PHP, then you can access the variables.

  1. Client side (JavaScript)

You already got them in data!

hearot
  • 124
  • 9
-1

Why are you using php in Ajax and you are assigning JavaScript variable to php. If you really want that response use cookies or session variables.

saketh
  • 317
  • 2
  • 10