Action depending on drop down

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gnawz
    New Member
    • Nov 2006
    • 64

    Action depending on drop down

    Hi

    I have a drop down thus

    [code=html]
    <form name = frmUtils method = POSTaction = "">
    <select name = Do>
    <option>Add</option>
    <option>Delet e</option>
    <option>Updat e</option>
    </select>
    </form>[/code]

    I want the form action to change based on the selection ie

    if selected Index is Add
    action = add.php


    if selected Index is Delete
    action = delete.php


    if selected Updateis Add
    action = update.php

    Somebody help
    Last edited by pbmods; Jul 24 '07, 12:38 AM. Reason: Added CODE tags.
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    Originally posted by gnawz
    Hi

    I have a drop down thus


    <form name = frmUtils method = POSTaction = "">
    <select name = Do>
    <option>Add</option>
    <option>Delet e</option>
    <option>Updat e</option>
    </select>
    </form>

    I want the form action to change based on the selection ie

    if selected Index is Add
    action = add.php


    if selected Index is Delete
    action = delete.php


    if selected Updateis Add
    action = update.php

    Somebody help
    Thats sounds tricky. Could you use some javascript on the dropdown onchange event? This could use document.getEle mentById to write the innerHtml of the form (give the form an id that is the same as the name) and add the appropritae action.

    That would be the lines of my first attempt. Have a go and let me know how you get on. I'll be happy to help.

    Cheers
    nathj

    Comment

    • kovik
      Recognized Expert Top Contributor
      • Jun 2007
      • 1044

      #3
      You should approach the situation differently.

      [code=php]<?php
      if(!empty($_POS T['do']))
      {
      switch($_POST['do'])
      {
      case 'add':
      case 'update':
      case 'delete':
      header('Locatio n: http://www.mydomain.tl d/' . $_POST['do'] . '.php');
      }
      }
      ?>
      <form method ="post" action = "">
      <select name ="do">
      <option value="add">Add </option>
      <option value="delete"> Delete</option>
      <option value="update"> Update</option>
      </select>
      </form>[/code]

      Comment

      • nathj
        Recognized Expert Contributor
        • May 2007
        • 937

        #4
        Originally posted by volectricity
        You should approach the situation differently.

        [code=php]<?php
        if(!empty($_POS T['do']))
        {
        switch($_POST['do'])
        {
        case 'add':
        case 'update':
        case 'delete':
        header('Locatio n: http://www.mydomain.tl d/' . $_POST['do'] . '.php');
        }
        }
        ?>
        <form method ="post" action = "">
        <select name ="do">
        <option value="add">Add </option>
        <option value="delete"> Delete</option>
        <option value="update"> Update</option>
        </select>
        </form>[/code]
        I see where you are coming from. Use the same action but check what to do on the receiving page. A much slicker solution.

        This isn't even my issue and I found that helpful. Thanks for the tip. I don't know what I was thinking.

        I would change the !empty() to isset() in this example though, I find it to be a more comprehensive check, and being an optimist I prefer to check that something is done rather than not done.

        All in all a much better, less compicated solution than my original thought. I think I may have got a bit stuck on using a technology just because I can - a rookie mistake.

        Cheers
        nathj
        Last edited by nathj; Jul 24 '07, 06:00 AM. Reason: Adding comments

        Comment

        • gnawz
          New Member
          • Nov 2006
          • 64

          #5
          these are my files

          reports.php
          [code=php]
          $Report = $_GET['report'];

          switch ($Report)
          {

          case 'add' :

          addfunction();

          break;

          case 'delete' :

          deletefunction( );

          break;

          case 'update' :

          updatefunction( );

          break;
          }
          [/code]

          the other file with my select...

          checkrpt.php
          [code=javascript]
          <form name="frmUtils" method="post" action="">

          <select name="Do" id ="Do"onChange=" SwapReport();">
          <option value="0">Selec t view</option>
          <option value="1">Add</option>
          <option value="2">Updat e</option>
          <option value="2">Delet e</option>
          </select>
          </form>


          <script type="text/javascript">

          function SwapReport()
          {
          if (document.frmUt ils.Do.selected Index == 1)
          {
          document.frmUti ls.action = "reports.php?re port=add";
          }
          else if (document.frmUt ils.Do.selected Index == 2)
          {
          document.frmUti ls.action = "reports.php?re port=update";
          }
          else (document.frmUt ils.Do.selected Index == 3)
          {
          document.frmUti ls.action = "reports.php?re port=delete";
          }

          }
          </script>[/code]

          Help from here. It is not working yet.

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            I think this is a syntax error. Should be else if.
            Code:
            else (document.frmUtils.Do.selectedIndex == 3)
            Unless you want
            Code:
            document.frmUtils.action = "reports.php?report=delete";
            as a default then remove the condition after else
            Code:
            else {
            document.frmUtils.action = "reports.php?report=delete";
            You originally asked to make the form action conditional so why not
            <script type="text/javascript">
            [HTML]function SwapReport()
            {
            if (document.frmUt ils.Do.selected Index == 1)
            {
            document.frmUti ls.action = "add.php";
            }
            else if (document.frmUt ils.Do.selected Index == 2)
            {
            document.frmUti ls.action = "update.php ";
            }
            else if(document.frm Utils.Do.select edIndex == 3)
            {
            document.frmUti ls.action = "delete.php ";
            }
            }
            </script>[/HTML]

            Comment

            • gnawz
              New Member
              • Nov 2006
              • 64

              #7
              Still not working...
              I appreciate all the help people

              Comment

              • nathj
                Recognized Expert Contributor
                • May 2007
                • 937

                #8
                Originally posted by gnawz
                Still not working...
                I appreciate all the help people
                In what way? What exactly is not working?

                Do you get any error messages at all?

                Cheers
                nathj

                Comment

                • gnawz
                  New Member
                  • Nov 2006
                  • 64

                  #9
                  It just won't do anything at all.

                  Comment

                  • nathj
                    Recognized Expert Contributor
                    • May 2007
                    • 937

                    #10
                    Originally posted by gnawz
                    It just won't do anything at all.
                    Hi gnawz,

                    Sorry to go on like this but have you turned error reporting on?

                    This will really help to track down what may be going on.

                    When you say it won't do anything what do you mean:
                    1) Does the drop down appear on the page?
                    2) If so is there data in the drop down?
                    3) Can you select the data in the drop down?
                    4) What happens when you tab out of the drop down (or move focus in some other way)? Do you get any error messages?
                    5) Have you checked the generated source code view using something like FireFox with the web developer add-on, if so what doe sit say for the code in each case?

                    Perhaps answers to these will shed a bit more light on it, otherwise it's a complete black out from this side.

                    Cheers
                    nathj

                    Comment

                    • gnawz
                      New Member
                      • Nov 2006
                      • 64

                      #11
                      The code does not swap any thing n I do not get any errore.

                      Yes I can select but when I do,nothing happens.

                      No change in the page and nothing moves.

                      I'm stuck

                      Comment

                      • kovik
                        Recognized Expert Top Contributor
                        • Jun 2007
                        • 1044

                        #12
                        Originally posted by nathj
                        I would change the !empty() to isset() in this example though, I find it to be a more comprehensive check, and being an optimist I prefer to check that something is done rather than not done.
                        empty() is actually a more comprehensive check, as it does what isset() does as well as check that the data has a value. And, in this case, we have no use for an empty value, therefore empty() is ideal.

                        Comment

                        • kovik
                          Recognized Expert Top Contributor
                          • Jun 2007
                          • 1044

                          #13
                          Originally posted by gnawz
                          The code does not swap any thing n I do not get any errore.

                          Yes I can select but when I do,nothing happens.

                          No change in the page and nothing moves.

                          I'm stuck
                          Why is no one asking the obvious question? Where do you define addfunction(), updatefunction( ), and deletefunction( ), and what is it that they actually do? If nothing is being displayed, it's because of them.

                          Comment

                          • nathj
                            Recognized Expert Contributor
                            • May 2007
                            • 937

                            #14
                            Originally posted by volectricity
                            empty() is actually a more comprehensive check, as it does what isset() does as well as check that the data has a value. And, in this case, we have no use for an empty value, therefore empty() is ideal.
                            volectricity,

                            Thanks for the clarity there. i had misunderstood some documentation I read, obviously reading in a rush as I learn all this stuff.

                            Cheers
                            nathj

                            Comment

                            • gnawz
                              New Member
                              • Nov 2006
                              • 64

                              #15
                              My functions are defined in the same page they are called.

                              Comment

                              Working...