0

Hi,

I have a javascript function to disable and enable a textbox.

<SCRIPT LANGUAGE="JavaScript">
function enable()
{
document.form1.tmphone2.disabled=false;
document.form1.pmphone2.disabled=false;

}
function disable()
{
document.form1.tmphone2.disabled=true;
document.form1.pmphone2.disabled=true;
}
</SCRIPT>

I need to call the function in a php code, on condition
Heres my code:

<?php 
$cc = $row['company_country'];
$s = "SG";
if ($cc == $s)
	{
echo "<SCRIPT LANGUAGE='javascript'>disable()</SCRIPT>";
}
else
{
//echo "test ";
echo "<SCRIPT LANGUAGE='javascript'>enable()</SCRIPT>";
}
?>

The above php code doesnt work..
But if i used the same javascript disable() function in an onclick event it works.
Why does the code doesnt work ?
where have i gone wrong.?
:(

2
Contributors
5
Replies
7
Views
7 Years
Discussion Span
Last Post by EvolutionFallen
0

PHP code won't actually call the function for you. It just prints whatever is in the echo statement to the page, it doesn't trigger any function names it prints.

If you don't mind my asking, why disable the textboxes? Instead, you can have PHP print them out, or not print them out:

<?php 
$cc = $row['company_country'];
$s = "SG";
if ($cc != $s) { ?>
<input type="text" id="tmphone2" name="tmphone2" size="10" maxlength="10" />
<input type="text" id="pmphone2" name="pmphone2" size="10" maxlength="10" />
<?php
}
?>
0

PHP code won't actually call the function for you. It just prints whatever is in the echo statement to the page, it doesn't trigger any function names it prints.

If you don't mind my asking, why disable the textboxes? Instead, you can have PHP print them out, or not print them out:

<?php 
$cc = $row['company_country'];
$s = "SG";
if ($cc != $s) { ?>
<input type="text" id="tmphone2" name="tmphone2" size="10" maxlength="10" />
<input type="text" id="pmphone2" name="pmphone2" size="10" maxlength="10" />
<?php
}
?>

Hey thanks for the code,
Actually i m using for Phone numbers,
But my design is different...i have the two text fields, in two different places...
HOw do i then write the code...

0

Hey thanks for the code,
Actually i m using for Phone numbers,
But my design is different...i have the two text fields, in two different places...
HOw do i then write the code...

Write the same if statement again in the place you want the second text box. As long as it's in the same scope, $cc and $s will have the same values unless you modify them somewhere in between the two locations. Alternatively, you can create a flag:

<?php 
$cc = $row['company_country'];
$s = "SG";
$print_textbox = FALSE;   // this is the flag to decide whether or not to print the second textbox. Defaults to no.
if ($cc != $s) { ?>

<input type="text" id="tmphone2" name="tmphone2" size="10" maxlength="10" />

<?php
$print_textbox = TRUE;   // tell the if statement further down to print the second textbox
}
?> 

<!-- Other code goes here, HTML or whatever -->

<?php
// this is where you want the second textbox
if($print_textbox) { ?>

<input type="text" id="pmphone2" name="pmphone2" size="10" maxlength="10" />

<?php 
} ?>

Using the flag lets you change $cc and $s between the two textboxes while still printing the textbox if necessary.

0

hey thnks for the code,
It helped me...
Got things to work..
:)
Thanks again

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.