0

I would like to know how to get a PHP array field value from a PHP file using jquery/Ajax. In fact, I have three files: mapage.html , moslem.php and moslem1.php . You find below the code of each file.

The code of mapage.html:

<script type="text/javascript">
    $(document).ready(function() {
        setTimeout( test, 1000);
    });
    function test() {
        $.ajax({
            url: 'moslem.php',
            type: 'POST',
            success: function(data) {
                var max;
                max = data;
                setTimeout( test1, 500);
                function test1() {
                    $.ajax({
                        url: 'moslem1.php',
                        type: 'POST',
                        success: function(data1) {
                            document.write(data1+"<br>");
                        }
                    });
                }
            }
        });
        setTimeout( test, 1000);
    }

The code of moslem.php :

    <?php
$con=mysqli_connect("localhost","root","","test");
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM jqcalendar ORDER BY id DESC LIMIT 0, 1");
while($row = mysqli_fetch_array($result)) {
    $max=$row['Id'];
}
echo $max;
mysqli_close($con);
?>

The code of moslem1.php :

    <?php
$con = mysqli_connect("localhost", "root", "", "test");
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM jqcalendar ORDER BY id DESC LIMIT 0, 1");
while ($row = mysqli_fetch_array($result)) {
    $notification = array('id' => $row['Id'], 'subject' => $row['Subject'], 'location' => $row['Location'], 'description' => $row['Description'], 'starttime' => $row['StartTime'], 'endtime' => $row['EndTime']);
}
print_r($notification);
mysqli_close($con);
?>

Well, when I run the file mapage.html, it displays the below line every one second :

Array ( [id] => 22 [subject] => reading books [location] => at home [description] => reading some books. [starttime] => 2014-05-21 11:00:00 [endtime] => 2014-05-21 12:00:00 ) 

So as you can notice above it displays the content of the array "notification" which exist in the code of moslem1.php. Now, my question is how can I modify the line below to display just a field value of that array (for example the value of the filed "subject" or any other field):

document.write(data1+"<br>");

Thanks in advance.

3
  • use php array and json Commented May 20, 2014 at 10:13
  • You need to convert the PHP array to json array at server and send the json to javascript Commented May 20, 2014 at 10:14
  • use echo json_encode($max); to deliver an json object which you can simply access in javascript. Commented May 20, 2014 at 10:15

1 Answer 1

1

Add dataType:'JSON', to your Ajax requests and change print_r($notification); to echo json_encode($notification); in moslem1.php then you will be able to access it as document.write(data1.subject);

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot my friend :) ... It works!!...I will mark your answer as the best one :)

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.