PHP: submit form and simultaneously save data - php

i have a form with userdata, such as name, address, email etc. and i wanna do a action to a page on a foreign domain which i can not manipulate. Normally i would say that the form should do action="www.notmydomain.com/externalscript.php" to send the data via POST, but now i first wanna save this data an then submit the formdata to this target. This isn´t possible in this case so I tried to send my formdata to another script on MY server which saves the data an then reuses the POSTvars and send it with curl. This works so far but i need to redirect to www.notmydomain.com/theforeignscript.php so that the user ends at this domain. curl doesn´t do that because it only can do request, right?!
The first page shows the form:
<form id="newsletter" action="myscript.php" method="post">....</form>
..which is used by myscript.php to save the data in a csv and then again should submit the data to www.notmydomain.com/externalscript.php (which cannot be edited)...
Has anybody a hint how this could be done?
Thanks in advance.

You can use header("Location: http://domain.com/script.php"); to redirect the user to another address. This needs to be executed before anything is printed by your script, so make sure you catch all curl errors etc. properly to avoid ugly PHP error messages.

Related

PHP Script that sends an Email when users press on SUBMIT button in the form

I have a submission form, and I wrote a PHP script to make form send an email with input data that the user inserts it in the fields of the form
Now I put the PHP code in the same file of HTML and CSS, I mean that the HTML, CSS and PHP in the same file
When I put the file in the CMS, that what happened to the page, you could see what happen from here: https://www.hochikiamerica.com/acd-landing-2
PHP Script and HTML code of the form
https://github.com/Mstava/FreelancerProject/blob/master/formScript.php
Now, I need to know where to put the PHP code in the HTML file to avoid this
and How to ensure that code is working and it sends the Email
When you click on submit your form action takes you to action_page.php That's where you should read your post variables and send the email. Take a look at this post I wrote several years ago blue host email
A common misconception is that additional PHP can be run upon the user doing something (i.e. clicking submit button). Not the case. When the page has been rendered, no further PHP on that page can be executed.
So, what to do?
You have two options:
(1) You can create a second page (action_page.php or some name), that is specified on the action= attribute of the form tag. That additional page will receive the data the user typed in, via PHP variables $_POST (if method="post") or via $_GET if method=get, and you can then use that data to send the contact form, and either display new data to the user or send the user back to the original page. Of course, you may need additional PHP to acknowledge the form has been sent, etc - and this additional code will need to handle both the case where the user is visiting the page for the first time, and when the contact form has been sent and the user is seeing the page for the second time.
(2) You can use AJAX (javascript/jQuery) to grab the form data, send it to a secondary PHP file, which will receive the data via the $_POST/$_GET variables, send the email, and return a response back to the first page.
These days, mostly we use the second method, because it is much more powerful. For one, the user remains on the same page. For another, there is no page refresh. For another, your javascript can do other things after the form has been sent.
AJAX is actually pretty simple - just do a google search for YouTube videos on creating a login system with PHP and AJAX. You should be able to find one of around 10 mins or less that explains all you need to know to send your contact form, and send feedback back to the calling page.
Here is a 5-minute YouTube tutorial that will show you the basics:
Install a simple PHP and Ajax login system

how to send back acknowledge message to html by php

I'm a beginner in HTML and PHP. I made an HTML file include a form that sends variables with POST format to a PHP file for process and validation. In addition this form sends a picture file.
But really I confused that this PHP file how send back acknowledge for show to user.
For more description I want design an HTML form that send values to PHP and finally show user "thank you message" if the image size and format is valid or show "unable to upload your image" in first html form. Is it possible??
Regards...
HTML is static stuff. It sends nor validates anything. The HTML describes what a page should look like and if there are special elements, like your form. It is the blueprint that a browser uses to render a page.
The browser can communicate with a server. The browser can send a request to the server, and the server sends a response in return.
The request consists of a URL and optionally extra data, like POSTed form data.
The server normally always sends a reponse, unless something goes wrong (in terms of connection loss or a time-out). Even in case of a server error, you will often still get a response).
So, since you got a php script that processes the form data, it is its reponsibility to send a reponse that tells you, the user of the browser, whether posting has failed or succeeded.
The response is loaded and displayed by the browser as a new page, so after sending the form, the browser will render the response it got, which might contain an error message or a thank you message, and if you like, a new form to upload another file.
After that, there's also the possibility to post forms using AJAX. Then, you can get the response in the background (asynchonously) and add it to the existing page. But that's a whole new chapter, and I think you should try the normal way first.
After process your Post data use
use this code on that page :-
header('Location: http://www.example.com?msg="success"');
and on that side use that code
if(isset($_GET['msg'])){echo $_GET['msg']//or show what ever you want like you message in echo ;})

How do I identify where the POST data sent to a PHP script came from?

I have a ton of data collection forms on my website, and I wrote a PHP script to handle all the data. All the forms have that one script as their action, and POST as the method. The handler emails a copy of the data to me, and I'd like for the emails I get to contain the URL of the form where they originated. Is there any way in PHP to get the url of the form which was submitted to the script? Or do I have to add an extra hidden field in every form with its URL?
Send the following variable in the email as well:
$_SERVER['HTTP_REFERER']
If you want to ensure that posts only arrive from your own form, you could put a one-time token on the form in a hidden field to validate.

jquery grabbing post details and returning them to the form

I've been trying to use jQuery to grab the information from $_POST and return the user back to the actual form if their email address already exists in the system, however I can't seem to get jQuery to grab the php variable $post (which has all the information from $_POST) and spit them back to the original form. Could someone help me out please? :)
**************** EDIT ****************
So here's the basic structure.
Form: Name, Email, Address.
Submitting the form calls a php function to check if user exists in the database. If the email address is in the database, I want it to say "sorry this email already exists, click here to try again".
After clicking on the link to try again, I want the form to re-display with the fields they just typed in to display again. <- this is what I thought jQuery could do to re-post the information back to the form page..?
If the user doesn't exist, the user is saved into the database.
Does that make more sense?
From the sound of your question what you're trying to do doesn't make sense. This is because PHP is a server side language while Javascript is a client side (in the browser). This means that Javascript, and therefore jQuery, don't have access to PHP's variables ($_POST included).
There are two common ways to solve this:
Have PHP generate the form. You would output the values from $_POST, or another data location, into the form (ex., echo the variable into the input tag's value attribute). This is by far the easiest method. For example: printf('<input type="text" name="foo" value="%s"/>', $someCleanVariable);
Have the PHP generate JSON or XML, and use AJAX to get the data from the PHP script. You would have to parse out the values into the form. This is basically the same as the previous example, except it's more portable: you can have any source consume the data instead of just your form.
However, make sure that you protect your users when doing this: you need to clean the data that you're sending back to the user to make sure that there is no HTML, Javascript, or other malicious code in it. You don't want people being able to alter the look of your page by passing it data. See Cross-site Scripting Attacks.
Cheers.

Ctrl+r in firefox for refreshing page and my php code

i have create a form (so it's PHP and HTML hybrid-code). it has ability to send '$_POST'. And when i click it, it work perfectly on sending and displaying input.
But there's something happening when i click Ctrl+R in firefox for represhing the page. I got this confim dialog : "To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier"
my question
what is it, (this confirm dialog ?)
what i have to do on my code so it able to suppress this dialog ?
You probably have created an HTML page that contains a <form>. The form is used to send data to the HTTP server (that is, the webserver that hosts your site).
The HTTP protocol defines different request types used to send data to the server and to retrieve data from the server. The most used are GET and POST. You must learn about all this if you want to be anything more than a very bad PHP programmer, which is unfortunately (or fortunately, if you are on the hacker side) very common.
Your problem is that Firefox has arrived on the page you are talking about after sending a POST request. If you reload the page, it has to send the same data again in the form of a POST. Due to the conventions on what a POST request should be used for (usually to modify data on a database), the browser asks the user if he is sure about what he wants to do.
There are mainly two options to circumvent this:
Change the form method to GET; or
Use a redirection after the POST.
To use the first method, you could simply add a method="get" parameter to your form tag:
<form action="senddata.php" method="get"> ... </form>
To use the second method, you simply redirect the user after the POST request, using something like
header("Location: blahblahblah")
The most used pattern is the POST-Redirect, that is, the second method I told you about. There are many security implications on using GET to change data on a database (if you are interested on that, and you should be, as every PHP programmer should, read about XSRF).
Submitting a form (sending a POST request) is commonly used to confirm an order on eCommerce sites. Therefore, submitting it twice would submit the order, twice. Therefore browsers, tend to ask for confirmation that a user wants to send the POST request again.
In order to prevent this, you need to make the refresh do a GET request instead of a POST request. To do this, simply redirect to the same page after processing the form.
header("Location: /path/to/self");
This will make it so when the user hits refresh, it will be sending a GET request instead of a POST request, and it won't prompt for confirmation.
To clairify, it goes like this:
Form gets sent via POST (User clicks on form)
Form gets processed
User gets redirected to the same page (via GET)
User now will be refreshing a GET request instead of a POST request.
I guess whenever your form (php, asps, static html etc) contains post information that may either form field infor or other, is sent to the server via firefox, it displays such a message before sending the data again to server. it serves as a security protection from Mozilla developers. I guess it can be disabled via about:config but it is not recommended to so.
Also it is a normal behaviour. It should be like this and have been like this for a fairly long time in firefox.
You may like to have a look here:
http://forums.mozillazine.org/viewtopic.php?f=38&t=682835&st=0&sk=t&sd=a&hilit=Firefox+must+send
alternatively use GET instead of POST to send your data...
Regards
If the form was submitted successfully, answer with the status code 303:
header('Location: http://www.example.com/', TRUE, 303);
This forces the browser to use a GET request for the resulting page. A reload won’t send any POST data, and no pop up is shown.

Categories