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!
Related
I want to validate my form input fields on button click in jQuery. But I don't want to use client side validation. How can I add PHP server-side validation on button click?
I am using modal box for form.
I know it is a beginner's level question, but currently I am unable to figure it out because of my low expertise.
Yes you can validate using PHP and it's not a big deal. I'll provide a simple example here to pass the form data to PHP for validation.
<?php
if( isset($_POST['submitForm']) )
{
$userName = $_POST['userName'];
$userAge = $_POST['userAge'];
if ($userAge > 21)
{
echo "Hey " + userName;
}
}
?>
<html>
<body>
<form method="POST" action="">
Name: <input type="text" name="userName">
Age: <input type="number" name="userAge">
<input type="submit" name="submitForm" value="Validate">
</form>
</body>
</html>
If you wish to pass the data to another page to validate, just give the file name in action attribute, like action="Your_PHP_File.php" and validate separately. Just a tip: If you wish to redirect on success (heavily used):
header("Location: Your_URL_Here");
Anyways, this is not a better user experience. You must use client side validation using JavaScript/any JavaScript framework, and use language like PHP only to communicate with the server, such as storing the data in the database, retrieving data from the database.
I have written some code to create a dymanic HTML form element for my application,I want on submit that form will post data and php will write to database.
here is the code
$("#add_id").click(function(name){
name=prompt("Task name");
if (name) {
$('.dashboard').append('<div class="each_task" id="task'+ count+'"><label>'+name+
'</label><form class="task_form" action="process_task.php" method="post"><div class="ctrl_btn" id="start'+
count+'"><input type="hidden" name="task_name" value="'+name+'" /><input name="task_start" type="hidden" id="start_time'+
count+'" value="" /><input class="task_start" type="submit" id="'+count+'" value="Start" /></form></div><form class="task_form" action="process_task.php" method="post"><div class="ctrl_btn" id="end'+count
+'"><input type="hidden" name="task_name" value="'+name+'" /><input name="task_end" type="hidden" id="end_time'+count+'" value="" /><input class="task_end" type="submit" id="'+count+'" value="End" /></form></div></div>').css('color', 'red');
Now when I submit the form it sends name and start_time or end_time value to php script which will insert data to database. the problem is dynamically created HTML element doesn't stick around.
Can anyone suggest how could I get around this problem and can have my dynamically created html stick to the page and also on submit post data to database.
Thanks
I'm happy to improve my answer if I didn't get your question accurately, but I'm assuming you want to stop the page from getting refreshed when you press the form submit button.
Here's a solution to prevent the page refresh using Javascript.
It is important to note that if you use this solution, you have to handle the form submission using Javascript and AJAX. An example of how to do this.
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 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.
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.