I am trying to get Ajax to load a form which is created in PHP and assigned a variable $copen.
In Ajax how do I go about placing the variable in the URL part of Ajax to open and retrieve the JSON data.
The code in my Ajax script:

        $.ajax({
             async: false,
             url: 'queryme3x.php',
             type: 'get',
             dataType: "json",
             success: function(data) {
               $.each(data, function(i, val){

                val.timestamp = Date.parse(val.date);

                // save the years so we can create year markers
                var year = new Date(val.timestamp).getFullYear();
                if (years.indexOf(year) < 0)
                    years[years.length] = year;

                // combine data & templqate
                var html = postTemplate(val);

                $('#timeline').append(html);

And the code in my PHP script:

<?php
require("dbenter.php");
$checks = $_POST['check1'];
if (isset($checks) && !empty($checks)){
foreach ($_POST['check1'] as $tst1) {
    $tst1 = implode("' OR Category ='", $checks);
}
$checks1 = "'".$tst1."'";
}

$sql = "SELECT * FROM tstline WHERE Category =$checks1";
$result = mysql_query($sql);
$rows = array();
while ($r = mysql_fetch_assoc($result)) {
    $row = array();
    $row['date'] = $r['date'];
    $row['Year'] = $r['Year'];
    $row['Category'] = $r['Category'];
    $row['Subcategory'] = $r['Subcategory'];
    $row['Location'] = $r['Location'];
    $row['Shortdescription'] = $r['Shortdescription'];
    $row['Fulldescription'] = $r['Fulldescription'];
    array_push($rows,$row);
}
$fdate = date('mdy');
$fp = $checks1.$fdate.".json";
$stripout = array("'","OR","="," ","Category");

$nfname = str_replace($stripout,"",$fp);
echo $nfname;
$tst2 = $checks;
if (file_exists($nfname)) {
    $copen = $nfname;
    return true;
}
else {
    $copen = fopen($nfname,'w+');
    fwrite($copen, json_encode($rows));
}
echo json_encode($rows);
?>

As you can see its the value of $copen that I want to carry into the Ajax URL section.
Any help would be appreciated :)

Member Avatar for LastMitch

Carrying a PHP variable to Ajax

What do you mean by carrying PHP variable?

You meant Passing PHP variable or Retrieving PHP variable?

Ajax how do I go about placing the variable in the URL part of Ajax to open and retrieve the JSON data.

Where is your data in your Ajax? There should be data.

Retrieving a PHP variable $copen and passing it to the ajax url option. So it might be url: 'queryme3x.php?copen=<?php echo $copen?>'

when I echo the data it shows the data also the output file shows the correct data in it.
the $copen represents the file name which would be cat1cat2070513.json for example

Try this... Save the value to an input element of type "hidden" from your PHP code. The information wont be visible but it will be accessible client side. Then client side, grab the value using jQuery assign it to a variable then work with that variable in your Ajax routine.

you should parse $nfname
$copen is a file handeler, not a file name

Thanx for all ya help it still has a problem with getting the filename from inside an iframe and putting it to the text box

<div class="container">
  <div class="header" id="header">Who Runs London: Timeline</div>
    <div class="wrlcp_login"><a href="userlogin1.html">Admin Login</a></div>
    <div class="left-side-top" id="ltr">
  <input type="hidden" name="hiddta" id="hiddta" /><br><br>Use the table on the right
To select your options for the timeline of key London events.<br><br>
Click on the arrow heads to scroll up or down.
    </div>
    <div class="left-side-bottom"><br><Br>
Got feedback, did we miss something important, have we got something wrong?<br><br>
<a href="contactus.php" target="middlesect">Click here</a> to help us improve the information.</div>
    <div class="middle">
        <iframe id="middlesect" src="timeline.html" name="middlesect"></iframe>
</div>
    <div class="right-side"><h1 class="daterange1">Data Range</h1>
    <ul class="uldrop1">
        <li class="listopts"><a href="tlnorman.html" target="middlesect">Norman</a></li>
        <li class="listopts"><a href="tlplant.html" target="middlesect">Plantagenet</a></li>
        <li class="listopts"><a href="tltudor.html" target="middlesect">Tudor</a></li>
        <li class="listopts"><a href="tlliz1.html" target="middlesect">Elizabethan</a></li>
        <li class="listopts"><a href="tlgeorge.html" target="middlesect">Georgian</a></li>
        <li class="listopts"><a href="tlvictoria.html" target="middlesect">Victorian</a></li>
        <li class="listopts"><a href="tledward.html" target="middlesect">Edwardian</a></li>
        <li class="listopts"><a href="tlmodern.html" target="middlesect">Modern</a></li>
        <li class="listopts"><a href="more.php" target="middlesect">More...</a></li>
    </ul>
    <h1 class="daterange2">Themes</h1>
    <ul class="uldrop2">
        <li class="themeopt1"><a href="tlpop.html" target="middlesect" class="themeopt1">Population</a></li>
        <li class="themeopt4"><a href="tlcivic.html" target="middlesect" class="themeopt4">Civic Admin</a></li>
        <li class="themeopt3"><a href="tledu.html" target="middlesect" class="themeopt3">Education</a></li>
        <li class="themeopt2"><a href="tltrans.html" target="middlesect" class="themeopt2">Transport</a></li>
        <li class="themeopt5"><a href="tlhealth.html" target="middlesect" class="themeopt5">Health</a></li>
        <li class="themeopt6"><a href="tlenv.html" target="middlesect" class="themeopt6">Environment</a></li>
        <li class="themeopt7"><a href="tlcrime.html" target="middlesect" class="themeopt7">Crime</a></li>
        <li class="themeopt8"><a href="tlcomm.html" target="middlesect" class="themeopt8">Commerce</a></li>
        <li class="themeopt1"><a href="tlleisure.html" target="middlesect" class="themeopt1">Leisure</a></li>
        <li class="themeopt9"><a href="tlworld.html" target="middlesect" class="themeopt9">World Events</a></li>
        <li class="themeopt91"><a href="moreopts.php" target="middlesect" class="themeopt91">More Options</a></li>
    </ul>
    <div class="multishow">
    <input type="checkbox" name="check1[]" value="Population" />Population<br>
    <input type="checkbox" name="check1[]" value="Civic Admin" />Civic Admin<br>
    <button onClick="clickme('middlesect')" >Show Query</button>
    </div>
    </div>
</div>
</body>
</html>

and the other document is the queryme3x.php

<?php
require("dbenter.php");
$checks = $_POST['check1'];
if (isset($checks) && !empty($checks)){
foreach ($_POST['check1'] as $tst1) {
    $tst1 = implode("' OR Category ='", $checks);
}
$checks1 = "'".$tst1."'";
}

$sql = "SELECT * FROM tstline WHERE Category =$checks1";
$result = mysql_query($sql);
$rows = array();
while ($r = mysql_fetch_assoc($result)) {
    $row = array();
    $row['date'] = $r['date'];
    $row['Year'] = $r['Year'];
    $row['Category'] = $r['Category'];
    $row['Subcategory'] = $r['Subcategory'];
    $row['Location'] = $r['Location'];
    $row['Shortdescription'] = $r['Shortdescription'];
    $row['Fulldescription'] = $r['Fulldescription'];
    array_push($rows,$row);
}
$fdate = date('mdy');
$fp = $checks1.$fdate.".json";
$stripout = array("'","OR","="," ","Category");

$nfname = str_replace($stripout,"",$fp);
echo $nfname;
$tst2 = $checks;
if (file_exists($nfname)) {
    $copen = $nfname;
    return true;
}
else {
    $copen = fopen($nfname,'w+');
    fwrite($copen, json_encode($rows));
}
?>
$copen1 = json_encode($rows);
<script type="text/javascript">
var op1 = "hiddta";
var op2 = parent.document.getElementById(op1);
var opvalue = op2.value;
op2.value=<?php echo $nfname?>;
</script>
<?php
/*echo json_encode($rows);
*/
?>

does this look right as it doesnt seem to want to write to the text box in the main screen outside the iframe.

It isn't easy to follow just what you are after. However, one thing I have noted. Your while loop to build the $rows array is a complete waste of time. You have that array already - the SQL query is returning it as $r!

I am trying to take the $nfname variable that appears in the iframe id "middlesect" from a page called queryme3x.php and place into a text box in the index.html page

I have this code that is my main code the index3.html which has an iframe in it that everything loads to.

<body>
<div class="container" id="container">
    <div class="heading" id="heading">
    Who Runs London<br>Timeline
  <div class="admin"><a href="userlogin1.html" class="adminlogin">Admin Login</a></div>
  </div>
  <div class="lopt" id="lopt">
    <div class="datepertitle">Date Periods</div>
    <ul class="uldrop1">
        <li class="listopts"><a href="tlangsax.html" target="middlesect" class="liopt1">Anglo Saxon</a></li>
      <li class="listopts"><a href="tlnorman.html" target="middlesect" class="liopt1">Norman</a></li>
      <li class="listopts"><a href="tlplant.html" target="middlesect" class="liopt1">Plantagenet</a></li>
      <li class="listopts"><a href="tltudor.html" target="middlesect" class="liopt1">Tudor</a></li>
      <li class="listopts"><a href="tlliz1.html" target="middlesect" class="liopt1">Elizabethan</a></li>
      <li class="listopts"><a href="tlgeorge.html" target="middlesect" class="liopt1">Georgian</a></li>
      <li class="listopts"><a href="tlvictoria.html" target="middlesect" class="liopt1">Victorian</a></li>
      <li class="listopts"><a href="tledward.html" target="middlesect" class="liopt1">Edwardian</a></li>
      <li class="listopts"><a href="tlmodern.html" target="middlesect" class="liopt1">Modern</a></li>
    </ul>
  </div>
  <div class="lopt2" id="lopt2">
    <div class="catstitle">Categories</div>
    <ul class="uldrop2">
        <li class="theme1"><a href="tlpop.html" target="middlesect" class="theme1">Population</a></li>
      <li class="theme2"><a href="tlcivic.html" target="middlesect" class="theme2">Civil Administration</a></li>
      <li class="theme3"><a href="tledu.html" target="middlesect" class="theme3">Education</a></li>
      <li class="theme4"><a href="tltrans.html" target="middlesect" class="theme4">Transportation</a></li>
      <li class="theme5"><a href="tlhealth.html" target="middlesect" class="theme5">Health</a></li>
      <li class="theme6"><a href="tlenv.html" target="middlesect" class="theme6">Environment</a></li>
      <li class="theme7"><a href="tlcrime.html" target="middlesect" class="theme7">Crime</a></li>
      <li class="theme8"><a href="tlcomm.html" target="middlesect" class="theme8">Commerce</a></li>
      <li class="theme9"><a href="tlworld.html" target="middlesect" class="theme9">World Events</a></li>
      <li class="theme10"><a href="moreopts.php" target="middlesect" class="theme10">More Options...</a></li>
    </ul>
  </div>
  <div class="lopt3" id="lopt3">Got feedback. Did we miss something important, have we got something wrong?<br><br>
  <a href="contactus.php" target="middlesect">Click here</a> to help us improve this information.
  </div>
  <div class="lopt4" id="lopt4">Use the tables above to select your options for the timeline of key London events.</div>
  <div class="contents" id="contents">
        <iframe class="middlesect" id="middlesect" frameborder="0">
    </iframe>
  </div>
</div>
</body>
</html>

and so when you click on the more options it loads tst1.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="wrltest2.css" rel="stylesheet" type="text/css" media="screen">
<script type="text/javascript">
function startquery(){
    alert ("test");
}
</script>
</head>

<body>
<form method="post" action="queryme3x.php">
    <input type="checkbox" name="check1[]" value="Population"/> Population&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="checkbox" name="check1[]" value="Civil Adminstration"/> Civil Administration<br/>
    <input type="checkbox" name="check1[]" value="Commerce "/> Commerce&nbsp;&nbsp;&nbsp;
    <input type="checkbox" name="check1[]" value="Crime"/> Crime<br>
    <input type="checkbox" name="check1[]" value="Education"/> Education&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="checkbox" name="check1[]" value="Transport"/> Transport<br>
    <input type="checkbox" name="check1[]" value="Health"/> Health&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="checkbox" name="check1[]" value="Environment"/> Environment<br>
    <input type="checkbox" name="check1[]" value="Leisure"/> Leisure&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="checkbox" name="check1[]" value="World"/> World<br><br>
    <input type="submit" value="Start Query" />
    <input type="button" value="Display Data" onClick="startquery()"
</form>
</body>
</html>

clicking on the Start Query button it loads queryme3x.php. It is from the queryme3x.php that I want to use the variable $nfname and place it into a hidden area in the lopt2 div on the side. I have tried using parent.document.getElementById('hidif") and the top.document.getElementById('hidif') and it doesnt seem to work trying to copy the variable outside the iframe. What am I doing wrong. Any help would be appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.