tags:

views:

20

answers:

2

I have a form in CodeIgniter, and I need to change the Action of the form based on what is in the actual form.

ie: if <select name="type"> == business then I need action="business/submit"

Is there a simple way to do this?

Right now my attempted workaround is to use Javascript to grab the data from the inputs, then send it to a dynamically generated link - however it's not ideal for my situation.

Thoughts?

A: 

I'm not familiar with CodeIgnitor, but can't you send the form to a single Action that would read the type and forward the request to whichever Action you want it to go to?

Scott Saunders
+1  A: 

I have never worked on CodeIgniter but I can tell you how you can do it in PHP.

You have to understand that you cannot do this "dynamically" without Javascript. As you suggested that you do not wish to use Javascript, I am suggesting a dirty way to handle this situation.

You cannot modify the action attribute with PHP alone, so we will have to post it to one PHP file and then based on the selection (from your select box) you can include the file that you need. You can use switch case or if based on your preference.

This is just indicative. Please do not flame me for not following standards or not taking any security measures !

<?php 
     // sample code snippet 

      if($_POST['type']=== 'something') { 
          include 'something.php'; 
      } 
      else if($_POST['type']=== 'somethingElse') { 
           include 'somethingElse.php'; 
      } 
      else { 
           include 'totallyDifferentOne.php'; 
      }

     // continue your code 

?>
Amit Dugar
Well, it's not that I'm opposed to using Javascript, just don't like the one I'm using. What I will probably do is actually use jQuery and set the ACTION attr to something else onSubmit.
gamerzfuse
That is a neater way of doing it on client-side, but do consider the case if your form user is on a device that does not have javascript. He/She/It may not be able to submit the form, or he/she/it will end up submitting the form to the wrong file.Never assume anything !
Amit Dugar