XAF
XAF

Reputation: 1472

Get 1 input field value to 2 different controllers by using 2 buttons

I have a <form> which currently sends the input field data to 1 function in the controller. Which looks like this.

<?php echo form_open('moneyexchange/invest_first_page');  ?>
  <input  type="text" placeholder="Amount in &euro;"  name="writtenamount"/>
    <button class="btn btn-lg" type="submit" >Invest</button> 
    <button class="btn btn-lg" type="submit" >Borrow</button>
<?php echo form_close();?> 

The problem is I need Invest button to go to invest_first_page function inside money exchange controller, and the Borrow button to go to a different function inside the same controller called borrow_first_page. I need to use one input field and it is a must, so cannot use two input fields and then after that they go to the function they are redirected to two different pages,so is there any way to get this form to post the input value to two different controller when clicked on different buttons.

Just for extra information I am posting both the function in controller which the buttons direct too.

    public function invest_first_page(){
$this->load->helper('form');
         $this->load->library('form_validation');
        $this->load->view('header');
        $userProvidedAmount = $this->input->post("writtenamount");
        $data =  array(
        'userProvidedAmount' => $userProvidedAmount
         );
        $this->load->view("invest_firstpage", $data);
        $this->load->view('footer');
                }

 public function borrow_first_page(){
        }

Can I use JavaScript be used to do it?

Upvotes: 1

Views: 62

Answers (4)

Masnad Nihit
Masnad Nihit

Reputation: 1996

Paul Rolas code is perfect but there is 2 things you need to consider, since your not using value in your place holder so your data should be like this

data: { writtenamount: $("#amount").data('value')},

And most probably its better to use method instead of type

method: 'POST',

Hope this helps.

Upvotes: 1

prola
prola

Reputation: 2823

Example Below

    <html>
<head>
    <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script>
        $( document ).ready(function() {
            $('#yourbuttonid').click(function() { 
                $.ajax({
                     url: 'moneyexchange/invest_first_page',
                     type: 'POST',
                     data: { writtenamount: $("#amount").val()},
                     success: function (result) {
                       // do stuff
                     }
                });
            });
        });
    </script>
</head>
<body>
    <?php echo form_open('moneyexchange/invest_first_page');  ?>
        <input  type="text" placeholder="Amount in &euro;"  name="writtenamount"/>
        <button id="yourbuttonid" class="btn btn-lg" type="submit" >Invest</button> 
        <button class="btn btn-lg" type="submit" >Borrow</button>
    <?php echo form_close();?>
</body>
</html>

Upvotes: 2

Glubus
Glubus

Reputation: 2855

Couple of things you can do.

1) Give your buttons a name attribute

You're saying that you are restricted to only one input field, but you forget that the button you click can also be used to pass data. Then in your php script you'll check which button was clicked and then do whatever functionality you need based on that.

2) Use Ajax to send your postdata

You could use javascript (or preferably jQuery) to handle the post request for you. You give both buttons a different id-tag, so <button id="button1"> and <button id="button2">. With this information you can then use javascript to find which button was clicked, and make an ajax request to the right php function.

Upvotes: 0

harmoniemand
harmoniemand

Reputation: 259

you could use the OnSubmit event of the form to do that. (look here) You could simply catch the event and route to the needed controller.

Upvotes: 0

Related Questions