Jump to content

Calling Javascript Function from PHP


Styles2304

Recommended Posts

I'm populating a form and, in the process, trying to show/hide certain divs based on whether or not certain radio buttons are marked as checked.

 

Am I missing something that is causing the toggleDisplay code being called by the PHP to not execute?

 

<input type="radio" name="Mil_Status" value="ETS" onclick="toggleDisplay('show', 'Mil_FromTo', 'main'); toggleDisplay('hide', 'Mil_Serving', 'main'); toggleDisplay('hide', 'Mil_Discharge', 'main');" <?php if ($Mil_Status == 'ETS') { echo 'Checked'; ?>> <?php echo "<script type='text/javascript'>toggleDisplay('show', 'Mil_FromTo', 'main'); toggleDisplay('hide', 'Mil_Serving', 'main'); toggleDisplay('hide', 'Mil_Discharge', 'main');</script>"; } ?> ETS or Retired <br>

Link to comment
https://forums.phpfreaks.com/topic/160788-calling-javascript-function-from-php/
Share on other sites

Your input tag is never closed. And I don't know how you want that function to trigger, so this is the best I can do.

 

Yes it is, look closely. Right after "checked", I close the php tag and then close the input tag. I'm trying to trigger the function right there . . . that's what I'm asking for help with. The function is defined in a seperate file and loaded in the head but I don't know how to trigger it.

Yes it is, look closely. Right after "checked", I close the php tag and then close the input tag. I'm trying to trigger the function right there . . . that's what I'm asking for help with. The function is defined in a seperate file and loaded in the head but I don't know how to trigger it.

 

Only if $Mil_Status == 'ETS' . Otherwise the closing tag is not output (it is inside the curley brackets for the if statement).

 

All the best

 

Keith

The function is defined in a seperate file and loaded in the head but I don't know how to trigger it.

At the very bottom of your output, immediately before the closing </html> tag, do

echo "<script language='javascript' type='text/javascript'>";
echo "    toggleDisplay('show', 'Mil_FromTo', 'main');";
echo "    toggleDisplay('hide', 'Mil_Serving', 'main');"; 
echo "    toggleDisplay('hide', 'Mil_Discharge', 'main');";
echo "</script>";

or whatever variants you need

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.