Jump to content

[SOLVED] Calling some javascript from within php


HaLo2FrEeEk

Recommended Posts

I did a lot of research on this and tried as many things as I could, but I still can't get this to work.  I have a div I need to hide on a certain event within my php file.  The event already has a check to kill the script if a certain parameter is met, so I tried something like this:

 

$gt = $_REQUEST['gt'];
if(!$gt) {
?>
<div style="display:none;"><select name="film" id="film"><option value="#"></option></select></div>
<script language="Javascript" type="text/javascript">
document.getElementById('therest').style.display='none';
</script>
<?php
  die("Gamertag not specified<br><br>");
  }

 

Which I thought would print the javscript code to the page when the value "gt" was not supplied, thus making the div "therest" hidden.  It's not working though.  Can anyone lead me to a solution to make this work please?  Thanks in advance, and sorry for all the questions today!

Link to comment
Share on other sites

There is absolutely no reason to use Javascript to make an element hidden on page load! Javascript should only be used to modify content after the page loads. The reason that code is not working would depend on a few differnt things (such as where, exactly, that code is on the page in relation to the other content). But, that's not the right way to do it in any event.

 

You just need to set the style of the div directly through style attributes like this

<?php

$gt = $_REQUEST['gt'];
$theRestStyle = ($gt!=='') ? '' : 'display:none;';
    
?>

<div id="therest" style="<?php echo $theRestStyle; ?>">Div Content goes here</div>

Link to comment
Share on other sites

of store the HTML to that specific div in a PHP variable, if the condition your looking for is true, then echo it out, if not then just dont echo it out..that if you dont want to have the div and its information available and sent to the browser.... just a suggestion...

Link to comment
Share on other sites

I guess I didn't explain properly.  The snippet I posted is from a php page that will be called via AJAX, after the page is loaded completely.  The ajax page recieves the field "gt" as an input and uses it.  If that field isn't present it messes up the validation process of the parent page, causing all sorts of issues.  I need to hide "therest" so that no data can be entered there and thus the validation will always return false until the "gt" field is filled in.  Filling in the "gt" field and either clicking a button or blurring the field will call the AJAX again, populating a div directly below it, which contains a select element.  When a value is chosen from that select element then "therest" can be shown again.

 

Here's the page I'm talking about:

 

http://infectionist.com/misc/capture/public_test/capture.php

 

if you don't have an xbox live gamertag, just use halo2freeek, that's mine and it'll work for testing purposes.

Link to comment
Share on other sites

I'm not seeing the problem, if the field is blank the message "Gamertag not specified" is displayed and nothing happens when I click the button. If I use a valid value and select something from the list, the page is populated with data. If I then remove the gamertag the data is removed. Seems the validation works just fine.

 

But, from reading your last post it would seem there is some sort of problem and I am just not following the right steps. Not fully understanding the problem, I can't be sure, but I think there is a simple enough solution.

 

The AJAX page uses the input to generate content to be displayed on the page, but in some instances the value is invalid and you need the page to react in some why (outside of the normal content being replaces). In that case, you need to determine what are all the error conditions you want to handle. Then have the AJAX page return an error code instead of the content. You can then have the JavaScript on the parent page inspect the return value. If the value is not an error condition, then display the content like you do now. Of, if it is an error condition, then do whatever other actions are needed.

 

Here is some "mock" code:

 

AJAX Page

<?php
if ($_REQUEST['gt']==='')
{
    echo "ERROR:No gamertag";
}
else
{
    //echo the normal content to be displayed
}

 

JavaScript in Parent page

xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
  {
    var returnValue = xmlhttp.responseText;
    if (returnValue.substr(0, 5) == 'ERROR')
    {
      //Get the error condition, e.g. 'No gamertag'
      var errorCondition = returnValue.substr(6);

      //Use a switch for multiple error conditions, not needed if only using one
      switch(errorCondition)
      {
        case 'No gamertag':
          //Do error handling (i.e. disabling/hiding elements)
          break;
        default:
          //Add code for unknown error conditions if applicable
          break;
      }
    }
    else
    {
      //run normal code for a success return value
    }
  }
}

 

The bottom line is include the error handling for the parent page IN the parent page.

Link to comment
Share on other sites

No, I changed it after posting that link to just hide the "therest" div everytime the gamertag textbox was changed, no matter if it was emptied or whatever.  This should stop people from being able to enter all the information, change the gamertag, then click submit, confusing my system.  This way if they change the gamertag, they have to reselect a film from the list, and refill all the information out.

 

I think I've got this under control, thank you very much for your help.

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.