• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

sending a php variable to javascript

I have used this method before successully. a submit, an insert performs.. then it should evauluate the bottom 2 checkboxes and go to the right place.


function justcd() {
      

<?php

echo("currentid = $maxrow1;");

?>
       var baseURL = "cdinput.php?id="+currentid;
       top.location.href = baseURL;
       return true;
      

}




if(isset($_POST['Submit'])) {
//random crap, does an insert

$cdcb = $_GET['compileddatacb'];
$cdkw = $_GET['keywordscb'];

if ($cdcb == 1 && $cdkw == 0) {

$c1 = ocilogon("K3GISDCP", "K3GISDCP", "GIS");
$idquery = "select max(dc_id_id) AS MAXIMUM from k3gisdcp.dc";
$s1 = OCIParse ($c1, $idquery);
OCIExecute($s1);

       //$committed2 = ocicommit($c1);

       while (OCIFetchInto ($s1, $row, OCI_ASSOC)) {
        
         $maxrow1 = $row['MAXIMUM'];
        
         }
            print "<script>\n";
            print "justcd();\n";
            print "</script>\n";
            
}

//html:

<p>
      <input type="checkbox" name="compileddatacb" value="checkbox">
Add associated compiled data?</p>
    <p>

0
dprasad
Asked:
dprasad
  • 7
  • 4
  • 2
  • +1
1 Solution
 
BatalfCommented:
Did you remember to put in a <script> and </script> tag around your javascript function.

i.e.

<script type="text/javascript">
function justcd() {
     
<?php

echo("currentid = $maxrow1;");

?>
      var baseURL = "cdinput.php?id="+currentid;
      top.location.href = baseURL;
      return true;
     

}
</script>
0
 
dprasadAuthor Commented:
Yes, same problem. Here's most of the code now:

<script type="text/javascript">

function justcd() {
      

<?php

echo("currentid = $maxrow1;");

?>
       var baseURL = "cdinput.php?id="+currentid;
       top.location.href = baseURL;
       return true;
      

}
/*
function justkw() {

}

function both() {

}
*/
</script>
<?php



if(isset($_POST['Submit']))

      {
      $dt = $_POST['dc_title'];
                ...
$query="blah";
$statement = OCIParse ($c1, $query);
      echo $query;
      OCIExecute ($statement, OCI_DEFAULT);
      
      
      $committed1 = ocicommit($c1);


$cdcb = $_GET['compileddatacb'];
$cdkw = $_GET['keywordscb'];

if ($cdcb == 1 && $cdkw == 0) {

$c1 = ocilogon("K3GISDCP", "K3GISDCP", "GIS");
$idquery = "select max(dc_id_id) AS MAXIMUM from k3gisdcp.dc";
$s1 = OCIParse ($c1, $idquery);
OCIExecute($s1);

       //$committed2 = ocicommit($c1);

       while (OCIFetchInto ($s1, $row, OCI_ASSOC)) {
        
         $maxrow1 = $row['MAXIMUM'];
        
         }
            print "<script>\n";
            print "justcd();\n";
            print "</script>\n";
            
}

else if ($cdkw == 1 && $cdcb == 0) {
            print "<script>\n";
            print "justkw();\n";
            print "</script>\n";
}

else if ($cdkw == 1 && $cdcb == 1) {

            print "<script>\n";
            print "bothcdkw();\n";
            print "</script>\n";

}




      
      
}
      
      
      
      
      
      
      
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<p align="left"><strong><U>Data Catalog Main Table </U></strong>(DC)</p>
  <div align="left">

 
  <form method="post" action="" enctype="multipart/form-data">

    <table width="324" border="0">
     
      <tr>
        <td><div align="center"><strong>Title:
              <input name="dc_title" type="text" id="dc_title">
        </strong></div></td>
      </tr>
      <tr>
        <td><div align="center"><strong>Minx:
            <input name="dc_minx" type="text" id="dc_minx">
        </strong></div></td>
      </tr>
      <tr>
        <td><div align="center"><strong>Miny</strong>:
            <input name="dc_miny" type="text" id="dc_miny">
        </div></td>
      </tr>
      <tr>
        <td><div align="center"><strong>Maxx</strong>:
            <input name="dc_maxx" type="text" id="dc_maxx">
        </div></td>
      </tr>
      <tr>
        <td><div align="center"><strong>Maxy:
            <input name="dc_maxy" type="text" id="dc_maxy">
        </strong></div></td>
      </tr>
      <tr>
        <td><div align="center"><strong>Metafile:
            <input name="dc_metafile" type="file" id="dc_metafile"  size="40">
        </strong></div></td>
      </tr>
      <tr>
        <td><div align="center"><strong>Survey ID:
            <input name="dc_survey_id" type="text" id="dc_survey_id">
        </strong></div></td>
      </tr>
      <tr>
        <td><div align="center"><strong>Date of Data:
            <input name="dc_dateofdata" type="text" id="dc_dateofdata">
        </strong></div></td>
      </tr>
      <tr>
        <td><div align="center"><strong>Updated Date:
            <input name="dc_updateddate" type="text" id="dc_updateddate">
        </strong></div></td>
      </tr>
      <tr>
        <td><div align="center"></div></td>
      </tr>
      <tr>
        <td><div align="center"><strong>K3 ID:
            <input name="dc_k3id" type="text" id="dc_k3id">
        </strong></div></td>
      </tr>
      <tr>
        <td><div align="center"><strong>POC:
            <input name="dc_poc" type="text" id="dc.poc">
        </strong></div></td>
      </tr>
      <tr>
        <td><div align="center"><strong>Filetype(s):
          <input name="dc_filetype" type="text" id="dc_filetype">
        </strong></div></td>
      </tr>
      <tr>
        <td><div align="center"><strong>Availability:
              <input name="dc_availability" type="text" id="dc_availability">
        </strong></div></td>
      </tr>
</table>
    <p>
      <input type="checkbox" name="compileddatacb" value="checkbox">
Add associated compiled data?</p>
    <p>
      <input type="checkbox" name="keywordscb" value="checkbox">
      Add associated keywords?
    </p>
    <p>
      <input type="submit" name="Submit" value="submit">
</form>
</p>
</body>
</html>
0
 
AndyCommented:
PHP is server side and all the code is run before it hits the users browser where as Javascript is client side.

If the code above has been posted in the order that it appears in your page then $maxrow1 will be undefined in your javascript function because it is not set until later in the page.

If you just want to forward to another page use the PHP header() function instead of the JS.

ie: Replace...
          print "<script>\n";
          print "justcd();\n";
          print "</script>\n";

...with...
          header("Location:cdinput.php?id=".$maxrow1);

Hope that helps.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BatalfCommented:
And $maxrow1 is defined?

If not, lines like this

echo("currentid = $maxrow1;");

could result in a Javascript error. You could try modify it to

echo("currentid = '$maxrow1';");

Also check in your browser source(Right click, select view source in your browser), that the justcd function is called.

At last, check for any javascript errors. If they isn't alerted to you, you may find them by double clicking on the warning sign that appears in the bottom left corner of the browser.

Batalf

0
 
BatalfCommented:
A header function is a good idea if nothing has been sent to the browser earlier in the script.

You could also use a meta tag instead of javascript. i.e. instead of

print "<script>\n";
          print "justcd();\n";
          print "</script>\n";

use



?>
<META HTTP-EQUIV=Refresh CONTENT="0; URL=cdinput.php?id=<?php echo $maxrow1; ?>">
<?
0
 
dprasadAuthor Commented:
Reapz: I tried using header, doesn't seem to work.

Batalf: maxrow isnt echoing, also trying to echo the checkboxes like this, nothing shows up.
$cdcb = $_GET['compileddatacb'];
$cdkw = $_GET['keywordscb'];

echo $cdcb;
echo $cdkw;



 hmm, It gave a javascript error on line 5 when I tried to call justcd() which is just before:

 <?php

echo("currentid = $maxrow1;");

?>

in the java script


I also tried to use the <meta http tag batalf, didnt work unfortunately
0
 
BatalfCommented:
Do you think you could open the page in your browser, right click and select view source and then post that browser source code here.

It would make it easier to debug.

Batalf.
0
 
dprasadAuthor Commented:
So I guess the problem is getting the checkbox values:

$cdcb = $_GET['compileddatacb'];
$cdkw = $_GET['keywordscb'];

echo $cdcb;
echo $cdkw;

0
 
BatalfCommented:
You're using POST as form method, so it should be

$cdcb = $_POST['compileddatacb'];
$cdkw = $_POST['keywordscb'];
0
 
dprasadAuthor Commented:
Here's the source

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">

function justcd() {
      

currentid = ;       var baseURL = "cdinput.php?id="+currentid;
       top.location.href = baseURL;
       return true;
      

}

/// I tried changing to post, still doesnt echo. hmm
0
 
BatalfCommented:
Yes the problem is the $maxrow1 which is missing. It results in a javascript error

As I could see, it isn't until later in the script. Due to this, you should change the javascript function to this

function justcd(currentid) {
 
      var baseURL = "cdinput.php?id="+currentid;
      top.location.href = baseURL;
      return true;
     
}

and call it like this:

print "<script>\n";
print "justcd($maxrow1);\n";
print "</script>\n";

0
 
BatalfCommented:
>"As I could see, it isn't until later in the script"

"As I could see, it isn't CREATED until later in the script"
0
 
AndyCommented:
You say that header didn't work. How so? Did it output an error message on the page or did it just do nothing?
0
 
minnirokCommented:
The problem is you're outputting your javascript before the html headers are written.  Move your javascript code below the <html>and <head> tags and you may have more luck.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now