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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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