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'];
}
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>";
}
What am i doing wrong. The variable will not get picked up. I have tried to echo it out just to check but the echo was blank. What did i do wrong?
if($result->num_rows){
while($row = $result->fetch_array(MYSQLI_ASSOC)){
//Displays them
echo '<h4> ' . $row['fname'] . ' ' . $row['lname'] . ' <h4/>' .'<br/>';
}
request.php below
$requestedId = $_GET['account_id'];
$userId = $_SESSION['user_id'];
Thats the portions of the code i need help with. The account id is passed through the URL.
In the while loop you need to put the name account_id in it, not just the value
echo '<h4> <a href="request.php?account_id='.$row['account_id'].'">' .
$row['fname'] . ' ' . $row['lname'] . '</a> <h4/>' .'<br/>';
I am new to PHP and have just started the professional development in this language. I have created a dynamic dropdown list in php as under:
$sql="select DISTINCT State from branchinfo";
$result = $conn->query($sql);
if($result->num_rows > 0)
{
echo "< SELECT NAME='states'>";
while($row=$result->fetch_assoc())
{
echo "< OPTION NAME = '" . $row['State'] . "'" . " VALUE = '" . $row['State'] . "'>" . $row["State"];
}<br>
echo "< /SELECT>";
}
echo "< INPUT TYPE='submit' name='submit' value='submit'>";
Problem is when I select a state and click on submit, the list reloads and my selection is lost, the first option gets selected by default. I have tried to embed the script within OPTION but it didn't worked, I tried it as under:
echo "< OPTION NAME='" . $row['State'] . "'" . " VALUE='" . $row['State'] . "'" . if(isset($_POST["submit"])){ if($_POST["states"] == $row['State']) echo "selected";} . " >" . $row["State"];<br>
I am not using any javasrcipt / jquery till now on this page and not planning to use it either. plz provide a solution within this code. Please help.
Some additional information
The mthod i tried as mentioned above, works fine on hardcoded static drop downlist items written in html form. It stops working for dynamically generated list.
You have to add dropdown inside form tag, then it will work.
$sql = "select DISTINCT State from branchinfo";
$result = $conn->query($sql);
echo "<form method='POST'>";
if ($result->num_rows > 0) {
echo "<SELECT NAME='states'>";
while ($row = $result->fetch_assoc()) {
$sel = ( $_POST['states'] == $row['State'] ) ? "selected" : "";
echo "< OPTION NAME = '" . $row['State'] . "'" . " VALUE = '" . $row['State'] . "' " . $sel . ">" . $row["State"] . "</OPTION>";
}
echo "</SELECT>";
}
echo "<INPUT TYPE='submit' name='submit' value='submit'>";
echo "</form>";
Use session to remember your choice. And when the page is reloaded just retrieve your previous selected value.
like
while($row=$result->fetch_assoc())
{
if($sessionvalue==$row['State']){
echo "< OPTION SELECTED NAME = '" . $row['State'] . "'" . " VALUE = '" . $row['State'] . "'>" . $row["State"];
}else{
echo "< OPTION NAME = '" . $row['State'] . "'" . " VALUE = '" . $row['State'] . "'>" . $row["State"];
}
}
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));
I have an Access database that has been converted to MySQL. I have a table, Contacts, with fields 'EmailAddress', 'HideEmailAddress', 'FaxNumber', 'HideFaxNumber', 'PhoneNumber', and 'HidePhoneNumber'. The hide fields are all checkboxes, so have values of 1 or 0, with 1 being TRUE.
I need, either in my MySQL Select statement or in my PHP query, to be able to not show the information if the hide field has a value of 1. Also, it's pretty common that only one of the three fields will be marked as hide, so I can't do a blanket "if any of these are 1 hide all of them.' I need to output the query to a webpage, and either show or hide the three fields above depending on the value of the hide field for each column. My SELECT statement is retrieving all six values, and the PHP is turning it into an array, but I don't think anything with array comparison will help here.
I just have no idea what I should be doing here. I've been searching for a couple of days now and nothing seems to come close to what I need. I have a VERY ad-hoc if-elseif-else running in my PHP loop in order to get it done by the deadline, but that requires 9 or 12 different statements, and I'm sure there's a better way to do this.
Here's my code. I've chopped it up a lot from the original query, but that query does work, so if there are typos here, it's from the chop-up. I just can't figure out how to suppress a field based on the value of another field. This is also why I have the two AS statements in the SELECT statement - in the original query, there's information coming from five different tables and there's "FaxNumber" and "EmailAddress" in both of them. I left the AS in just in case it changes things.
<?php
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('Error connecting to MySQL server.');
$query = 'SELECT Contact.CompanyID, Contact.WebContact, Contact.FirstName, Contact.LastName, Contact.WorkPhone, Contact.HidePhoneNumber, Contact.FaxNumber AS ConFaxNumber, Contact.HideFaxNumber, Contact.EmailAddress AS ConEmailAddress, Contact.HideEmailAddress FROM Contact WHERE TDAT_Contact.WebContact = "1"';
mysqli_query($dbc, $query);
if($r = mysqli_query($dbc, $query)) {
while ($row = mysqli_fetch_array($r)) {
if(($row['HidePhoneNumber'] == 1) && ($row['HideFaxNumber'] == 1)) {
echo '<hr><div align="query"><span class="strong">Contact:</span> ' . $row['FirstName'] . ' ' . $row['LastName'] . '<br /><br /><span class="strong">Direct Email Address: </span>' . $row['ConEmailAddress'] . '</div>';
} elseif($row['HidePhoneNumber'] == 0) && ($row['HideFaxNumber'] == 0) {
echo '<hr><div align="query"><span class="strong">Contact:</span> ' . $row['FirstName'] . ' ' . $row['LastName'] . '<br /><span class="strong">Direct Telephone: </span>' . $row['WorkPhone'] . '<br /><span class="strong">Direct Fax: </span>' . $row['ConFaxNumber'] . '<br /><span class="strong">Direct Email Address: </span>' . $row['ConEmailAddress'] . '</div>';}
}
...
else { // Query didn't run.
echo '<p style="color: red;">Could not retrieve the data because:<br />' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
}
}}
mysqli_close($dbc); // Close the connection.
?>
<?php
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('Error connecting to MySQL server.');
$query = 'SELECT Contact.CompanyID, Contact.WebContact, Contact.FirstName, Contact.LastName, Contact.WorkPhone, Contact.HidePhoneNumber, Contact.FaxNumber AS ConFaxNumber, Contact.HideFaxNumber, Contact.EmailAddress AS ConEmailAddress, Contact.HideEmailAddress FROM Contact WHERE TDAT_Contact.WebContact = "1"';
mysqli_query($dbc, $query);
if($r = mysqli_query($dbc, $query)) {
while ($row = mysqli_fetch_array($r)) {
echo '<hr><div align="query"><span class="strong">Contact:</span> ' . $row['FirstName'] . ' ' . $row['LastName'] . '<br />';
if ($row['HidePhoneNumber'] == 0) {
echo '<span class="strong">Direct Telephone: </span>' . $row['WorkPhone'] . '<br />';
}
if ($row['HideEmailAddress'] == 0) {
echo '<span class="strong">Direct Email Address: </span>' . $row['ConEmailAddress'] . '<br />';
}
if ($row['HideFaxNumber'] == 0) {
echo '<span class="strong">Direct Fax: </span>' . $row['ConFaxNumber'];
}
echo '</div>';
}
} else { // Query didn't run.
echo '<p style="color: red;">Could not retrieve the data because:<br />' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
}
mysqli_close($dbc); // Close the connection.
You can do it in mysql and avoid extra php coding
something like this assuming the Hide fields are booleans
$query = 'SELECT Contact.CompanyID, Contact.WebContact, Contact.FirstName,Contact.LastName,
case when Contact.HidePhoneNumber then '' else Contact.WorkPhone end as WorkPhone,
case when Contact.HideFaxNumber then '' else Contact.FaxNumber end AS ConFaxNumber,
case when Contact.HideEmailAddress then '' else Contact.EmailAddress and AS ConEmailAddress FROM Contact WHERE TDAT_Contact.WebContact = "1"';
then on php you just
if($r = mysqli_query($dbc, $query)) {
while ($row = mysqli_fetch_array($r)) {
echo '<hr><div align="query"><span class="strong">Contact:</span> ' . $row['FirstName'] . ' ' . $row['LastName'] . '<br /><span class="strong">Direct Telephone: </span>' . $row['WorkPhone'] . '<br /><span class="strong">Direct Fax: </span>' . $row['ConFaxNumber'] . '<br /><span class="strong">Direct Email Address: </span>' . $row['ConEmailAddress'] . '</div>';
}
}
Break your html output in small pieces, observe the common patterns, and factorize it in functions:
function hide($field, $label, $row){
if ($row['Hide'.$field] == 0)
echo '<span class="strong">'.$label.'</span>'.$row[$field].'<br/>';
}
$hiddenfields = array ('EmailAddress' => 'Email Address', 'PhoneNumber' => 'Direct Telephone' /* etc. */);
foreach($hiddenfields as $field => $label)
hide($field,$label,$row);
This should get you started.
You are using AND operator(($row['HidePhoneNumber'] == 1) && ($row['HideFaxNumber'] == 1) which means both conditions must be true to execute the conditional code.To resolve your problem you can use 'nested loop' within your while loop and check expressions for example:
$conditions=array("0"=>"phonenumber","1"=>"fax","2"=>"email") //associative array
for($i=0;$i<count($conditions);$i++){
if($row[$conditions[$i]]==1){
echo $row.[$conditions[$i]];
}
else{
echo "user doesn't want to share this information";
}
}
if condition returns 1 it will echo associated array member.