Conditional form action...how?

Discussion in 'PHP' started by dwest, Jan 18, 2007.

  1. #1
    Hi to all,
    If I have a form such as:
    <form id="form_items" action="" method="post" enctype="multipart/form-data">
    <input type="submit" name="btn_add_items" value="Add Items" />
    <input type="submit" name="btn_del_items" value="Delete Selected Items" />
    <input type="submit" name="btn_add_custom" value="Add Custom Items" />
    </form>
    Code (markup):
    I want the following conditions to be possible:
    If "btn_add_items" is selected then post to add_items.php
    If "btn_del_items" is selected then post to del_items.php
    If "btn_add_custom" is selected then post to add_custom_items.php

    How do I code that into the form action?

    Thanks!
     
    dwest, Jan 18, 2007 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You can do it on the processing page with php, or you can use javascript to do it on the same page.

    Doing it on a form processing page is probably going to be the easiest way to do it.
     
    jestep, Jan 18, 2007 IP
  3. dwest

    dwest Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I understand that. My question is how. :)
     
    dwest, Jan 18, 2007 IP
  4. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #4
    On the PHP side you can test which $_POST variable for the checkboxes is set and act accordingly.
     
    noppid, Jan 18, 2007 IP
  5. dwest

    dwest Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hmmmm....sorry, I'm confused. There are no check boxes.
     
    dwest, Jan 18, 2007 IP
  6. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #6
    Opps, the submit button values can be used the same way.
     
    noppid, Jan 18, 2007 IP
  7. West

    West Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Simple.

    
    if (isset($_POST['btn_add_items'])) {
        // do this block of codin
    } elseif (isset($_POST['btn_del_items'])) {
       // do this block of coding
    } elseif (isset($_POST['btn_add_custom'])){
      // do this block of coding
    }
    
    PHP:
    Hope this helps.
     
    West, Jan 18, 2007 IP
  8. dwest

    dwest Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks West,
    I understand the php part of this. Perhaps I should have been more clear.

    I need to know how to apply the conditional code to the form action. How do I put the code in the <form action="">?

    Make sense?
    Thanks!
     
    dwest, Jan 18, 2007 IP
  9. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #9
    Usually the page can do more then one job. <form action="filename.php">

    
    if (isset($_POST['btn_add_items'])) {
        // do this block of codin
    } elseif (isset($_POST['btn_del_items'])) {
       // do this block of coding
    } elseif (isset($_POST['btn_add_custom'])){
      // do this block of coding
    }
    
    
    //if we fall through, display the form.
    
    PHP:
    this is a simplistic example.
     
    noppid, Jan 18, 2007 IP
  10. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #10
    mad4, Jan 18, 2007 IP
  11. West

    West Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Did anyone ask for an advanced one, or did I get lost somewhere?
     
    West, Jan 18, 2007 IP
    dwest likes this.
  12. dwest

    dwest Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Somehow my question isn't being understood...
    I want to know how to make the form's ACTION be conditional.

    <form action=""> What goes in between the "" to accomplish the conditional submittal depending on the button selected??

    Can it be a php function perhaps?
     
    dwest, Jan 18, 2007 IP
  13. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #13
    Oh, then you need javascript perhaps. I'm no JS expert.

    West, The example I posted in whole was simplistic to what I referred to. Your code was fine.
     
    noppid, Jan 18, 2007 IP
  14. West

    West Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Noppid, I got you. I read it wrong. Apologies. :)

    Also, I agree with you. This is definitely not a PHP job. Sounds more like a javascript job. This needs to happen before post, but after load and that's not PHP.
     
    West, Jan 18, 2007 IP
    noppid likes this.
  15. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #15
    
    <script language="javascript">
    function formaction( str )
    {
    	switch( str )
    	{	
    		case "add":
    		document.form_items.action = 'add_items.php';
    		document.form_items.submit();
    		break;
    		
    		case "del":
    		document.form_items.action = 'del_items.php';
    		document.form_items.submit();
    		break;
    		
    		case "cus":
    		document.form_items.action = 'add_custom_items.php';
    		document.form_items.submit();
    		break;
    	}
    }
    </script>
    <form id="form_items" name="form_items" action="" method="post" enctype="multipart/form-data">
    <input type="button" value="Add Items" onClick="formaction('add')"/>
    <input type="button" value="Delete Items" onClick="formaction('del')"/>
    <input type="button" value="Add Custom Items" onClick="formaction('cus')"/>
    </form>
    
    HTML:
     
    krakjoe, Jan 18, 2007 IP
    West and noppid like this.
  16. West

    West Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Thanks for that. :) Looks like it should work let's hope it does. :)
     
    West, Jan 18, 2007 IP
  17. dwest

    dwest Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Yes, thanks indeed. But is there no way to do the same thing with PHP??
     
    dwest, Jan 18, 2007 IP
  18. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #18
    We have actually shown you how to do this in both PHP and JS now. Perhaps there is still a communication gap?
     
    noppid, Jan 18, 2007 IP
    dwest likes this.
  19. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #19
    php is a server side language, it cannot interact that way with html, there are two ways to do it, and you now have both, so choose ....
     
    krakjoe, Jan 18, 2007 IP
    dwest likes this.
  20. dwest

    dwest Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Will do. Thanks.
     
    dwest, Jan 18, 2007 IP