Page 1 of 1

Passing Javascript variable to php

Posted: Thu Aug 16, 2007 6:16 am
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]

Re: Passing Javascript variable to php

Posted: Thu Aug 16, 2007 6:28 am
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.

Posted: Thu Aug 16, 2007 6:43 am
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.

Re: Passing Javascript variable to php

Posted: Thu Aug 16, 2007 7:55 am
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

Posted: Thu Aug 16, 2007 8:19 am
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.

Posted: Thu Aug 16, 2007 4:03 pm
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?

Posted: Thu Aug 16, 2007 5:15 pm
by feyd
impulse() wrote:Could you not echo out the Javascript variable, in Javascript, and capture it with PHP tags?
Nope.

Solved !

Posted: Fri Aug 17, 2007 9:07 am
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