0

I need a simple way to retrieve multiple PHP variables into html divs. I searched a lot of posts but I can't found an answer.

I am looking for something like this:

go-to-index.php

<?php
    $name = 'Jonh';
    $phone = '123456789';
    $details = 'Detail about';
?>

index.php

<div class="name">Your Name is : <?php echo $name; ?></div>
<div class="phone">Your Phone Number is : <?php echo $phone; ?></div>
<div class="details">Your Details are : <?php echo $details; ?></div>

I want instead of echo to get them via AJAX Call.

What is the correct AJAX REQUEST syntax to do that?

UPDATE

My bad I do not noticed before but forgot to say I also need to load the calls one by one. I have too many requests and take a lot of time.

May the query .each() function should work like I want?

5
  • Make an AJAX request to go-to-index.php from index.php. The go-to page will need to output the content. Commented Aug 25, 2015 at 23:03
  • Thanks for your reply. I understand what you say but I need a litle help. I don't know the correct syntax of code. May you can give me an example code. Thanks Commented Aug 25, 2015 at 23:06
  • There is a doc here on how to do it, api.jquery.com/jquery.ajax. It isn't a single line thing. You'll probably want to return a JSON object and then parse through it on the client to get your individual values....or you could just have the PHP build your 3 divs and then just inject that into your page.. Commented Aug 25, 2015 at 23:08
  • Yes exaclty I want to return JSON object but I do not know how to do, I have already looked at the link you have send me. I will try to make the code myself but is so difficult. Commented Aug 25, 2015 at 23:11
  • Try out echo json_encode(array("name"=> 'Jonh', 'phone' => '123456789', 'details' => 'Detail about'));. Commented Aug 25, 2015 at 23:16

2 Answers 2

1

In your PHP:

<?php
echo json_encode(Array(
    'name' => "John",
    'phone' => "1234567890",
    'details' => "Details about..."
));

Your HTML:

<div class="name">Your Name is : <span class="name_value"></span></div>
<div class="phone">Your Phone Number is : <span class="phone_value"></span></div>
<div class="details">Your Details are : <span class="details_value"></span></div>

Your jQuery:

$(document).ready(function(){
    $.getJSON('user-info.php',function(data){
        $(".name_value").html(data.name);
        $(".phone_value").html(data.phone);
        $(".details_value").html(data.details);
    });
});

Note: you'll set the user-info.php string to the URL (relative or absolute) of your PHP script that grabs the user info.

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

3 Comments

Working perfect, but I want to do something like and is not working. $content = "THIS IS A GOOD QUESTION" if(preg_match('[QUESTION]', $content)) { $conform = "Found"; }else{ $conform = "Not Found"; } echo json_encode(Array( 'name' => $conform, 'phone' => "1234567890", 'details' => "Details about..." ));
You want their name to be "Found" or "Not Found"?? How about we add the indicator of if it's a question into its own field.. Here's that: $is_question = ( stripos( $content, 'question' ) ? 1 : 0 ); echo json_encode( Array( 'name' => "John", 'phone' => "1234567890", 'details' => "Details about...", 'is_question' => $is_question ) );
Hello, do you know how can I make it to load the requests one by one and not waiting for all ?
0

You need a PHP script that will output JSON containing the values you want, and you need a Javascript handler to ask for that data and do something when it gets it. Here's an example:

# File: go-to-index.php
<?php

$name = 'Jonh';
$phone = '123456789';
$details = 'Detail about';

echo json_encode(
    [
        'name' => $name,
        'phone' => $phone,
        'details' => $details
    ]
);

Then your HTML page:

<!-- File: index.php -->
<div class="name">Your Name is : <span class="container"></span></div>
<div class="phone">Your Phone Number is : <span class="container"></span></div>
<div class="details">Your Details are : <span class="container"></span></div>

<button class="loadMe" type="button">Click here to make things work</button>

And finally your jQuery:

$(document).ready(function() {
    $('.loadMe').click(function() {
        $.ajax({
            // Path to your backend handler script
            url: 'go-to-index.php';
            // Tell jQuery that you expect JSON response
            dataType: 'json',
            // Define what should happen once the data is received
            success: function (result) {
                $('.name .container').html(result.name);
                $('.phone .container').html(result.phone);
                $('.details .container').html(result.details);
            },
            // Handle errors in retrieving the data
            error: function (result) {
                alert('Your AJAX request didn\'t work. Debug time!');
            }
        });
    });
});

You can do this on any event - the button was just an example. You can also use plain Javascript or any other library, just used jQuery since you tagged it in your question.

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.