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.?
:(

Recommended Answers

All 5 Replies

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
}
?>

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...

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.