Link to home
Start Free TrialLog in
Avatar of dprasad
dprasad

asked on

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>

Avatar of Batalf
Batalf
Flag of United States of America image

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>
Avatar of dprasad
dprasad

ASKER

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>
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.
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

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; ?>">
<?
Avatar of dprasad

ASKER

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
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.
Avatar of dprasad

ASKER

So I guess the problem is getting the checkbox values:

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

echo $cdcb;
echo $cdkw;

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

$cdcb = $_POST['compileddatacb'];
$cdkw = $_POST['keywordscb'];
Avatar of dprasad

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Batalf
Batalf
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>"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"
You say that header didn't work. How so? Did it output an error message on the page or did it just do nothing?
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.