dot
dot

Reputation: 15680

codeigniter - how to create two forms that "share" a input field

I'm wondering how to resolve an issue where I have one text box and two buttons. Each button needs the same data in the text box to accomplish its task.

One button is to update the existing record they are reviewing (with the new value in the text box), and the other button is used to add a new record (again, using the new value in the text).

One idea I had was to use jquery to update a hidden text box that gets updated when the visible text box is modified by the user.

So something like this: (this is just pseudocode...)

 <form name="form1" method="post" action="controller1/method1">
     <input type=text name=visibleTextBoxForForm1></input>
     <button type=submit value=UPdate>
 </form>
 <form name="form2" method="post" action="controller2/method2">
     <input type=hidden name=hiddenTextBoxforForm2></input>
     <button type=submit value=New>
 </form>
 <script>
        $('#visibleTextBoxForForm1').live('change', function() {
             //update a hidden textbox in form2 with value of this textbox.
         });
 </script>

Is there a better way to do this?

Upvotes: 1

Views: 809

Answers (2)

dispake
dispake

Reputation: 3329

Alternatively, you could do it via JQuery. Tie a clicklistener for each button and provide the correct URL to the form on click.

Here's some quick code... you'd have to correct the proper jquery queries for the correct elements.

<form name="form1" method="post">
   <input type=text name=visibleTextBoxForForm1></input>
   <button type=button value=Update>
   <button type=button value=New>
</form>

<script>
    $('update').click(function() {
          $(form1).attr('action', <update url>).submit();
    });

    $('new').click(function() {
          $(form1).attr('action', <new url>).submit(); 
    });
</script>

Upvotes: 1

Major Productions
Major Productions

Reputation: 6042

If that's the only field, then simply have one form with two buttons and handle that text data based on the name of the button used to submit it.

<form name="form1" method="post" action="controller1/method1">
    <input type="text" name="text" />
    <input type="submit" name="insert" value="Insert New Data" />
    <input type="submit" name="update" value="Update Existing Data" />
</form>

PHP (not CodeIgniter, since I'm not familiar with that framework):

if(isset($_POST['insert'])) {
    // insert $_POST['text']
} else if (isset($_POST['update'])) {
    // update $_POST['text']
} else {
    // error
}

Upvotes: 0

Related Questions