?
Solved

sending a php variable to javascript

Posted on 2005-03-29
14
Medium Priority
?
239 Views
Last Modified: 2011-10-03
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
Comment
Question by:dprasad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 13655416
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
 

Author Comment

by:dprasad
ID: 13655484
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
 
LVL 6

Expert Comment

by:Andy
ID: 13655490
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 32

Expert Comment

by:Batalf
ID: 13655522
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
 
LVL 32

Expert Comment

by:Batalf
ID: 13655551
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
 

Author Comment

by:dprasad
ID: 13655722
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
 
LVL 32

Expert Comment

by:Batalf
ID: 13655759
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
 

Author Comment

by:dprasad
ID: 13655765
So I guess the problem is getting the checkbox values:

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

echo $cdcb;
echo $cdkw;

0
 
LVL 32

Expert Comment

by:Batalf
ID: 13655796
You're using POST as form method, so it should be

$cdcb = $_POST['compileddatacb'];
$cdkw = $_POST['keywordscb'];
0
 

Author Comment

by:dprasad
ID: 13655832
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
 
LVL 32

Accepted Solution

by:
Batalf earned 2000 total points
ID: 13655909
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
 
LVL 32

Expert Comment

by:Batalf
ID: 13655915
>"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
 
LVL 6

Expert Comment

by:Andy
ID: 13655923
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
 
LVL 7

Expert Comment

by:minnirok
ID: 13655967
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question