I'm trying to pull a first name, last name, and email from a data base and trying to put into Firstname Lastname , with PHP. Every time I try to echo the data I only get Firstname Lastname. The email and <> doesn't show up? Anyone know how I can echo .
Here's the code:
while($row = mysqli_fetch_assoc($res)){
echo $row['firstname'] . " " . $row['lastname'] . " <" . $row['email'] . ">,";
}
Your code works fine, but < and > characters form a HTML tag, that is why you don't see the mail.
If you look at the source code of your echo, you will see the <mail> appearing.
You can also do this :
while($row = mysqli_fetch_assoc($res)){
echo htmlspecialchars($row['firstname'] . " " . $row['lastname'] . " <" . $row['email'] . ">,");
}
htmlspecialchars will transform < and > to their HTML code, so they will be visible even if you're not looking at the source.
A useful function to have is this one :
// Echoes the string in a HTML friendly way
function echoHTML($str)
{
echo htmlspecialchars($str);
}
while($row = mysqli_fetch_assoc($res)){
echoHTML($row['firstname'] . " " . $row['lastname'] . " <" . $row['email'] . ">,");
}
PS : be careful about the last comma if you want to concatenate email adresses, you will have a trailing , at the end of the list of mails. Here is how to avoid it :
$adresses = [];
while($row = mysqli_fetch_assoc($res))
{
// No more "," here
$adresses[] = $row['firstname'] . " " . $row['lastname'] . " <" . $row['email'] . ">";
}
echoHTML(implode(', ', $adresses));
Related
While writing my website I encountered the following Problem:
On the first page, you can enter some data like names, dates, addresses etc.
In addition, there are a few checkboxes with fixed data.
After filling out the form I want it to get sent to my e-mail, which is working for the most part.
<?php
//subject of the e-mail
$subject = "Test";
// the message
$msg = "Antragssteller: " . $_POST['ANachname'] . ", " . $_POST['AVorname'] . "<br>E-Mail: " . $_POST['AMail'] . "<br><br>" .
"Testperson: " . $_POST['MNachname'] . ", " . $_POST['MVorname'] . "<br>Adresse: " . $_POST['Adresse'] . "<br><br>";
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// send email
sendMailInt($subject, $msg);
?>
Now I want to add some input with checkboxes on my website, which also works up to a certain point.
<?php
//subject of the e-mail
$subject = "Test";
// the message
$msg = "Antragssteller: " . $_POST['ANachname'] . ", " . $_POST['AVorname'] . "<br>E-Mail: " . $_POST['AMail'] . "<br><br>" .
"Testperson: " . $_POST['MNachname'] . ", " . $_POST['MVorname'] . "<br>Adresse: " . $_POST['Adresse'] . "<br><br>" .
"checkbox1: " . $_POST['checkbox1'] . "<br>" .
"checkbox2: " . $_POST['checkbox2'] . "<br>" .
"checkbox3: " . $_POST['checkbox3'] . "<br>" .
"checkbox4: " . $_POST['checkbox4'] . "<br>" .
"checkbox5: " . $_POST['checkbox5'] . "<br>" .
"checkbox6: " . $_POST['checkbox6'] . "<br>" .
"checkbox7: " . $_POST['checkbox7'] . "<br>" .
"checkbox8: " . $_POST['checkbox8'] . "<br>" .
;
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// send email
sendMailInt($subject, $msg);
?>
Specifically for the checkbox input, I want to have something like an if-statement to check if the box has been checked or not.
If a box is unchecked, the value is empty, which means I have an empty line in my e-mail. Since I have around 20 checkboxes, it can get a bit messy in the e-mail, if 10 boxes in a row are unchecked.
I tried writing the $msg = as HTML mail and adding PHP code into it, but then my website stopped working (went completely blank, an error I encountered a few times before, when using wrong code)
<?php
//subject of the e-mail
$subject = "Test";
// the message
$msg = "
<html>
<head>
<title>Test Mail</title>
<body>
<?php
echo "Antragssteller: " . $_POST['ANachname'] . ", " . $_POST['AVorname'] . "<br>E-Mail: " . $_POST['AMail'] . "<br><br>" .
"Testperson: " . $_POST['MNachname'] . ", " . $_POST['MVorname'] . "<br>Adresse: " . $_POST['Adresse'] . "<br><br>";
if ($_POST['checkbox1'] == true)
echo $_POST['checkbox1'] . "<br>";
else
echo "";
if ($_POST['checkbox2'] == true)
echo $_POST['checkbox2'] . "<br>";
else
echo "";
if ($_POST['checkbox3'] == true)
echo $_POST['checkbox3'] . "<br>";
else
echo "";
if ($_POST['checkbox4'] == true)
echo $_POST['checkbox4'] . "<br>";
else
echo "";
if ($_POST['checkbox5'] == true)
echo $_POST['checkbox5'] . "<br>";
else
echo "";
if ($_POST['checkbox6'] == true)
echo $_POST['checkbox6'] . "<br>";
else
echo "";
if ($_POST['checkbox7'] == true)
echo $_POST['checkbox7'] . "<br>";
else
echo "";
if ($_POST['checkbox8'] == true)
echo $_POST['checkbox8'] . "<br>";
else
echo "";
?>
</body>
</html>
";
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// send email
sendMailInt($subject, $msg);
?>
Is there any way to skip empty variables in the mailer variable, so that the mail that gets sent looks clean without holes?
Looking forward to hearing from you guys and thanks in advance.
First thing you can do is in the file you're working with to force errors to show on the page (only use this in development environments):
error_reporting(E_ALL);
ini_set('display_errors', 1);
Second, multiple form inputs of the same data type can be greatly simplified by putting them in an array. Here's what the syntax would look like:
<input name="checkbox[]" value="1" />
<input name="checkbox[]" value="2" />
<input name="checkbox[]" value="3" />
<input name="checkbox[]" value="4" />
<input name="checkbox[]" value="5" />
Then in PHP you could do something like this that will only print out the checkboxes that were selected:
foreach ($_POST['checkbox'] as $checkbox) {
echo $checkbox . "<br>";
}
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.
How can i display search results using mysqli multiquery. I want to display values from my listing-details table and from my user table. Here is my code:
$searchquery="SELECT * FROM `listing-details` WHERE `listing-address` LIKE '%" . $address . "%' AND `listing-address-street` LIKE '%" . $street . "%' AND `listing-address-barangay-id` LIKE '%" . $barangay . "%'";
$searchquery.= "SELECT `user.user-username`, `user.user-firstname`, `user.user-lastname`, `listing-details.user-username` FROM `user`, `listing-details` WHERE `listing-details.user-username`=`user.user-username`";
if (mysqli_multi_query($conn, $searchquery)) {
do {
if ($result=mysqli_store_result($conn,$searchquery)){
while($row=mysqli_fetch_row($result)){
$listingid =$row['listing-id'];
$username =$row['user-username'];
$listingbedquantity =$row['listing-bedquantity'];
$listingbedtype =$row['listing-bedtype-id'];
$listingguestsquantity =$row['listing-guestsquantity'];
$listingplacetype =$row['listing-placetype-id'];
$listingpropertytype =$row['listing-propertytype-id'];
$listingbathroomquantity =$row['listing-bathroomquantity'];
$listingaddress =$row['listing-address'];
$listingstreet =$row['listing-address-street'];
$listingbarangay =$row['listing-address-barangay-id'];
$listingamenities =$row['listing-amenities-basic-id'];
$listingsafetyamenities =$row['listing-amenities-safety-id'];
$listingsaphotos =$row['listing-amenities-safety-photos-id'];
$listingspace =$row['listing-space-id'];
$listinglandmark =$row['listing-landmark'];
$listingpreferences =$row['listing-preferences-id'];
$listingphotoset =$row['listing-photosset-id'];
$listingexperience =$row['listing-experience-id'];
$listingfrequency =$row['listing-frequency-id'];
$listingstartdate =$row['listing-startdate'];
$listingrate =$row['listing-rate-id'];
$listingprice =$row['listing-price'];
$listingrules =$row['listing-rules-id'];
$listingtitle =$row['listing-title'];
$listingdescription =$row['listing-description'];
$firstname =$row['user-firstname'];
$lastname =$row['user-lastname'];
echo "<ul>\n";
echo "<li>"."" . "<h2>" . $listingtitle . "</h2></li>\n";
echo "<li><h6>" . $listingaddress . ", " . $listingstreet . ", " . $listingbarangay . "</h6></li>";
echo "<li><i>" . $listingdescription . "</i></li>";
echo "<ul>\n";
echo "<li>"."" . "<h2>" . $listingtitle . "</h2></li>\n";
echo "<li><h6>" . $listingaddress . ", " . $listingstreet . ", " . $listingbarangay . "</h6></li>";
echo "<li><i>" . $listingdescription . "</i></li>";
echo "<li style='float:right;'>By: " . $firstname . " " . $lastname . "</i></li>";
echo "</ul>";
echo "<hr width='80%' noshade='1'>";
}
mysqli_free_result($result);
}
}
while (mysqli_next_result($conn));
}
However, when I get to run it,the page loads, but results won't show. The purpose of it is to be able to display listing details from a listing-details table listed by the complete name from the user table. Two two tables have user-username column as common key.
Like many learners, you are using the wrong tool, simply because you don't know the proper one.
You need not a multiquery (which you never actually need anyway) but a JOIN.
Simply rewrite your two monster queries to a join like this
$searchquery="SELECT l.*, u.`user-username`, u.`user-firstname`, u.`user-lastname`
FROM `listing-details` l, user u
WHERE l.`user-username`=u.`user-username`
AND `listing-address` LIKE ...";
Besides, your quoting is wrong.
A form sends name, topic, email, and blurb to MySQL db. Email is not a required field, but if it is !empty I want to include it as the name and echo the row data.
This is my idea so far:
if (!empty($_POST['email']))
{
$row['name'] = "<A href='mailto:' . $_POST['email'] . '>' . $_POST['name'] . '</A>";
}
else
{
$row['name'] = $_POST['name'];
}
I am having trouble sorting out the coding for the email/name any help please. And the rows will be:
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td><Posted by : ' . $row['name'] . ' on ' . $row['date'] . '<br />
Topic : ' . $row['topic'] . '<br />Thoughts : ' . $row['thoughts'] . '</td>';
echo "</tr>";
Not very well sorted yet, help please :-) thanks.
are you want that if the email is not empty then you want to replace it with name and echo it? if yes then try the below code..
if (!empty($_POST['email']))
{
$row['name'] =$_POST['email'];
$row['name']="<A href='mailto:' . $_POST['email'] . '>' . $_POST['name'] . '</A>";
}
else
{
$row['name'] = $_POST['name'];
}
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.