Suppose I have two form in individual pages. One is Called ParytForm.html and second one is clientForm.html.
And I have a single PHP file which contains two php functions. One is PartyForm() and second one is clientForm().
Now is it possible, when user fill the form form PartyForm.html then partyForm() is called. and when user fill the form from clientForm.html then
clientForm() is called.
Thanks
In one form put
<input type="hidden" name="from" value="ParytForm.html" />
and in the other:
<input type="hidden" name="from" value="clientForm.html" />
then in your PHP code distinguish your input:
if ('clientForm.html' == $_POST['from'])
{
// do stuff
PartyForm();
}
else if ('ParytForm.html' == $_POST['from'])
{
// do other stuff
clientForm();
}
I adapted ParytForm.html for fun ;-)
You can send a variable by GET method ("do=partyform"), check it in php and call right function.
You can do this multiple ways, one way is to include a hidden field in each form, and then use a conditional after you submit to run the right function.
Your html code below
<form action="action.php" method="post">
...
<input type="hidden" name="formName" value="PartyForm">
<input type="submit" value="Submit">
</form>
<form action="action.php" method="post">
...
<input type="hidden" name="formName" value="ClientForm">
<input type="submit" value="Submit">
</form>
and then in your action.php or whatever you call the page you submit to
if($_POST['formName'] == "PartyForm"){
partyForm();
}else if$_POST['formName'] == "ClientForm"){
clientForm();
}
To do this, you will need some way of the PHP program to distinguish between the two requests. This can be done by adding a parameter to the URL the form is submitting to, or changing the name of the submit button. I tend to prefer the URL method though, because it is cleaner.
for example
Form 1 could be
<form method="phpfilename.php?dofunction=1">
Form 2 could be
<form method="phpfilename.php?dofunction=2">
Then, on your PHP file, you could check which form is sent using
$form = $_GET['dofunction'];
if ($form == '1') doFunction1();
else if ($form == '2') doFunction2();
The best way to do this (I think) is to use the PHP include() function. Use your forms inside a .php file (which can include HTML only), and include the functions file in both form files.
Then, just use action="clientForm()" to call the function when the form is submitted.
Related
I have a smarty project, and in the .tpl file, there is a form:
<form method="get" action="{$smarty.server.PHP_SELF}?action=func1">
<input type="text" name="username"/>
<input type="submit">
</form>
there is a question, if the php file have many function for different action requests, so in the template if have many forms, I want to through the action for distinguish.
but in my practice, see upper code, I write like this, this can not delivery the action to my php file.
I want to write the action in the form action, because this can be more standard. so I don't want to write it in a hidden input. why write in the action can not pass into the php file?
You could use submit button with specified name and value:
<form method="get" action="{$smarty.server.PHP_SELF}">
<input type="text" name="username" />
<input type="submit" name="action" value="func1" />
</form>
and then you'll get a global variable $_POST['action'] with value func1. But the value will be showing on your button title, so I offer you to find forms only by submit name, for example name='submit_form1'.
I have my page as follows:
<form id="filterQuery" action="#" method="post">
...
<input type="submit">
</form>
<?php
// use $_POST to determine query restrictions and conditions
?>
Now my problem is with the action of the form, because if I make it "#" self, nothing happens when clicking submit.
I'm using a template which shows subpages as include in <div>s. In other words, my page is not a standard HTML page which contain <head> and <body> etc, but is in the following format: basic.php#!/my_sub_page.
Trying to make action "basic.php#!/my_sub_page" results in an empty _POST
I tried your example and everything works fine with /index.php#!/smth in action and post variables.
<form action="#!/something" method="post">
<input type="text" name="var1" value="123">
<input type="submit"></form>
<?php
print_r($_POST);
?>
This is how I check it.
Are you sure you didn't forget name attributes in inputs?
Do you see their values in url after ? if you change method to get?
How do I link the PHP to the HTML form? I understand how to do the PHP and how to do the HTML, but how do I link the php to the html or is that automatic,
how does the HTML form know about the PHP?
In your form set the action attribute to the path of your php script eg:
<form action="/path/to/php/script.php" method="post">
...
</form>
You set your action="" in your form to point to your PHP script. When the user clicks the submit button in your form, the PHP script will be called and the formdata will be handed over to the PHP script.
The method you choose when making your form is how PHP will gather the values passed in.
As such:
<form action="handler.php" method="post">
<!-- OR -->
<form action="handler.php" method="get">
The action tells where the form values will be sent to and the method tells how the values of the items in the form will be passed back to the server. The post method will send the values back so they may be retrieved by the $_POST array (both post and get can be retrieved by the $_REQUEST array). For example:
<input type="text" name="myInput">
Will post back to the server and can be retrieved by
$var = $_POST['myInput'];
It's always best to test if there is actually an input, and the following can be used
if(isset($_POST['myInput'])) { /*do something if set*/}
else{ /*do something if not set*/}
If the form was submitted by the get method, the values of the form is passed back in the URL, like such:
http://www.domain.tld/handler.php?myInput=someValue
The value is then retrieved by using the $_GET array:
$var = $_GET['myInput'];
Once again, you should test that it exists.
For good examples and explanations, please read a PHP book or search for PHP and HTML forms. This is the very basics of PHP.
Try this in a php file
<form action="" method="post">
<input type="text" value="html form data" name="name" />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST['submit']))
echo 'I am php. I know this value is from html - '. $_POST['name'];
?>
If you are talking about refilling your form with the php values: inside your input fields, just add the request variable.
<input type="text" name="input1" value="<?=$_REQUEST['var_name']?>" />
If you are talking about sending date to php, just point to a file using the form action.
<form action="file.php" method="post">
</form>
Then you process all the data in that php file.
I am trying to save my form data into a wordpress database before submitting it. Please help. Heres my code and the php code to insert into database:
<form method="post" action="https://app.icontact.com/icp/signup.php" name="icpsignup"
id="icpsignup1030" accept-charset="UTF-8" onsubmit="return verifyRequired1030();">
<input type="hidden" name="redirect" value="http://www.123.com/thank-you.html">
<input type="hidden" name="errorredirect" value="http://www.123.com/error.html">
<input type="hidden" name="listid" value="16360">
<input type="hidden" name="specialid:16360" value="MA3A">
<input type="hidden" name="clientid" value="1259610">
<input type="hidden" name="formid" value="1030">
<input type="hidden" name="reallistid" value="1">
<input type="hidden" name="doubleopt" value="0">
<input type="text" name="fields_email" size="21" style="padding:5px;">
<input type="image" name="submit" src="signup button top.png" value="" height="32px">
</form>
And the code to insert into DB:
<?php
require_once('../../../wp-config.php');
global $wpdb;
$table_name = "icontact_emails";
$email = $_POST[fields_email];
if (trim($email) != ""){
$wpdb->insert($table_name, array('email' => $email, 'timestamp'=> date("Y- m-d h:i:s")));
}
?>
"Submitting" is the only way to transfer it to the server, whether you submit the form via AJAX or traditional methods - no difference.
Remember, this is a client-server model. The form is on the client, the DB is on the server.
I can INFER from your question that you really mean: I am trying to save this form to a database before I submit it to ANOTHER SITE which I don't control. Meaning you want to save it to your own database before you submit the data to a 3rd party.
If that's the case here's what I recommend:
Use the form's onSubmit event (<form onSubmit="YourJavasScriptMethod();">). Use JQuery and $.ajax() to read all of your form inputs and submit them to your own script which can save the values to the database. At the end of your onSubmit method be sure to return true so the browser will continue to submit the form.
Here's a pseudo-code example:
<form id="myForm" onSubmit="SaveToMyOwnDatabase();">
<input />... //your inputs
</form>
and your javascript:
function SaveToMyOwnDatabase()
{
// Create Json object by selecting each of the form inputs
// Call $.ajax and submit the Json to your script which processes the json
return true;
}
I would recommend using Contact Form 7 (WP plugin). There is a PHP hook built in that you can use to do this called wpcf7_before_send_mail. It will pass an object with all of the contact form data to your custom function(s) which you can use to write to the database or do other server side stuff. Here's a (really) short example (this goes in functions.php):
add_action( 'wpcf7_before_send_mail', 'save_contact_to_db' );
function save_contact_to_db( $cf7 ) {
$email = $cf7->posted_data['fields_email'];
// put database writing stuff here
}
There is a little more info to be had here. Also this link should be helpful since it's people discussing how to do exactly the same thing you are asking. There is also a plugin that will automatically write CF7 or Jetpack form submissions to the database for you but I've never used it so I can't comment on it. In general though I recommend using contact form 7 for this, especially since you are sending the form post data to a php script that is not on your server.
I think the solution is simple : submit your form to a PHP page that will : 1) save it in your database and 2) send it wherever you want. By far the simplest and most used solution in your case!
I have MySQL generating forms on one page with the same action and submit button. The number of forms vary. They all call the same PHP file when submitted. Also, I have one PHP file which collects the data upon submission. See the example below.
The problem is when one of the forms is submitted, values get confused with different fields from different forms. Example: When form1 is submitted, PHP receives the form6's values.
How can I make sure each form submits its own values?
HTML Code Example:
<form method="POST" action="index.php?action=newhistory" name="history_1">
<input type="hidden" name="id" value="1">
<input type="text" name="history">
<input type="submit" name="add_history" value="Submit">
</form>
<form method="POST" action="index.php?action=newhistory" name="history_6">
<input type="hidden" name="id" value="6">
<input type="text" name="history">
<input type="submit" name="add_history" value="Submit">
</form>
PHP Code Example:
case 'newhistory':
$id = $_POST['id'];
$history = $_POST['history'];
$sql = mysql_query("INSERT INTO history (id, history) VALUES('".$id."', '".$history."')", $link);
break;
Any solutions?
It will submit the content encapsulated by the <form></form> tags. Having several form's action attribute point to the same page should not create the problem you describe.
The code you wrote here looks fine. Check your HTML code, and ensure you have the corrent <form></form> tags surrounding the elements of each form.
Please use different names for the submit button, then check.
if(isset($_POST('add_history'))
{
}
if(isset($_POST('add_history1'))
{
}
If you hit submit in one form, the browser is supposed to send only the values from this form. When form1 is submitted, PHP will NOT receive the form6's values. Maybe you didn't close the form tag properly or have any JavaScript going wild.