Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    77
    Thanks
    4
    Thanked 0 Times in 0 Posts

    submit button in form

    I have a form and I want the submit button to execut action depend on the option selected.

    I have a dropdown menu with:
    action 1
    action 2
    action 3

    and a submit button.
    If action 1 is selected, when submit, do action1.php, if action 2 is selected, when submit, do action2.php, and so on....

    ty

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    How about when they click any of the options,
    javascript sends to one PHP script ... that script
    determines what the value is and redirects the user.
    That redirection is invisible to the user.

    If that's OK, you want to use javascripting:

    $('select').change(function() {
    $('form').submit();
    });

    And then in the PHP script, redirect ...

    <?php
    // default page
    $page="index.php";
    if($_POST['action']=="1"){
    $page="action1.php";
    }
    if($_POST['action']=="2"){
    $page="action2.php";
    }
    if($_POST['action']=="3"){
    $page="action3.php";
    }
    header("location: $page");
    ?>

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    77
    Thanks
    4
    Thanked 0 Times in 0 Posts
    ok, but where I insert this?

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    The PHP script can be called "action.php".
    It gets saved all by itself.

    In your main page, you'll put the
    javascript function near the top,
    and your form wherever the select
    box is.

    Something like this (I found this example using Google):
    Code:
        <script type="text/javascript">
        function FormSubmit() {
        document.myform.action = 'action.php';
        document.myform.submit();
        return;
        }
        </script>
         
        <form method="post" name="myform">
         
        <select name="action" onChange="FormSubmit(this)">
        <option value="1">1st choice</option>
        <option value="2">2nd choice</option>
        </select>
        </form>

  • #5
    New Coder
    Join Date
    Nov 2011
    Posts
    77
    Thanks
    4
    Thanked 0 Times in 0 Posts
    it works but... I have a problem.

    this is a search form.... using action GET
    and when I submit... just go to page1, page2 or page3 without showing the search results.

    I need this to use 3 types of page results (action1 = page1, actio2 = page2, action3=page3)

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Show me your script that you call "action.php".
    The script that does the actual search.

  • #7
    New Coder
    Join Date
    Nov 2011
    Posts
    77
    Thanks
    4
    Thanked 0 Times in 0 Posts
    <?php
    // default page
    $page="index.php";
    if($_POST['action']=="1"){
    $page="page1.php";
    }
    if($_POST['action']=="2"){
    $page="page2.php";
    }
    if($_POST['action']=="3"){
    $page="page3.php";
    }
    header("location: $page");
    ?>

    and the search code is in page1, page2, and page3...
    but I need to GET the words from the form selected on the page from where was use submit button

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    ooooh I see ...


    <form method="post" name="myform">
    Search For: <input type="text" name="target"> &nbsp;
    <select name="action" onChange="FormSubmit(this)">
    <option value="1">1st choice</option>
    <option value="2">2nd choice</option>
    </select>
    </form>


    <?php
    // default page
    $page="index.php";

    if($_POST['target']){
    $target=$_POST['target'];

    if($_POST['action']=="1"){
    $page="page1.php?t=$target";
    }
    if($_POST['action']=="2"){
    $page="page2.php?t=$target";
    }
    if($_POST['action']=="3"){
    $page="page3.php?t=$target";
    }

    }// end if target
    header("location: $page");
    ?>


    On the page1, page2, page3

    if($_GET['t']){
    $target=$_GET['t'];
    }

  • #9
    New Coder
    Join Date
    Nov 2011
    Posts
    77
    Thanks
    4
    Thanked 0 Times in 0 Posts
    and if I have 2 or more "target" ?
    search for target1 AND target2 AND target3 .....


    and now...
    Is listing all data from db... not the target word
    Last edited by _user; 02-21-2012 at 08:58 PM.

  • #10
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I guess you add more text lines ...
    name="target1"
    name="target2"

    And add them to the URL variable in the 2nd script.

    Your URL will get sort of long if you have too many, but it shouldn't hurt anything.
    You could also save the search targets in cookies and not even have them
    show in the URL. I guess it depends on what you're searching and what is
    supposed to happen if the search doesn't find anything. A lot of things ...
    but I don't have enough information to answer.

  • #11
    New Coder
    Join Date
    Nov 2011
    Posts
    77
    Thanks
    4
    Thanked 0 Times in 0 Posts
    if 0 results... echo 0 results found )
    well... isn't a simple way?


    or... how I can do this...
    if action1 is selected outputsearch = " .... way1" and if action2 is selected outputsearch = " ....way2"

  • #12
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Let me ask this ...
    What are you searching? A database?
    and what is the purpose of the script?

    I guess I don't even understand what your search thing is supposed to do.
    Where is the scripting that actually does the search?

  • #13
    New Coder
    Join Date
    Feb 2012
    Posts
    40
    Thanks
    0
    Thanked 9 Times in 9 Posts
    I think this may be what your after:
    Code:
    <script type="text/javascript">
    function checkAct() {
    	var f = document.getElementById('YOUR_FORM_ID');
    	var s = document.getElementById('SELECT_ID');
    	if( s.selectedIndex == 1 ) { 
    		f.setAttribute("method","POST");
    		f.setAttribute("action",s.options[1].value) ;  
    		f.submit();
    	}
    	if( s.selectedIndex == 2 ) { 
    		f.setAttribute("method","POST");
    		f.setAttribute("action",s.options[2].value) ;  
    		f.submit();	}
    }
    </script>
    </head>
    <body>
    <form id="YOUR_FORM_ID" >
    <select id="SELECT_ID" onchange="checkAct()">
    <option value="">choose your action</option>
    <option value="action1.php">action-ator1</option>
    <option value="action2.php">action-ator2</option>
    </select>
    </form>
    You can omit the f.submit() if you dont wont the form to submit upopn selection
    Last edited by cercos; 02-21-2012 at 11:23 PM.

  • #14
    New Coder
    Join Date
    Nov 2011
    Posts
    77
    Thanks
    4
    Thanked 0 Times in 0 Posts
    ty ty ty ty...
    but please explain and the last part... with f.submit().
    ty again

  • #15
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Please tell us what you're searching.
    A database?
    HTML pages?
    ??

    The answer depends on knowing what you're goal is.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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