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

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?

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>

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
}

Related

How to change action attribute in form element when I click the button?

In this code I have 3 button update,back,delete when I click anyone of it, $page should assign to action attribute in form element and post data should go based on which button is clicked.
<form action='<?php echo $page; ?>.php' method='post'>
<?php
if(isset($_POST['update'])) {
$page='book_update';
}
if(isset($_POST['delete'])) {
$page='book_delete';
}
if(isset($_POST['back']))
{
$page='patientpage';
}
?>
In PHP you can access only to the data that were already sent by the browser in the request.
If you want to detect on which button is clicked, you can name your buttons, like this:
<form action="/page.php" method="post" id="form_id">
<input type="submit" value="edit" name="action">
<input type="submit" value="delete" name="action">
<input type="submit" value="back" name="action">
</form>
After click on the button, the form is submitted and you will find the value in PHP in $_POST['action'].
If you want to modify the action attribute dynamically based on the button before the form is submitted, you have to do with javascript, e.g. document.getElementById('form_id').action = 'something-else.php';

Remove the form input fields data after click on submit?

hi i am using a form for users subscription on the website with target is _new.
when i click on the submit button that the submitting message shows on the new window but the previous window still holding the data.
How to remove input fields data after submitting.
<form target="_new" action="samplepage.php" method="post">
<input type="text" name="inputtxt1" />
<input type="text" name="inputtxt2" />
<input type="submit" value="submit" />
</form>
Any suggestions???
Make the autocomplete - off
try this
<form target="_new" action="samplepage.php" method="post" autocomplete="off">
<input type="text" name="inputtxt1" />
<input type="text" name="inputtxt2" />
<input type="submit" value="submit" />
</form>
Reset form data using this
$('form').get(0).reset();
$(document).ready(function(){
$('form_name').submit(function(){
$('input[type="text"]').val('');
});
});
You can set value to null for text field using jquery on submit button click
$("input[type=submit]").click(function()
{
$("input[type=text]").val('');
});
EDIT:
I don't know is this a good approach, use blur instead of click event
$("input[type=submit]").blur(function()
{
$("input[type=text]").val('');
});
But it will meet your need.

Two html form actions

Is it possible to create two form actions? One should action a .php page and one should action a script on the same page.
<form method="post" action="">
// Input fields here
<input type="submit" name="calculate" value="Calculate price">
// Form should perform the calculation script below (not attached here)
<input type="submit" name="order" value="Order">
// Form should perform the action 'order.php'
</form>
Searched for a long time, but could not find a solution. Does anyone know how to fix this?
If you want a button in a form to do something other than submit the form set its type to button
<input type="button" name="calculate" value="Calculate price">
then attach a click handler to the button to run your script.
Assuming you have an event that will lead to this change, yes but with javascript.
Example:
$('select#actions').on('change', function(){
var action = $('#actions').find(":selected").val();
$('#someForm').attr("action", action + '.php');//or whatever is the route
}
you can try something like this:
<script type="text/javascript">
function SubmitForm()
{
if(document.pressed == 'Calculate price')
{
document.myform.action ="calculate.php";
}
else
if(document.pressed == 'Order')
{
document.myform.action ="order.php";
}
return true;
}
</script>
<form method="post" onsubmit="return SubmitForm();">
// Input fields here
<input type="submit" name="operation" onclick="document.pressed=this.value" value="Calculate price">
// Form should perform the calculation script below (not attached here)
<input type="submit" name="operation" onclick="document.pressed=this.value" value="Order">
// Form should perform the action 'order.php'
</form>
So when you submits, trigger a function that capture your button value and set form action.
In this case, you can put your calculation script in a php file.

Pass One Input Value Into Input Type Hidden Value When User Submits

I have a simple form as outlined in the code below. I would like to append the value submitted in rm_accounts text box to the hidden page_confirm input value at the end of the URL. Hopefully that makes sense.
Essentially, if the user enters '123456' in the rm_accounts textbox, I want value of page_confirm to be http://www.mywebsite.com/index.php?rm_accounts=123456
<form name="signup" method="post" action="https://go.reachmail.net/libraries/form_wizard/process_subscribe.asp" >
<input type='text' name='rm_accounts' value='' />
<input type="hidden" name="page_confirm" value="http://www.mywebsite.com/index.php?rm_accounts=">
<input type="submit" name="Submit" value="Submit" >
</form>
All help is very much appreciated. Thanks!
Use Jquery focusout event to update hidden field value
When user enters 12345 and focus out of textbox or clicks submit(or anywhere) the below code get executed and update the value of hidden field.
$('input[type=text]').focusout(function(){
$('input[type=hidden]').val("http://www.mywebsite.com/index.php?rm_accounts="+$('input[type=text]').val());
console.log($('input[type=hidden]').val());
});
or in submit button click
$('input[type=submit]').click(function(){
$('input[type=hidden]').val("http://www.mywebsite.com/index.php?rm_accounts="+$('input[type=text]').val());
console.log($('input[type=hidden]').val());
});
Working JSFiddle link
http://jsfiddle.net/mkamithkumar/qNdny/1/
I would first suggest you give your HTML some IDs like so:
<form id="signup" method="post" action="https://go.reachmail.net/libraries/form_wizard/process_subscribe.asp" >
<input type='text' name='rm_accounts' id='rm_accounts' value='' />
<input type="hidden" name="page_confirm" id="page_confirm" value="http://www.mywebsite.com/index.php?rm_accounts=">
<input type="submit" name="Submit" value="Submit" >
</form>
Then use some jQuery for your task:
$('#signup').submit(function(){
var value = $('#rm_accounts').val();
var page_confirm = 'http://www.mywebsite.com/index.php?rm_accounts='+value;
$('#page_confirm').val(page_confirm);
});

A second button to start php script, how?

I have read the answer to this question, to execute PHP scripts with the click of a button. But what if I have a "nested button", like this :
<?php
if(!empty($_POST['act'])) {
echo "Ready to rock!";
$someVar = "Rock n Roll";
if(!empty($_POST['act2'])) {
echo $someVar;
} else {
?>
<form method="POST" action="">
<input type="hidden" name="act2" value="run">
<input type="submit" value="Rock It!">
</form>
<?php
}
} else {
?>
<form method="POST" action="">
<input type="hidden" name="act" value="run">
<input type="submit" value="Show It!">
</form>
<?php } ?>
I heard my problem can be solved with jQuery, but I no idea.
anyone please.
To execute a script on the server you use the action property of your form:
<form method="POST" action="myscript.php">
When clicking a input type="submit" the browser will go to to action of the form surrounding the input type="submit"
Nesting is not a issue, as the browser always will look for the 'surrounding' form.
Problem is in second form, so it will never calls in this code, because it fails in first $_POST variable IF statement, because in second form you do not POST variable "act". so you need to add it
<form method="POST" action="">
<input type="hidden" name="act" value="run">
<input type="hidden" name="act2" value="run">
<input type="submit" value="Rock It!">
</form>
with this form you should see echo $someVar;
p.s. if form action property is emtpy, by default it submits form to the same php script
Just like #DTukans said here, you need the hidden field. If you would post the second form, the value of act will be lost if you are not having a hidden field with the value of act from the first form.
In php you can also check which submit button you submitted by giving the input[type="submit"] a name, such as <input type="submit" name="form2">, then you could check if you submitted that form by:
if (isset($_POST['form2'])) {}, but this is not the case here.
Use the hidden input and you will be good to go.

Categories