Jump to content

Recommended Posts

Hello

Im not sure if the follow can be done [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /] anyone have any suggestions?? thanks.

<?php
$selection = empty($_POST['selection']);

if ($selection == 'Hard Bait')
{
?>
<script language="JavaScript">
MM_showHideLayers('Layer1','','show','Layer2','','hide','Layer3','','hide');
</script>
<?php
}
else if ($selection == 'Saltwater Bait')
{
?>
<script language="JavaScript">
MM_showHideLayers('Layer1','','hide','Layer2','','show','Layer3','','hide');
</script>
<?php
}
else if ($selection == 'Swim Bait')
{
?>
<script language="JavaScript">
MM_showHideLayers('Layer1','','hide','Layer2','','hide','Layer3','','show');
</script>
<?php
}
else
{
?>
<script language="JavaScript">
MM_showHideLayers('Layer1','','hide','Layer2','','hide','Layer3','','hide');
</script>
<?php
}
?>
Link to comment
https://forums.phpfreaks.com/topic/12308-running-html-and-php-heeeeelp/
Share on other sites

$selection = empty($_POST['selection']);

will not work. empty() is a boolean function that checks to see if something is empty. you would have to do it like so:
[code]
//checks to see if $_POST['selection'] is empty
if(!empty($_POST['selection'])) {
   $selection = $_POST['selection'];
   //rest of your code here
}
[/code]
A switch may be easier to use... saves some time in all your if-else-else-else's and reduces the number of squiggly brackets you are using.

On a slightly different subject: what are those squiggly brackets called?
() are parenthesis, what are these called? {}

=O
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
what are those squiggly brackets called?
[/quote]
uh, brackets? lol, now [i]that[/i] is quote worthy [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]
lol haha, I have no idea what they're called, one of my teachers called them sexy brackets though :S.

Anyways thanks for the help. So I take it I can interweave javascript and php together like that? it doesnt seem to be showing and hiding my layers :(. I've got a form with a jump menu and 3 layers drawn out. I want to show and hide those layers according to the selection :S. There must be an easier way?

[code]
<form name="form_select_bait" action="products.php" method="post">
    <font color="#000000" size="4" face="Copperplate Gothic Bold, Copperplate Gothic Light">Select Bait:</font>
      <select name="menu1" >
      <option value="Select Bait" selected>Select Bait</option>
      <option value="Hard Bait">Hard Bait</option>
      <option value="Saltwater Bait">Saltwater Bait</option>
      <option value="Swim Bait">Swim Bait</option>
      </select>
      <input name="selection" type="button" value="Go">
    </form>      
    
    <?php
          $selection = $_POST['selection'];
        
        if ($selection == 'Hard Bait')
        {
        ?>
            <script language="JavaScript">
                MM_showHideLayers('Layer1','','show','Layer2','','hide','Layer3','','hide');
            </script>
    <?php
        }
        else if ($selection == 'Saltwater Bait')
        {
        ?>
            <script language="JavaScript">
                MM_showHideLayers('Layer1','','hide','Layer2','','show','Layer3','','hide');
            </script>
    <?php
        }
        else if ($selection == 'Swim Bait')
        {
        ?>
            <script language="JavaScript">
                MM_showHideLayers('Layer1','','hide','Layer2','','hide','Layer3','','show');
            </script>
    <?php
        }
        else
        {
        ?>
            <script language="JavaScript">
                MM_showHideLayers('Layer1','','hide','Layer2','','hide','Layer3','','hide');
            </script>
    <?php
        }
        ?>
[/code]
Try a little error checking - make sure that the correct if/else selection is being opened.

If that isn't working - your javascript isn't working properly. I'm not great at JS, especially DHTML, so I wouldn't know =P

Edit: wikipedia is god. They shall now be known as curly brackets/braces. By order of Wikipedia
you can intermingle php and js but perhaps not in the way that you expect. php will parse it all and send it to the browser. but once the php is parsed, it is parsed, so if you have for instance:

[code]
<?php
   $x = 0;
   if ($x == 0) {
?>
<script lang = 'javascript'>
..
</script>
<?php
  } else {
?>
<script lang = 'javascript'>
.
.
.
</script>
<?php
  }
?>
[/code]
only the first <s cript></s cript> will actually be sent to the client, because the php script determined that the condition was true, and therefore ignored the else. So on your client, the 2nd <s cript></s cript> will not exist.
i'm a noob when it comes to js so i don't know if it's possible to grab posted vars in js or not. if so, then it would be better to just grab the posted vars from js and do the whole thing in js. if not, then you would need to pass the posted vars back to js and do your conditions in js instead of php. you would pass your posted var from php to js like so:

[code]
<script lang='javascript'>
  var selection = <?= $_POST['selection'] ?>;
</script>
[/code]

the only way you will be able to inter-mingle the two like you are wanting to do is using the AJAX method but i think that would be just overcomplicating things for what you are trying to do...I think..I guess it depends on what you are trying to do overall.
Thanks for the suggestion!

I think I am overcomplicating things.

What I want to accomplish is to have a jump menu with the following options:

[code]
     <form name="form_select_bait" action="index.php" method="get">
      <select name="menu_bait_type" >
      <option value="4" selected>Select Bait
      <option value="1">Hard Bait
      <option value="2">Saltwater Bait
      <option value="3">Swim Bait
      </select>
      <input type="button" value="Go">
    </form>  
[/code]

So depending on the option, I would like to stay on the same page say index.php and display different pictures depending on the selection.

Ive tried to check and see if my selection is working and it doesnt seem to be working.

[code]
    <?php
          $selection = $_GET['menu_bait_type'];
        echo "my $selection";
?>
[/code]

Any suggestions on attacking this problem? thanks for the help!
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.