Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
To preface, I am sorry for asking what is likely an extremely simple question. I am an intern for a non-profit helping with some web tasks and have been asked to build a submission form. I want the data inputted in the fields to send to my email address. The form code is fine, however it is the PHP script that seems to not be working. Can anyone suggest a simple PHP code (or another method) to parse this info a designated email address? Currently using method="post"
Here is the form code: http://pastebin.com/7Gxb92n5
Thank you!
Alex
Your form uses the method POST, so in send_form_application.php you will have to use the mail() function.
Name [Last, First MI] <input type="text" name="name" /><br />
Primary Organization <input type="text" name="primary_organization" /><br />
This above is your HTML that will POST variables contained in a $_POST array.
So $_POST['name'] will contain you name and
$_POST['primary_organization'] will contain the primary organization (what the user made as an input in the form)
The "most important part" of this HTML input is the name because it is what you will use to "fill" the mail function with it's attributes.
Your PHP will look something like:
mail($to, $subject, $message, $headers);
?>
The $content could be all the elements of the post like this (should actually be above the previous code)
$content = $_POST['name'] . "\n";
$content .= $_POST['primary_organization'] . "\n";
?>
And so on...
Please tell me if you cant understand something!
IMPORTANT: while this might work, this is not really secure yet, you should ALWASY validate the user input!
On the .php page handling the post ("send_form_application.php" in your case),handle all incoming form fields in the following way:
1) Create variables to store each of your incoming form fields...
...
$name = $_POST["name"]
$primary_org = $_POST["primary_organization"]
$primary_pos = $_POST["primary_position"]
..
ETC...
2) Create an email body string variable to hold all these values however you want to...
3) Then use php's mail functionality to email the info to yourself...
More info here-> http://php.net/manual/en/function.mail.php
Hope that helps!
To verify your server/mail configuration is okay, try the simplest possible code:
<?php
mail("info#nonprofit.org", "form info", print_r($_POST, TRUE));
And you could assemble the body much easier like this (note that clean_string makes no sense for the email body):
$email_message = <<<END
Name [Last, First MI] : $_POST["name..."]
Primary Organization : $_POST["org..."]
Street Address : $_POST["street..."]
City : $_POST["city..."]
State : $_POST["state..."]
END;
Well here is a script that should give you the basic Idea on how to send an email via PHP
http://email.about.com/od/emailprogrammingtips/qt/How_to_Send_Email_from_a_PHP_Script.htm
But other than that your html looks fine...
Related
I'll be thrilled with any solution that will do that. Here's what I've been trying:
I have an input form that gets variable info. I want to email using php mail(), the variable info that was input. I've put the php mail() later in the same html/php file (I haven't used js in here at all). I can't figure out how to access those input variable values, to put into the mail() function.
<form target="_blank" action="https://www.paypal.com/cgi-bin/webscr" method="post"> <br>
Donation Amount $: <input type="text" name="amount" size="20"> <br>
Instructions to us:
<textarea name="eventcardinstructions" rows=6 cols=60></textarea><br>
<input type="hidden" name="add" value="1">
<input type="hidden" name="cmd" value="_cart">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_cart_SM.gif" border="0" name="submit">
etc.
</form> <!-- closes the form and lets the user hit a submit button-->
This is collecting inputs from the user to send to paypal. It's working. The user inputs an amount into an input field and instructions into a textarea. The amount is sent to paypal. The textarea instructions ARE NOT SENT to paypal, which is what I want.
Later (before the form ends OR) after end of form and the submit button that sends this to paypal, I want to reference the "amount" and "instructions" in php mail() function. The mail() function is working and sends emails. It just ignores and doesn't send the variable amounts. So the emails say:
Subjectline: Event Card
Body contents: Event card bought for
The mail function is:
<?php
// headers allow the email to recognize html with in the
// message & process it.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$eventcardmsg = "Event card bought for " . $_POST['amount'];
$eventcardmsg = $eventcardmsg .= $_POST['eventcardinstructions'];
mail("email#ourdomain.net", "Event Card", $eventcardmsg, $headers);
?>
How do I setup amount and eventcardinstructions so the values are used?
I see a couple related q&as but can't figure it out from them. This answer is using it the same way I am by using the $_POST['variable']; but it's not working for me.
https://stackoverflow.com/questions/30016669/how-do-you-save-a-inputs-value-in-a-variable
This one:
Assign input form to a variable looks like it's using php in the middle of the html input form to set the variable. I could try that, but seems odd to need to, like I'm not understanding something.
Thanks!!
First, as TomasH already pointed, your $eventcardmsg assignation is wrong.
Anyway it might be simplified like this:
$eventcardmsg =
'Event card bought for ' . $_POST['amount'] . ' ' . $_POST['eventcardinstructions'];
(don't omit at least one space between amount and instructions)
Now if you keep getting an wrong email content, the main question in this issue is: are POST query parameters sent to your PHP script?
So you should test it with something simple like:
<?php
echo '<pre>' . print_r($_POST, true) . '</pre>';
Then if it shows empty parameters (or even those parameters not cited), you must examine the way you're sending data from the HTML page.
Again already pointed by TomasH, it might be somme inconsistency while you previously send data to Paypal: if you don't identify it, please edit your question to show us the whole Paypal process.
EDIT (trying to answer the multiple OP's comments)
Regarding string concatenation in general
You may concatenate as many strings as you want into a resulting string, using the . operator, so:
$a = 'A';
$b = 'BB';
$c = 'CCC';
echo $a . $b . $c; // prints ABBCCC
Regarding .=
Any operator (not only .) may be used in conjunction with =, as a shortcut to avoid repeating the 1st operand when it is also the result, so:
$result .= $ope2;
// the above expression is pretty equivalent to the one below
$result = $result . $op2;
Regarding comments related to your original issue
It's not clear what you exactly want now, because you said:
I don't really want to send the form to a php file of my own.
But your original question tells you can't emit a proper email, and to emit an email you must first send the form to the php file where you emit it!
Anyway, you should test like I previously suggested:
echo '<pre>' . print_r($_POST, true) . '</pre>';
This way, you will get the whole list of which POST query parameters are passed to your script: print_r() is the function that prints this list, and <pre> is the enclosing HTML tag that makes this print more readable.
Be careful to type the statement exactly: from your penultimate comment, I guess you typed something wrong.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to use a MySQL database to verify that a user has bought a program from my site. I don't need to use a database, but this is the easiest way that I though this could be accomplished (any suggestions on different ways to accomplish a purchase-verification system are greatly appreciated).
My current algorithm:
User pays for the program (through one of various payment gateways provided by WooCommerce)
User is given a unique, randomized hex string by the server (this string is also stored in the database, as a key/value pair; string: 0; the 0 signifies unused)
User enters the string into the program (required), which sends an HTTP request (which includes the string) to the server. This request tells the server to look for the given string in the database. If it's found, change it's value to 1 (used). If the value is already 1 OR the string cannot be found, send an error report back to the user (program will not work).
If everything works correctly, the user can now use the program.
I have to use the HTTP request because only the server can edit the database. My website uses shared hosting, so I cannot directly modify the database on any local user.
I've never worked with databases before, but I know I have to use the INSERT, CREATE TABLE, UPDATE, and SELECT commands. In addition, I've never directly worked with HTTP requests.
How should the HTTP request look like, and how do I handle them on the server? Any other tips on how to work the database would also be appreciated.
Note: I'm probably going to use PHP (server-side) and Python (client-side).
Your html would look like this
<html>
<body>
<form action="validate.php" method="post">
Name: <input type="text" name="name"><br>
unique string: <input type="text" name="uniqueString"><br>
<input type="submit">
</form>
</body>
</html>
And on the server side, your validate.php would look like this.
<?php
$name = $_POST["name"];
$unqString = $_POST["uniqueString"];
//connect to your DB
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$data = mysql_query("SELECT uniqueStringUsedFlag FROM <stringTableName> WHERE uniqueString='.$unqString.'")
or die(mysql_error());
while($info = mysql_fetch_array( $data )) {
if $info['uniqueStringUsedFlag']==1{
return 'Error Message';
}else
{
mysql_query("update <stringTableName> set uniqueStringUsedFlag =1 where uniqueString='.$unqString.'");
return 'Success';
}
}
//if the control comes here, it means the record was not found.
return 'Error message'
?>
Replace < stringTableName > with your actual table name.
And do the same with the columns too.
PS: This code is untested. And please do proper validations, etc. Haven't included that here.
Let me know if this works.
I have a question and try searching on Google and Stakoverflow and could't find any perfect solution. I am using phpmailer and want to let the user to customize it's body message. So I have given and option to edit the email body text by providing a texarea input box in back end where user will input the email text with some my own provided php variable.
For example body text is "Dear {$username}, thank you for signup.". Once I store this input into database and want to retrieve this in php:
$username = "ABC"; //username variable
$message = $row["message"]; //stored email body message
echo $message;
OUTPUT DATA: "Dear {$username}, thank you for signup."
I want to get it like this "Dear ABC, thank you for signup.". How is it possible or is there any other option I can use it with php varialbe or defined type?
You're not getting an answer you're expecting because contents of $row["message"] don't get evaluated again. And that's actually a good thing because users could use any variable name they want, which would create a security threat.
There is a quality solution, though. You should check out template engines for PHP, e.g. Smarty and Twig. They allow you to create templates with placeholders, for example something like:
Dear {{username}}, thank you for sign-up!
which you could store in a file or let your users define their own templates. Template engine will then read the template and, using the values you send it, replace those placeholders with actual values.
To see how this is done in practice, choose a template engine and check its documentation.
I have found a solution in PHP with str_replace function. This function is really very helpful. I have tried with modified input message "Dear user_name, thank you for signup.":
$username = "ABC"; //username variable
$message = $row["message"]; //stored email body message
$message_new = str_replace("user_name", $username, $message);
echo $message_new;
OUTPUT DATA: "Dear ABC, thank you for signup."
You can use PHP eval() function.
$msg = "$cust_dt->cus_name, Your order placed successfully. ID: $sms_oid Total: $sms_ord_tot";
echo $msg;//output1
eval("\$msg= \"$msg\";");
echo $msg;//output2
Output 1: $cust_dt->cus_name, Your order placed successfully. ID:
$sms_oid Total: $sms_ord_tot
Output 2: John Doe, Your order placed successfully. ID: 454554545
Total: 1600
You can store a strings like $msg in a database and use eval() function to evaluate string into php code.
For More:
https://www.w3schools.com/php/func_misc_eval.asp
https://www.php.net/manual/en/function.eval.php
Just wanted to say THANK YOU!!!!
This also works by putting ((name)), and using that inside str_replace - instead of just using "user_name"
Which is a little more user-friendly - and keeps from accidentally replacing things if someone were to type "user_name", for instance lol
Plus it'll make it look cool when having your "insert" legend at the top saying:
Choose one of the following:
((name)) = Clients Name
You saved me so much time!
Let's say I have sugarcrm cases where I know what the case id and or number is.
I want to use email to sms to send to the techs what their work is. So, the link that needs to be sent must have the id and number in it. I want to send them to a simple PHP page that just has a dropdown menu with the two options accept and complete.
so if they receive the sms, they click on the link, it takes them to the page where they click on dropdown to accept the case.
Updating from the PHP is easy: it's just an update my sql query. I need to know how to send header or info in the link that the tech receives.
something like http://tech.com/caseupdate.php?case_number?case_id
so I can use that case number/id when updating
You need something like this:
Each variable in the query string should be separated by an & as +War10ck said
i.e:
http://tech.com/caseupdate.php?case_number=[num]&case_id=[id]
Then in your PHP you can get those variables using $_GET[].
<?php
if(isset($_GET['case_number']) && isset($_GET['case_id'])) {
$casen = (int) $_GET['case_number']; // assuming you need an Int value
$caseid = (int) $_GET['case_id'];
/* do your MySQL thing here now with $casen and $caseid */
}
?>
Dont know if this is right but got it working by doing the following
<input type="hidden" name="casen" value=<?php echo $casen;?>>
Thank you all for your help.
This si an excellent site for newbis to learn and get help in a direction to solve issues.
I´m very newbie on PHP, and programming in general, and I´m still reading my manual. I´m trying some excercises, and creating some simple tests of my own.
I have a form, with some checkboxes and select items. The user complete the form and it shows some output. It´s like a little game, where the user selects some answers, and the scripts shows a number that´s his/her score.
Now, I would like to receive an email each time a user completes the game, with all the answers.
The users completes some checkboxes and select items, so, all answers are arrays (So, I´ve tried $message_answer2 = $_POST['answer2']; and it won´t work).
So, I´ve tried:
$to = 'myown#mail.com';
$subject = 'New Game '.date("m/d/Y h:m:s");
$message_answer2 = join(', ', $_POST['answer2']);
mail($to, $subject, $message_answer2);
There are 2 issues with this:
There´s a PHP error that appears: Warning: join() [function.join]: Invalid arguments passed in ...
An email is dispatched (a blank email) each time I just reload the page. The emails should be sent only after the user hits the "send" button of the form.
I would really appreciate if someone could help me out with this :)
Thanks!!
Rosamunda
You should try this:
if (isset($POST['answer2'])) {
$to = 'myown#mail.com';
$subject = 'New Game '.date("m/d/Y h:m:s");
$message_answer2 = join(', ', $_POST['answer2']);
mail($to, $subject, $message_answer2);
}
I'm assuming when the user submits the form you're having it post to the current page. If that's the case then when you're originally loading the page there is no data set in $_POST['answer2'] which is why you're getting an error, and then its sending the blank message because there is no data to send. Adding that an email will be sent only if statement will make it so that only if there is data being posted to the page in 'answer2'.
join() is for use on arrays. Unless you've used the PHP-specific array notation on your form's field names <input name="answer2[]">, PHP will NOT make $_POST['answer2'] an array - it'll just be an ordinary string.
If your form looks like
<input name="answer2" ...>
<input name="answer2" ...>
then you're still only get a single string value in $_POST. You must use the [] notation to tell PHP it should create an array and add each individual answer2 to that array. When not in array mode, PHP will simply overwrite $_POST['answer2'] with each answer2 value it finds in the form data.