0

I know this questions has been asked in various forms, but I've been unable to find a response that squares with what I'm trying to accomplish. Total amateur, so any additional pointers are appreciated.

The Task:
Use a form on page1 to post data to PHP file, which then searches the MySQL table for rows corresponding to the $_POST[]. Then, the PHP file echoes the result as JSON_encode($variable). From there, the PHP file redirects the user back to page1, which (in theory) has a JQuery script that calls the $variable from the PHP file and create html with the data.

The Code:
PHP

<?php
ob_start();
session_start();
session_save_path('path');
mysql_connect("", "", "")or die(); mysql_select_db("db")or die();
$pname = $_POST['country'];
$result = mysql_query("SELECT * FROM project WHERE name = '$pname'");      
$array = mysql_fetch_row($result);  
echo json_encode($array);
header("page1.html");
?>

html/jquery/ajax

<script type="text/javascript">// <![CDATA[
$(document).ready( function() {
    $.ajax({
        type: 'POST',
        url: 'page.php',
        data: '',
        dataType: 'json',
        cache: false,
        success: function(result) {
            $('#content1').html(data);
        },
    });
});
// ]]></script>
<div id="content1"></div>

The php script works and echoes the JSON encoded variable, but nothing on #content1... I have a feeling either my code is wrong, or the data is lost while posting to the PHP file and then redirecting.

6
  • What is header("page1.html") supposed to do? Did you mean header("Location: page1.html")? If you do that, you can't use echo. Commented Feb 28, 2013 at 22:17
  • The argument to html() should be a string of HTML code. The JSON will decode into a Javascript object, not HTML. Commented Feb 28, 2013 at 22:20
  • Oops. Header is correct on the php file, I was removing the actual file name and deleted the whole thing. Commented Feb 28, 2013 at 23:25
  • Still, it doesn't make sense to put a redirect in a script used via AJAX. And PHP generates an error if you try to use header() after echo. Commented Feb 28, 2013 at 23:50
  • OK, let me try again without using 'header()'... Would it make more sense to post from JQuery ajax? Commented Mar 1, 2013 at 1:07

2 Answers 2

1

You're trying to append the variable data to the content but the variable is called result. Try this:

<script type="text/javascript">// <![CDATA[
$(document).ready( function() {
$.ajax({
type: 'POST',
url: 'page.php',
data: '',
dataType: 'json',
cache: false,
success: function(result) {
$('#content1').html(result);//<- this used to be $('#content1').html(data);
},
});
});
// ]]></script>
<div id="content1"></div>

Additionally, as many have pointed out you are simply outputting the json at the moment - there is nothing in place to generate the table.

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

3 Comments

plus. PHP is sending over an json'd array, so OP is trying to stuff a javascript array into an html context. square hole, round peg...
OK, made that change, but still no data. Is there a better way to do this? I'm sort of compiling a "Frakenstein" code at this point...
Alert or log the "result" variable in the success area. Check to see what exactly is in there.
0

Change $('#content1').html(data) to $('#content1').html(result);

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.