I have a form that is using php to email myself with enquiries. I have some jquery that is filling in details into a div,
How can I pass the Jquery var to PHP via ajax? (I’ve read that that is the best way?)
Here’s how’s it’s emailing me with php:
<? if(isset($_POST['submit'])) {
$to = "rob@domain.com";
$header = 'From: rob@domain.com';
$subject = "Quotation";
$enquiry_first_name = $_POST['enquiryfirstname'];
$enquiry_last_name = $_POST['enquirylastname'];
$enquiry_title = $_POST['enquirytitle'];
$enquiry_organisation = $_POST['enquiryorganisation'];
$enquiry_address = $_POST['enquiryaddress'];
$enquiry_country = $_POST['enquirycountry'];
$enquiry_email_address = $_POST['enquiryemailaddress'];
$enquiry_telephone = $_POST['enquirytelephone'];
$enquiry_additional_comments = $_POST['enquiryadditionalcomments'];
$body = "You have an quote request from the website:
Name: $enquiry_title $enquiry_first_name $enquiry_last_name
Type of organisation: $enquiry_organisation
Address: $enquiry_address, $enquiry_country
E-Mail: $enquiry_email_address
Tel: $enquiry_telephone
Comments: $enquiry_additional_comments
Kind regards";
mail($to, $subject, $body, $header);
echo "Thank you for your enquiry.";
} ?>
Here’s the jquery that is outputting data into a div:
function makeSummary() {
var summary = [];
$steps.not(":last").each(function (i, step) {
$step = $(step);
summary.push('<p><b>' + $step.data('name') + '</b></p>');
var $ch = $step.find('input[type="checkbox"]:checked');
if (!$ch.length) {
summary.push('<p>No items selected</p>');
} else {
$ch.each(function (i, ch) {
summary.push('<p>' + $(ch).val() + '</p>');
});
}
});
return summary.join('');
}
1) Make a hidden input field.
2) Pass the jQuery var content to the hidden input field
3) Get it in php with $_POST[‘hiddenname’]
E: Here is an example: http://jsfiddle.net/yLuNu/4/
It’s using a dropdown to store a value in a hidden field. You can use any output and store it inside of the hidden field.
E2: Since I didn’t really get what you want to pass to the hiddenfield: If you have a function and only want the output to save inside the hiddenfield:
What exactly do you want to pass to your script? I saw a checkbox so I thought you wanna use the change func. In case you only want to return the output of a function
while var is the output of your function. Just add this at the end of your existing func..