I have a form that post an array and I loop over the array which I want to display in an email.
$qty = $_POST['qty'];
foreach($qty as $value)
{
$qtyOut = $value . "<br>";
}
I then want to get the $qtyOut of each one and display in my email.
my email looks like so.
$message = "An order was submitted from: " . $additionalEmail . "<br/><br/>"
. "Customer Name: " . $custName . "<br/>"
. "Email:" . $customerEmail . "<br/>"
. "Message: <br/>"
. "<pre>" .$custName . "<br> "
. "Qty: ". $qtyOut . "Desc: " . $desc . "Options :" . $options . "Price : " . $price . "</pre>"
. "Submitted at: ". $time;
$message = wordwrap($message, 70, "\r\n");
I would like to have the:
. "Qty: ". $qtyOut . "Desc: " . $desc . "Options :" . $options . "Price : " . $price .
Display as many times as there are order lines to fill the email.
Can somebody point me in the right direction please, as only the last line is displayed that was entered.
You are redefining your $qtyOut variable.
Instead of
$qtyOut = $value . "<br>";
Use:
$qtyOut .= $value . "<br>";
Related
i have an html form , when user submits the data, the data goes into database, this was working fine until i added one more thing, i added mail function to send the mail after the data is submitted. my code is like below:
<?php
error_reporting(0);
session_start();
require('db_config.php');
if (isset($_POST['submit'])) {
$name = $_FILES['Photo']['name'];
list($txt, $ext) = explode(".", $name);
$image_name = time() . "." . $ext;
$tmp = $_FILES['Photo']['tmp_name'];
$shame = $_FILES['paymentphoto']['name'];
list($txts, $exts) = explode(".", $shame);
$receipt_name = time() . "." . $ext;
$tmps = $_FILES['paymentphoto']['tmp_name'];
if (move_uploaded_file($tmp, 'uploads/' . $image_name) && move_uploaded_file($tmps, 'receipt/' . $receipt_name)) {
$sql = "INSERT INTO members (firstname, lastname, image, company, designation, addressone, addresstwo, aadhar, city, state, pin, pan, rnameone, rnametwo, mobile, alternate, email, experience, businessdate, companyregistration, gstin, servicesoffered, fee, mode, receipt) VALUES ('" . $_POST['first_name'] . "','" . $_POST['last_name'] . "' , '" . $image_name . "','" . $_POST['company'] . "', '" . $_POST['designation'] . "','" . $_POST['address'] . "', '" . $_POST['address2'] . "', '" . $_POST['aadhaar'] . "', '" . $_POST['city'] . "', '" . $_POST['state'] . "', '" . $_POST['pin'] . "', '" . $_POST['pan'] . "', '" . $_POST['recommended'] . "', '" . $_POST['recommended2'] . "','" . $_POST['mobile'] . "', '" . $_POST['alternate'] . "', '" . $_POST['email'] . "', '" . $_POST['experience'] . "', '" . $_POST['date'] . "', '" . $_POST['registration'] . "', '" . $_POST['gst'] . "', '" . $_POST['services'] . "', '" . $_POST['fee'] . "', '" . $_POST['payment'] . "', '" . $receipt_name . "' )";
$mysqli->query($sql);
$to = "teiamembers#gmail.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$headers = "From:" . $from . "\nMIME-Version: 1.0\nContent-Type: text/html; charset=utf-8\n";
$headers2 = "From:" . $to;
$subject = "TEIA Membership Registration Request";
$subject2 = "TEIA Membership Request";
$message = $first_name . " has requested for TEIA Registration. Full Name:" . " " . $first_name . " " . $last_name . "<br>" . "Email:" . $from . "<br>" . "Mobile:" . " " . $_POST['mobile'] . "<br>" . "Company Name:" . " " . $_POST['company'] . "<br>" . "Designation" . " " . $_POST['designation'] . "<br>" . "Residence Address:" . " " . $_POST['address'] . "<br>" . "Office Address:" . " " . $_POST['address2'] . "<br>" . "Aadhaar:" . " " . $_POST['aadhaar'] . "<br>" . "City:" . " " . $_POST['city'] . "<br>" . "State:" . " " . $_POST['state'] . "<br>" . "Pin:" . " " . $_POST['pin'] . "<br>" . "Pan:" . " " . $_POST['pan'] . "<br>" . "Reference:" . " " . $_POST['recommended'] . "<br>" . "Alternate Number:" . " " . $_POST['alternate'] . "<br>" . "Experience:" . " " . $_POST['experience'] . "<br>" . "Aadhaar:" . " " . $_POST['aadhaar'] . "<br>" . "Date of Business Setup:" . " " . $_POST['date'] . "<br>" . "Company Registration Number:" . " " . $_POST['registration'] . "<br>" . "GSTIN:" . " " . $_POST['gst'] . "<br>" . "Services Offered:" . " " . $_POST['services'] . "<br>" . "Fee Paid:" . " " . $_POST['fee'] . "<br>" . "Payment Mode:" . " " . $_POST['payment'] . "<br>";
$message2 = "Your request for TEIA Membership Received. We will contact you Shortly. ";
mail($to, $subject, $message, $headers);
mail($from, $subject2, $message2, $headers2); // sends a copy of the message to the sender
if ($mysqli) {
$msg = "Your Request For Membership Registration Sent Successfully";
}
}
}
?>
now the problem is when the user submits the form, the mail is working properly, but the values are not going to database, can anyone please tell me what could be wrong here, thanks in advance
TAKE CARE OF SQL INJECTION first.
Some amount of checking is necessary all the time. At the very least...this one.
if($mysqli->query($sql)) { fine } else { error }
And in the "error" part of above, if you would have included error number and actual message, you yourself might have found the answer.
Main doubt I have is...you have some unique column and the insert did not happen as it did appear in database earlier.
Also, better to use an id int unique auto_increment for several purposes.
Secondly, you should have created $mailstatus and have checked the same and not mysqli for this message.
if ($mailstatus) {
$msg = "Your Request For Membership Registration Sent Successfully";
}
I am trying to make a button on a page that prints out data from the database and then you can press 2 different buttons, one that deletes them from the database and the other one inserts it into another table in the database and deletes the data from the database, but it keeps inserting it twice into the new table and I have no clue why, this here prints out the data and session variables + buttons:
if(!isset($_POST['orderby'])) {
foreach ($requests as $row) {
echo "<div class='requests'>" . "<li class='refunds'>" . "Palauttajan nimi: ".
$row['customer_name'] . "</br>" ."Palautettavat tuotteet: ".$row['product_name']."<br> "."Määrä: ".
$row['product_qty'] . " "
. "<br>Kommentti: " . $row['comment'] . "<br> " . "Hinta: " . $row['refund_total'] . "€ " .
"<br>" . "Päivämäärä: " . $row['request_date'] . " " .
"<a class='right' href='admin-page?deleteid=" . $row['request_id'] . "'>Hylkää</a></li>" .
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']
. "'>Hyväksy</a></li>" . "</div>";
$_SESSION['custname'] = $row['customer_name'];
$_SESSION['prodname'] = $row['product_name'];
}
} else {
foreach ($pergele as $row) {
echo "<div class='requests'>" . "<li class='refunds2'>" . "Palauttajan nimi: ".
$row['customer_name'] . "</br>" ."Palautettavat tuotteet: ".$row['product_name']."<br> "."Määrä: ".
$row['product_qty'] . " "
. "<br>Kommentti: " . $row['comment'] . "<br> " . "Hinta: " . $row['refund_total'] . "€ " .
"<br>" . "Päivämäärä: " . $row['request_date'] . " " .
"<a class='right' href='admin-page?deleteid=" . $row['request_id'] . "'>Hylkää</a></li>" .
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']
. "'>Hyväksy</a></li>" . "</div>";
$_SESSION['custname'] = $row['customer_name'];
$_SESSION['prodname'] = $row['product_name'];
}
}
and this should insert it into the database once and delete the data from the old table:
if(isset($_GET['acceptid'])) {
$accept = $_GET['acceptid'];
$custname = $_SESSION['custname'];
$prodname = $_SESSION['prodname'];
/* Query to do whatever here */
$wpdb->insert("wp_acceptedrequests", [
"customer_name" => "$custname",
"name_product" => "$prodname",
"date" => date("Y/m/d/G:i:sa") ,
]);
$wpdb->query("DELETE FROM wp_refundrequests WHERE request_id = $accept");
}
What makes them insert twice and how do I prevent it from doing that?
I just ran into a similar situation where $wpdb inserts where being duplicated.
In my case it was happening if I was authenticated and browser inspector was open.
i am using this code for my PHP form where i have added too much fields
i want to receive this form in HTML format while its comming in general message
also in mail should attached file.. which is also not coming
this is php back code is this
<?php
/** CONFIGURATION OPTIONS
* $email_address = The email address that all form submissions will be sent to
* $company_address = The destination address that will be used in the contact form */
$email_address = 'info#synergywebdesigners.com';
$company_address = 'Delhi, India';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
/** DO NOT EDIT ANYTHING BELOW THIS LINE UNLESS YOU ARE COMFORTABLE WITH PHP */
echo "<script>var companyAddress = '".$company_address."';</script>";
$field_rules = array(
'name' => 'required',
'email' => 'required|valid_email',
'phone' => '',
'contact_reason' => 'required',
'message' => 'required'
);
$error_messages = array(
'required' => 'This field is required',
'valid_email' => 'Please enter a valid email address'
);
$error_list = '';
$fields = $_POST;
if (!empty($fields))
{
//Validate each of the fields
foreach ($field_rules as $field => $rules)
{
$rules = explode('|', $rules);
foreach ($rules as $rule)
{
$result = null;
if (isset($fields[$field]))
{
if (!empty($rule))
$result = $rule($fields[$field]);
if ($result === false)
{
$field_name = ucwords(implode(' ', explode('_', $field)));
$error_list .= "<div class='error'>$field_name: " . $error_messages[$rule] . "</div>";
}
}
}
}
if (empty($error_list))
{
$subject = '[Application for Employment] New message from ' . $fields['name'] . '.';
$content = $fields['name'] . " sent you a message from your contact form:\r\n\n";
$content .= "-------\n" . $fields['message'] . "\r\n\n";
$content .= "Position Applied For: " . $fields['positionapplied']
. " \n\nTelephone Number: " . $fields['telephonenumber']
. " \n\nMobile Number: " . $fields['mobilenumber']
. " \n\nEmail: " . $fields['email']
. " \n\nDate of Birth: " . $fields['bday']
. " \n\nGender: " . $fields['gender']
. " \n\nPermanent Address: " . $fields['permanentaddress']
. " \n\nCity: " . $fields['city']
. " \n\nState: " . $fields['state']
. " \n\nPin: " . $fields['pin']
. " \n\nMarital Status: " . $fields['marital_status']
. " \n\nFather Name: " . $fields['fathername']
. " \n\nFather Occupation: " . $fields['fatheroccupation']
. " \n\nMother Name: " . $fields['mothername']
. " \n\nMother Age: " . $fields['motherage']
. " \n\nMother Occupation: " . $fields['motheroccupation']
. " \n\nSpouse Name: " . $fields['spousename']
. " \n\nSpouse Age: " . $fields['spouseage']
. " \n\nSpouse Occupation: " . $fields['spouseoccupation']
. " \n\nBrother Name: " . $fields['brothername']
. " \n\nBrother Age: " . $fields['brotherage']
. " \n\nBrother Occupation: " . $fields['brotheroccupation']
. " \n\nSister Name: " . $fields['sistername']
. " \n\nSister Age: " . $fields['sisterage']
. " \n\nSister Occupation: " . $fields['sisteroccupation']
. " \n\nEducation Qualification Course1: " . $fields['educationqualificationcourse1']
. " \n\nEducation Qualification University1: " . $fields['educationqualificationuniversity1']
. " \n\nName of the College or School: " . $fields['nameofschool1']
. " \n\nEducation Qualification Year From 1: " . $fields['educationqualificationyearfrom1']
. " \n\nEducation Qualification Year to 1: " . $fields['educationqualificationyearto1']
. " \n\nEducation Qualification Percentage 1: " . $fields['educationqualificationclasspercentage1']
. " \n\nEducation Qualification Specialisation1: " . $fields['educationqualificationspecialisation1']
. " \n\nEducation Qualification Course2: " . $fields['educationqualificationcourse2']
. " \n\nEducation Qualification University2: " . $fields['educationqualificationuniversity2']
. " \n\nName of School2: " . $fields['nameofschool2']
. " \n\nEducation Qualification Year from 2: " . $fields['educationqualificationyearfrom2']
. " \n\nEducation Qualification Year to 2: " . $fields['educationqualificationyearto2']
. " \n\nEducation Qualification Class Percentage 2: " . $fields['educationqualificationclasspercentage2']
. " \n\nEducation Qualification Course3: " . $fields['educationqualificationspecialisation2']
. " \n\nEducation Qualification Specialisation 2: " . $fields['educationqualificationspecialisation2']
. " \n\nEducation Qualification Course 3: " . $fields['educationqualificationcourse3']
. " \n\nEducation Qualification University 3: " . $fields['educationqualificationuniversity3']
. " \n\nName of School 3: " . $fields['nameofschool3']
. " \n\nEducation Qualification Year from 3: " . $fields['educationqualificationyearfrom3']
. " \n\nEducation Qualification Year To 3: " . $fields['educationqualificationyearto3']
. " \n\nEducation Qualification class Percentage 3: " . $fields['educationqualificationclasspercentage3']
. " \n\nEducation Qualification Epecialisation 3: " . $fields['educationqualificationspecialisation3']
. " \n\nComputer Proficiency Package Language: " . $fields['computerproficiencypackagelanguage']
. " \n\nComputer Proficiency Excellent: " . $fields['computerproficiencyexcellent']
. " \n\nComputer Proficiency Good: " . $fields['computerproficiencygood']
. " \n\nComputer Proficiency Average: " . $fields['computerproficiencyaverage']
. " \n\nProfessional Courses Programe 1: " . $fields['professionalcoursesprograme1']
. " \n\nProfessional Courses Duration 1: " . $fields['professionalcoursesduration1']
. " \n\nProfessional Courses year 1: " . $fields['professionalcoursesyear1']
. " \n\nProfessional Courses Organised by 1: " . $fields['professionalcoursesorganisedby1']
. " \n\nProfessional Courses Programe 2: " . $fields['professionalcoursesprograme2']
. " \n\nProfessional Courses Duration 2: " . $fields['professionalcoursesduration2']
. " \n\nProfessional Courses Year 2: " . $fields['professionalcoursesyear2']
. " \n\nProfessional Courses Organised by 2: " . $fields['professionalcoursesorganisedby2']
. " \n\n Employment Experience Name Address Organisation 1: " . $fields['employmentexpnameaddressorganisation1']
. " \n\nEmployment Exp Name Year From 1: " . $fields['employmentexpnameyearfrom1']
. " \n\nEmployment Exp Name Year to 1: " . $fields['employmentexpnameyearto1']
. " \n\nEmployment Exp Position 1: " . $fields['employmentexpposition1']
. " \n\nEmployment Exp Location 1: " . $fields['employmentexplocation1']
. " \n\nEmployment Exp Reason Leaving 1: " . $fields['employmentexpreasonleaving1']
. " \n\nEmployment Exp Name Address Organisation 2: " . $fields['employmentexpnameaddressorganisation2']
. " \n\nEmployment Exp Name Year From 2: " . $fields['employmentexpnameyearfrom2']
. " \n\nEmployment Exp Name Year To 2: " . $fields['employmentexpnameyearto2']
. " \n\nEmployment Exp Position 2: " . $fields['employmentexpposition2']
. " \n\nEmployment Exp Location 2: " . $fields['employmentexplocation2']
. " \n\nEmployment Exp Reason Leaving 2: " . $fields['employmentexpreasonleaving2']
. " \n\nEmployment Exp Name Address Organisation 3: " . $fields['employmentexpnameaddressorganisation3']
. " \n\nEmployment Exp Name Year From 3: " . $fields['employmentexpnameyearfrom3']
. " \n\nEmployment Exp Name Year To 3: " . $fields['employmentexpnameyearto3']
. " \n\nEmployment Exp Position 3: " . $fields['employmentexpposition3']
. " \n\nEmployment Exp Location 3: " . $fields['employmentexplocation3']
. " \n\nEmployment Exp Reason Leaving 3: " . $fields['employmentexpreasonleaving3']
. " \n\nCurrent Last Salary: " . $fields['currentlastsalary']
. " \n\nReferences Name 1: " . $fields['referencesname1']
. " \n\nReferences Designation 1: " . $fields['referencesdesignation1']
. " \n\nReferences Company Name 1: " . $fields['referencescompanyname1']
. " \n\nReferences Contact No 1: " . $fields['referencescontactno1']
. " \n\nReferences Email Id 1: " . $fields['referencesemailid1']
. " \n\nReferences Name 2: " . $fields['referencesname2']
. " \n\nReferences Designation 2: " . $fields['referencesdesignation2']
. " \n\nReferences Company Name 2: " . $fields['referencescompanyname2']
. " \n\nReferences Contact No 2: " . $fields['referencescontactno2']
. " \n\nReferences Email Id 2: " . $fields['referencesemailid2']
. " \n\nReferences Name 1: " . $fields['referencesname1']
. " \n\nFile: " . $fields['uploadField']
. " \n\nSubmission Date: " . $fields['submissiondate'] . " \n\n";
if (mail($email_address, $subject, $content, "From: " . $fields['email'] . "\r\nReply-To: " . $fields['email'] . "\r\nReturn-Path: " . $fields['email'] . "\r\n"))
{
echo "<h2 class='success'>Message Sent</h2>";
echo "<br/><p class='success'>Thank you <strong>" . $fields['name'] . "</strong>, your message has been submitted and someone will contact you shortly.</p>";
}
else
{
echo 'ERROR!';
}
}
}
function required($str, $val = false)
{
if (!is_array($str))
{
$str = trim($str);
return ($str == '') ? false : true;
}
else
{
return (!empty($str));
}
}
function valid_email($str)
{
return (!preg_match("/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}#)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*#(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD", $str)) ? false : true;
}
Writing the code to send mail with PHP is a major pain, believe me I have spent hours trying to do it manually. You finally get it working and then, oh, Google thinks it is spam. Then another service thinks it is spam. Not worth your time. Therefore I strongly recommend using a service such as http://www.mailgun.com/ which offers 10,000 emails a month for free. Check out this tutorial to get started: http://blog.mailgun.com/php-how-to-use-mailgun-efficiently/. Trust me, you want to do this.
The above code is designed to display info stored in sql table. everything is corresponding to the titles in the table and in the correct order. however the page it is from is only displaying the first 2 columns and not the others. everything looks as if it is in order to me. is my statement wrong?
<?php
$con=mysqli_connect("xxx","y","y","yyyy");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tripdata ");
while($row = mysqli_fetch_array($result))
{
echo $row['trip_id'] . " " . $row['image'] . " " . $row['date'] . " " . $row['destination'] . " " . $row['hl'] . " " . $row['cost'] . " " . $row['blurb'] . " " . $row['whatinc'] . " " . $row['whatopt'] . " " . $row['itin'] . " " . $row['depinfo'] . " " . $row['ppcode'];
echo "<br>";
}
mysqli_close($con);
?>
maybe you can try using assoc
while($row = mysqli_fetch_assoc($result))
{
echo $row['trip_id'] . " " . $row['image'] . " " . $row['date'] . " " . $row['destination'] . " " . $row['hl'] . " " . $row['cost'] . " " . $row['blurb'] . " " . $row['whatinc'] . " " . $row['whatopt'] . " " . $row['itin'] . " " . $row['depinfo'] . " " . $row['ppcode'];
echo "<br>";
}
i usual use this and no problem
assoc is index name based on field name
but array is based on number (0, 1, 2, 3)
Strange. Try do loop instead:
do {
echo $row['trip_id'] . " " . $row['image'] . " " . $row['date'] . " " . $row['destination'] . " " . $row['hl'] . " " . $row['cost'] . " " . $row['blurb'] . " " . $row['whatinc'] . " " . $row['whatopt'] . " " . $row['itin'] . " " . $row['depinfo'] . " " . $row['ppcode'];
echo "<br>";
} while($row = mysqli_fetch_array($result));
Im having problems passing PHP Strings to Javascript functions, I'v read a number of the post regarding this problem and tried several methods but none of them have worked for me.
basically I have two functions one written in PHP that pulls information from a database and the other written in Javascript designed to allow me to geocode an address for google maps and pass some info to be added to the info window, the two bits of code are shown below:
PHP
try {
$bubbleData = $dbConnection->getBubbleData();
} catch (Exception $e) {
echo "The following error occoured while attempting to get Google map info window data" .
" " . $e->getMessage() .
" " . "In file" .
" " . $e->getLine() .
" " . "on line" .
" " . $e->getLine();
}
if (mysql_num_rows($bubbleData) == 0) {
echo "No Placement data found to populate map";
} else {
while ($row = mysql_fetch_array($bubbleData)) {
$companyName = $row['Company_Name'];
$title = $row['Title'];
$address_1 = $row['Address_Line_1'];
$address_2 = $row['Address_Line_2'];
$address_3 = $row['Address_Line_3'];
$address_4 = $row['Address_Line_4'];
$post_Code = $row['Post_Code'];
$forename = $row['Forename'];
$surname = $row['Surname'];
$fullAddress = $address_1 . " " . $address_2 . " " . $address_3 . " " . $address_4 . " " . $post_Code;
$partAddress_1 = $address_1 . " " . $address_2;
$partAddress_2 = $address_3 . " " . $address_4;
$infoText = "<h4>" . $title . "</h4>" .
'<b>' . "Company name:" . '</b>' . " " . $companyName .
"</br>" .
"<b>" . "Employee name:" . '</b>' . " " . $forename . " " . $surname . '</br>' .
"<b>" . "Company address:" . "</b>" . " " . $partAddress_1 . '</br>' .
$partAddress_2 . '</br>' . $post_Code;
echo "<SCRIPT LANGUAGE='javascript'>
geocodeAddress(<?php echo json_encode($fullAddress);?>,<?php echo json_encode($infoText);?>);
</SCRIPT>'";
}
}
Javascript
function geocodeAddress (address,infoText) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode( {'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
loadMarker(results[0].geometry.location,infoText,address);
latlngArray.push(results[0].geometry.location);
} else {
alert("Geocode was not successful for the following reason: " +" "+ status);
}
});
}re
Now I know that the two work fine individually but whenever I try to call the javascript in side the PHP and pass it the variables I ge the following error in firebug:
Invalid value for property address:
I've been trying to make this happen all day, Ive used regexs plane echos and the json_encode() function and nothings worked can anyone help?
Thanks in advance
You have nested <?php ?> inside PHP code, which will not be interpreted as PHP. Rather, it just prints into your output as a string.
// Instead of
echo "<SCRIPT LANGUAGE='javascript'>
geocodeAddress(<?php echo json_encode($fullAddress);?>,<?php echo json_encode($infoText);?>);
</SCRIPT>'";
// Change to
echo "<SCRIPT LANGUAGE='javascript'>
geocodeAddress(" . json_encode($fullAddress) . "," . json_encode($infoText) . ");
</SCRIPT>'";
At least in my case, I have noticed that
<?php ...foo... ?>
segments in the HTML body, get php-evaluated only if the file extension is .php.
That is, I had to rename:
mv foo.html foo.php
to have php-values passed into javascript variables.