1

I'm trying to use the value sent by the AJAX request and echo it on a page. Here is how I did the AJAX on the first page (account.php):

account.php

$(document).on('click', 'div#demo', function(){
    var id = $(this).attr('data-id');
    $.ajax({
        url: 'view.php',
        type: 'get',
        data: {id : id},
        success: function(data) {
            console.log(data);
        }
    });
});

Note: From account.php the user is navigated away when she/he clicks on an a href tag. That a href tag actually holds the unique ID what I use to pass to view.php within a div with ID of #ad. Also I'm using Bootstrap that's why I pass data-id = value.

I'm sure the AJAX works perfectly as the Console (F12) shows me that the request has been sent and the value what I used also got passed and the HTML uses it but it does not appear on the actual page so I can only see it in the browser's console. I found similar questions to mine but there were no exact answers to solve the problem. The other file looks like this:

view.php

<?php 
require_once 'config.php';

$id = $_GET['id'];
$geturl = $conn->query("SELECT title, url FROM table1 WHERE id='$id'");
$url = mysqli_fetch_row($geturl);

$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <div class="container">
        <iframe class="embed-responsive-item" src="<?php echo ($url[1]); ?>"></iframe>

    </div>
</body>
</html>

So I would like to place an unique URL into an iframe after I get the value passed by AJAX and use that variable to do my SQL SELECT statements. After it succeed (because it does as it shows in the Console) I assign the SQL result-set to a variable and use that to give the value of the iframe. But if I click on an a href from account.php I'm navigated to view.php and I see only a blank iframe meanwhile the Console exactly shows that the iframe has the source what I want it to have.

I tried json_encode with the str_replace function but it does not work. I have also tried mixing encode and decode like

$enc = json_encode($row[1]);
$dec = json_decode($row[1]);
// and set the iframe src to $dec but still does not work.

Thanks for suggestions, appreciate your time!

UPDATE: I do not want to callback a function from the view.php, so I don't want to set the success: function(response) in AJAX to have a response from the sent request. I only want to use the passed variable on the view.php page, actually to echo it out after I ran my SQL stuff.

  • Turn on error reporting and look for errors that might have appeared – Saurabh Aug 16 '16 at 12:55
  • You are using GET to pass data. Send data in URL 'view.php?id='+id – user1885057 Aug 16 '16 at 12:58
  • 1
    if I read it correctly, it sounds like there are two requests to view.php, one via AJAX and one when the user clicks on a link? If so, anything you passed in the first request will not be present when you make the second request - remember HTTP is stateless. You've got two entirely separate requests and you're expecting data from one request to be available when you make the second request. I think you only need one request, and pass the id on the querystring during that request (whichever is appropriate) – ADyson Aug 16 '16 at 13:09
  • As I said, in account.php use document.location.href either with view,php?id= and an iframe, or directly with an url returned by view,.php – Adder Aug 16 '16 at 13:37
  • There something that works like you may have in mind, read about session variables in http://stackoverflow.com/questions/871858/php-pass-variable-to-next-page?rq=1 – Adder Aug 16 '16 at 14:22
  • @KrisztiánTóth see also my comment on the answers below. I don't think you grasp that you're not sending data "to the page", you're sending it to the server, which then returns some content to the user. You're sending two separate requests to the server. The first is via ajax and contains the ID. It returns that HTML to you and you see it the console. But that data _doesn't_ persist on the server after that. So when you click on the link, it makes a new request and forgets what happened in the ajax, it returns the HTML again (this time as a full webpage) but has no "id" data to work with... – ADyson Aug 16 '16 at 14:42
  • 1
    @KrisztiánTóth ...because you didn't supply one the second time. Adder's comment is right. You don't need ajax at all. Just render the href link and include the ID in it, like he showed you. – ADyson Aug 16 '16 at 14:42
  • @Adder and ADyson thank you very much! You were right, I do not need AJAX for this. I have learnt HTML, PHP and AJAX in let's say mixed-style but did not pay attention that how could I use the $_GET method. Thought I can access it only in-form validations! By the way thank you, you are awesome guys, I solved the problem. Appreciated your patiance and kindness! Christian – Krisztián Tóth Aug 16 '16 at 16:14

4 Answers4

0

First approach: Inside account.php change type to post and then in view.php get posted data as $_POST['id']

Second approach: you can send data in url like

'view.php?id='+id 
user1885057
  • 85
  • 1
  • 2
  • 12
0

The usual way to do this in ajax is to have a container div. Instead of console.log(data); you then append the html snippet delivered from view.php into the container.

$(document).on('click', 'div#demo', function(){
    var id = $(this).attr('data-id');
    $.ajax({
        url: 'view.php',
        type: 'get',
        data: {id : id},
        success: function(html) {
            $('div#container').append(html);
        }
    });
});

Or like this (the id is sent to view.php):

$(document).on('click', 'div#demo', function(){
    var id = $(this).attr('data-id');
    document.location.href="view.php?id="+id
});

Or like these two files:

$(document).on('click', 'div#demo', function(){
    var id = $(this).attr('data-id');
    $.ajax({
        url: 'view.php',
        type: 'get',
        data: {id : id},
        success: function(url) {
            document.location.href=url;
        }
    });
});

and view.php like this:

<?php 
require_once 'config.php';

$id = intval($_GET['id'];) //prevent sql injection
$geturl = $conn->query("SELECT title, url FROM table1 WHERE id='$id'");
$url = mysqli_fetch_row($geturl);

$conn->close();

echo $url[1];
?>
Adder
  • 5,708
  • 1
  • 28
  • 56
0

You must assign the returned HTML, which is in data, to a div or span tag. For example add to account.php the following HTML:

<div id="myframe"></div>

Then inside the success callback, instead of calling console.log(data); call this:

$('#myframe').html(data);
  • As I said to @Adder 's comment, I don't need a return value or a success callback. After it successfully sent to the view.php file I do my queries using that $_GET['id']; variable and everything is fine except that the variable does not show up. – Krisztián Tóth Aug 16 '16 at 13:23
0

Did two requests to the server and lost the value of the variable I wanted to send via AJAX. But two guys who commented here were very kind and helped me out how could I achieve this without AJAX and jQuery. Commented on their posts and the problem is solved!

Solution:

<a href="view.php?id=<?php echo $id; ?>" target="_self">Title</a>
// and on view.php used $_GET to access the appropriate ID what the user has clicked on.

Thanks to everybody who wished to help me out, the bests for you!

Christian