www.webdeveloper.com
Results 1 to 8 of 8

Thread: Passing PHP variables

  1. #1
    Join Date
    Oct 2004
    Posts
    65

    Passing PHP variables

    Passing PHP variables

    Re: Passing variables along in a form (POST).

    I'm using the following script which allows selections to be chosen without the user having to hit submit. However, I can't seem to integrate any hidden input fields for passing my variables along (my preferred method). I don't want to use GET (ie ?variable='var' etc) and sessions are proving inflexible. Any ideas?

    PHP Code:
    <script type="text/javascript">
    function 
    nav1()
       {
       var 
    document.myform1.home.selectedIndex;
       var 
    url_add document.myform1.home.options[v].value;
       
    window.location.href url_add;
       }
    </script> 
    <FORM NAME="myform1">
    Select Field:&nbsp;&nbsp;
    <SELECT NAME="home" onChange="nav1()">
    <OPTION VALUE="">Select Field
    <OPTION VALUE="">
    <option value="<?php echo $url.'/index1.php; ?>">Field 1
    <option value="<?php echo $url.'/index2.php; ?>">Field 2
    </SELECT>
    </FORM>
    Simon
    Simon Duncan
    BusyView

  2. #2
    Join Date
    Sep 2008
    Posts
    95
    One option may be to "POST" the form. This eliminates the GET url chain you're looking to avoid and prevents using SESSION stuff. Just set the target of the form to the URL you're looking to send to and "SUBMIT". Something like the following (untested) code should do the trick:

    Code:
    <script type="text/javascript"> 
    function nav1() 
       { 
       var v = document.myform1.home.selectedIndex; 
       var url_add = document.myform1.home.options[v].value; 
       document.forms['myform1'].target = url_add; 
       document.forms['myform1'].submit();
       } 
    </script> 
    
    
    
    <FORM NAME="myform1" method='POST'>
    Select Field:&nbsp;&nbsp;
    <SELECT NAME="home" onChange="nav1()">
    <OPTION VALUE="">Select Field
    <OPTION VALUE="">
    <option value="<?php echo $url.'/index1.php; ?>">Field 1
    <option value="<?php echo $url.'/index2.php; ?>">Field 2
    </SELECT>
    
    <input type='hidden' id='myHidden1' name='myHidden1' value='Something'>
    
    </FORM>

  3. #3
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    You can submit the form without a separate javascript function just do this

    PHP Code:
    <form action='<?php echo $_SERVER[PHP_SELF?>' method='POST'>
    <SELECT name='home' onchange="this.form.submit();">
    <OPTION VALUE="">Select Field
    <OPTION VALUE="">
    <option value="<?php echo $url.'/index1.php'?>">Field 1  // added ' after .php
    <option value="<?php echo $url.'/index2.php'?>">Field 2  //           "
    </SELECT>
    </FORM>

  4. #4
    Join Date
    Oct 2004
    Posts
    65

    no joy yet, but reckon I'm closer!

    A big thanks for your help Dasher and speghettiCode.

    speghettiCode I reckon if I could get this portion of your code to work, I'd be there:

    Code:
    document.forms['myform1'].target = url_add; 
    document.forms['myform1'].submit();
    But it's not working. My form was diverting successfully to a selected page when using the following:

    window.location.href = url_add;
    ... but of course, no hidden variables were being submitted. I guess because I'm was using Javascript to select the page, rather than submitting. I'm guessing if I can use submit() then I'd be using the form properly and my hidden variables would be submitted?


    Dasher... I also, (using your solution which I don't fully understand):

    <form action='<?php echo $_SERVER[PHP_SELF] ?>' method='POST'>
    ... fail to select a page based on a user's selection (nb. I've noted the added '; after .php).

    Can I really divert browsers to a different page depending on their selection without using javascript? This would be good if possible.

    Simon
    Simon Duncan
    BusyView

  5. #5
    Join Date
    Sep 2008
    Posts
    95
    Does it throw an error?

    If you put:

    Code:
    alert(url_add);
    document.forms['myform1'].target = url_add; 
    document.forms['myform1'].submit();
    Does it alert the url_add you were intending the page to divert to?

  6. #6
    Join Date
    Oct 2004
    Posts
    65
    Yes... the alert works. When I add:

    alert(url_add);
    ... a box appears giving me the url of the page I need it to go to. But I can't get it to go there... which it does if I use:

    window.location.href = url_add;
    .

    instead of:

    document.forms['myform1'].target = url_add;
    document.forms['myform1'].submit();

    Strange?

    Am willing to persevere with your solution since it uses submit, which I therefore hope will enable me to pass hidden variables in the form.
    Simon Duncan
    BusyView

  7. #7
    Join Date
    Sep 2008
    Posts
    95
    Oops! My mind was elsewhere...

    It's not "target" you're after...it's "action".

    Try this:

    Code:
    <script type="text/javascript"> 
    function nav1() 
       { 
       var v = document.myform1.home.selectedIndex; 
       var url_add = document.myform1.home.options[v].value; 
       document.forms['myform1'].action = url_add; 
       document.forms['myform1'].submit();
       } 
    </script>
    I've verified it works.

    Target specifies the window/frame the form submits in.
    Action specifies the page the form submits to.

    Apologies for the previous lapse of concentration.


    P.S. I would recommend implementing Dasher's observation...The php code is missing a closing ' after the ".php" part of each of your options in your form.
    Last edited by speghettiCode; 10-14-2010 at 05:48 AM.

  8. #8
    Join Date
    Oct 2004
    Posts
    65

    Working! Thanks!

    Yes yes yes! Massive thank you!

    Working.

    I knew you were onto something. Javascript! It's been on my to do list for about 7 years, but I never get around to it.

    Yes, I've implemented Dasher's suggestion too. Thanks both.

    Simon
    Simon Duncan
    BusyView

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles