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!
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
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.
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]
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.
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 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.
<?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.
<?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.