Passing Javascript variable to php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nbt725
Forum Newbie
Posts: 11
Joined: Thu Aug 16, 2007 6:08 am

Passing Javascript variable to php

Post by nbt725 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Dear Sir,

Hello ! I want to get the javascript variable in php code in following function

Code: Select all

script language="JavaScript">
function chg_subcatg(cfield,fieldnm)
{
// For Retrieval of current catg	
  var f1=document.forms[0];
  var sbox1=f1.elements[cfield];
  var choice=sbox1.selectedIndex;
  var [b] c_catg [/b]= sbox1.options[choice].value;
  document.forms[0].thefield.value = c_catg;

// For updation	of sub category
  var f=document.forms[0];
  var selectBox=f.elements[fieldnm];
<?php
  require_once "../functions_main.inc"; 
  $cxn = Connect_to_db("../vars.inc");
//  $sql = "select * from subcatg_mast where catg_id = 1";
 $sql = "select * from subcatg_mast where catg_id = " . $t1;

  $result = mysql_query($sql);
  while ($row = mysql_fetch_array($result))
    {
 ?>   	
var extraOption=new Option('<?php echo $row['subcatg_desc'];?>','<?php echo $row['catg_id'];?>',0,0);  
selectBox.options[selectBox.options.length]=extraOption;	

 <?php
    }
 ?>   
   
}
How can I pass the above java script variable c_catg to my %t1 variable to form where clause value in $sql. This variable c_catg has integer value, hence written this way to construct $sql.

Please guide.


Thanks & Regards,

Naimesh Trivedi
nbtrivedi@hotmail.com


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Passing Javascript variable to php

Post by superdezign »

nbt725 wrote:How can I pass the above java script variable c_catg to my %t1 variable to form where clause value in $sql. This variable c_catg has integer value, hence written this way to construct $sql.
You either need to the send the data as a POST / GET request to PHP, or use AJAX.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

As far as I can see .. You cannot do what you are trying as it stands unless you use AJAX functionality.

You should try to build pages without requiring javascript for core functionality. (Look at javascript as only enhancing your site users experience).

Dynamic/Chained Selects using Ajax Prototype/JQuery does what I think you are after with javascript using one of two possible javascript libraries available (more libraries are easily available on the web). If you read the entire thread it also goes into examples of how to allow for if the user does not have javascript enabled.
nbt725
Forum Newbie
Posts: 11
Joined: Thu Aug 16, 2007 6:08 am

Re: Passing Javascript variable to php

Post by nbt725 »

superdezign wrote:
nbt725 wrote:How can I pass the above java script variable c_catg to my %t1 variable to form where clause value in $sql. This variable c_catg has integer value, hence written this way to construct $sql.
You either need to the send the data as a POST / GET request to PHP, or use AJAX.

Yes I am trying to populate my database combo based on other combo value. I tried to do it with the DOM JavaScript example, I had and customized it. $_GET and/or $_POST I guess is not available until you form is posted or user clicks on submit button. I have selected type=POST in this form. This function is called from onClick even of a <select>. Everything done, except passing the current data value to the where clause is not getting done. So please guide on it. Ajax I understand may be a choice, but all these efforts would go waste and I don't know how to use Ajax also.

Thanks & Regards,

Naimesh Trivedi
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Basic structure I would use.. not tested and needs to be made relevant to your requirements

Code: Select all

<?php
   if (!empty($_POST['formset'])) {
       // get values from database based on $_POST['first'] and build second select box as $second
   } else {
       $second=''; // or whatever you want if second box is empty
   }
   // Also possibly need to validate if the second variable is set before processing the rest of form.

?>
<form name="myform">
  <select name="first" onchange="javascript::myform.submit();"><!-- cannot remember the js... is it document.myform.submit() ? -->
     <option value="one">One</option>
     <option value="one">Two</option>
     <option value="one">Three</option>
  </select>
  <noscript><input type="submit" name="changesel"></noscript><br />
  <?php echo $second; ?>
  // Rest of form
  <input type="hidden" name="formset" value=1 />
</form>
Note very little javascript required.. Mostly php. Noscript tag enables usage even if user has no javascript.
Hope that helps a bit. I would suggest you look at the link I previously pointed out if you would like a simple AJAX solution. If it doesn't help please reply to the topic stating why and I will possibly update it.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I'm sure I've done this in the past or I may have passed a PHP variable to Javascript.

Could you not echo out the Javascript variable, in Javascript, and capture it with PHP tags?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

impulse() wrote:Could you not echo out the Javascript variable, in Javascript, and capture it with PHP tags?
Nope.
nbt725
Forum Newbie
Posts: 11
Joined: Thu Aug 16, 2007 6:08 am

Solved !

Post by nbt725 »

CoderGoblin wrote:Basic structure I would use.. not tested and needs to be made relevant to your requirements

Code: Select all

<?php
   if (!empty($_POST['formset'])) {
       // get values from database based on $_POST['first'] and build second select box as $second
   } else {
       $second=''; // or whatever you want if second box is empty
   }
   // Also possibly need to validate if the second variable is set before processing the rest of form.

?>
<form name="myform">
  <select name="first" onchange="javascript::myform.submit();"><!-- cannot remember the js... is it document.myform.submit() ? -->
     <option value="one">One</option>
     <option value="one">Two</option>
     <option value="one">Three</option>
  </select>
  <noscript><input type="submit" name="changesel"></noscript><br />
  <?php echo $second; ?>
  // Rest of form
  <input type="hidden" name="formset" value=1 />
</form>
Note very little javascript required.. Mostly php. Noscript tag enables usage even if user has no javascript.
Hope that helps a bit. I would suggest you look at the link I previously pointed out if you would like a simple AJAX solution. If it doesn't help please reply to the topic stating why and I will possibly update it.
Thanks for the help. This is has worked, I twised this idea to bit and it worked.

Thanks & Regards,

Naimesh Trivedi
Post Reply