I have tried creating a list within a container in PHP and I am struggling because it's my first time doing a list in PHP. I want the list within the container to look like an example I found on http://getbootstrap.com/components/#list-group this example is under "List Group" then it's the "Linked Items" example.
My code:
// List
echo_lines(array(
// All cases
"<ul>",
"<div class='row'>",
"<div class='col-md-6'>",
"<div class='panel panel-default'>",
"<div class='panel-heading clearfix'>",
"<h4 style='margin:0; margin-top:5px; padding:0;'>All Projects</h4>",
"</div>",
"<div class='panel-body'>",
// Content
"<div class='list-group'>"
"<a href='#' class='list-group-item'>"Orange"</a>"
"<a href='#' class='list-group-item'>"Pizza"</a>"
"<a href='#' class='list-group-item'>"Beef"</a>"
"<a href='#' class='list-group-item'>"Chicken"</a>"
"</ul>",
The code you gave us is way to messy. You don't close most of the html tags, and I'm not sure what you're trying to achieve
Here is the example from bootstrap documentation that you linked, more or less mixed with your code.
I'm still not sure if "Orange", "Beef", "Pizza" and "Chicken" are just string or you want to use variables (since you escaped them on your code)
$str = "";
$str .= '<div class="list-group">';
$str .= ' Orange';
$str .= ' Beef';
$str .= ' Pizza';
$str .= ' Chicken';
$str .= ' ' . $variable . ''; // this is an example, if you want to use variable
$str .= '</div>';
echo $str;
If you want to use " or ' into a string, you have to escape it with a backslash \ depending on how you write your string.
Example :
$str = "";
$str .= 'Here you can use "double quotes" without escaping<br />';
$str .= "There you will need to escape \"double quotes\"<br />";
$str .= 'There you will need to escape \'simple quotes\'<br />';
$str .= "There, escaping 'simple quotes' is unnecessary";
More info at PHP: Strings manual
In PHP you must concatenate variables or constants to your string with the . operator, and you forgot to keep > within the quotation ("):
"<a href='#' class='list-group-item'>". Orange. "</a>"
"<a href='#' class='list-group-item'>". Pizza. "</a>"
"<a href='#' class='list-group-item'>". Beef. "</a>"
"<a href='#' class='list-group-item'>". Chicken. "</a>"
Also, why are you sometimes using commas and sometimes not?
Use php array :
<?php
$arr_foods = array('Orange', 'Pizza','Beef', 'Chicken');
$str_list_group = '';
foreach($arr_foods as $str_food)
{
//Concatenation
$str_list_group =. "<a href='#' class='list-group-item'>". $str_food. "</a>\n";
}
print $str_list_group;
?>
Related
I am trying to update a script written by someone else and need some help, as I do not work in Javascript often. The script is part of a SurveyGizmo survey, so may use some syntax/functions not standard in Javascript.
The script below works. It takes the results of each category's quiz score and prints a message stored in another variable in the survey. There are 5 categories. The output looks like this:
Category 1 Score
Category 1 Message
Category 2 Score
Category 2 Message
etc..
Below this code is my question.
function sgapiQuizResults(){
%%html .= '<div class="recommendations"><div class="something">Recommendations</div>';
foreach (sgapiQuizMessages() as %%category => %%message) {
%%html .= '<div class="recommendation '. sgapiCSSClassName(%%category) .'">';
%%html .= '<div class="header">' . %%category .'</div>';
%%html .= '<div class="graphic">'. sgapiScore(%%category) . '</div>';
%%html .= '<div class="content">' . %%message . '</div>';
%%html .= '</div>';
}
%%html .= '</div>';
return %%html;
}
What I would like to do is modify this to add additional messaging that cannot be stored in a variable. Basically, I would like to add a condition similar to as follows:
if ( Number(sgapiGetValue(8)) < 3 ) {
%%html .= "Message for Question 8";
}
if ( Number(sgapiGetValue(9)) < 3 ) {
%%html .= "Lorem Ipsum for 9";
}
if ( Number(sgapiGetValue(10)) < 3 ) {
%%html .= "Dolor for 10";
}
... and so on ...
The output of which would look like:
Category 1 Score
Category 1 Message
Message for Question 8
Category 2 Score
Category 2 Message
Lorem Ipsum for 9
etc..
I have been successful at simply adding &&html .= 'Hello World'; to the code, having that message printed to each category; however, any time I try to include a conditional statement, the script breaks.
Any help would be much appreciated!
function sgapiQuizResults(){
%%html .= '<div class="recommendations"><div class="something">Recommendations</div>';
foreach (sgapiQuizMessages() as %%category => %%message) {
%%html .= '<div class="recommendation '. sgapiCSSClassName(%%category) .'">';
%%html .= '<div class="header">' . %%category .'</div>';
%%html .= '<div class="graphic">'. sgapiScore(%%category) . '</div>';
%%html .= '<div class="content">' . %%message . '</div>';
if ( Number(sgapiGetValue(8)) < 3 ) {
%%html .= '<div class="content">Message for Question 8</div>';
}
if ( Number(sgapiGetValue(9)) < 3 ) {
%%html .= '<div class="content">Lorem Ipsum for 9</div>';
}
if ( Number(sgapiGetValue(10)) < 3 ) {
%%html .= '<div class="content">Dolor for 10</div>';
}
%%html .= '</div>';
}
%%html .= '</div>';
return %%html;
}
revised.
can try this?
in the below php statement I want to assign ID to EmployeeName.I will use the ID tag to search an element in the echoed list by name.Where am I making the mistake ?
<?php while( $toprow4 = sqlsrv_fetch_array( $stmt4) ) {
echo "<div class='parent-div'><span class='rank'>" . $toprow4['rank'] . "</span><span class='name'>" id = ' . $toprow4['EmployeeName'] .' "</span><span class='points'>" . $toprow4['pointsRewarded'] . "</span></div>";
} ?>
Add the id to the div element as an attribute and not as textContent
echo "<div class='parent-div' id='" . $toprow4['EmployeeName'] . "'><span class='rank'>" . $toprow4['rank'] . "</span><span class='name'>" . $toprow4['EmployeeName'] . "</span><span class='points'>" . $toprow4['pointsRewarded'] . "</span></div>";
and try and make that name a valid ID by removing spaces or replace them by -.
You need to check your quotes. If you aren't already, I'd strongly recommend using an editor with code highlighting.
<?php
while ($toprow4 = sqlsrv_fetch_array( $stmt4)) {
$rank = $toprow4['rank'];
$id = $toprow4['EmployeeName'];
$points = $toprow4['pointsRewarded'];
echo "<div class='parent-div'><span class='rank'>$rank</span>";
echo "<span class='name' id='$id'></span><span class='points'>$points<span></div>";
}
?>
Seems like you have a big problem escaping your output string correctly. Perhaps you should to try write it clean by using templates and sprintf() like in this sample.
i did not test the logic of the while statement you have given
<?php
$template = '<div class="parent-div">';
$template .= '<span class="rank">%s</span>';
$template .= '<span class="name">id = %s</span>';
$template .= '<span class="points">%s</span>';
$template .= '</div>';
while($toprow = sqlsrv_fetch_array($stmt4) {
echo sprintf(
$template,
$toprow['rank'],
$toprow['EmployeeName'],
$toprow['pointsRewarded']
)
}
?>
Your code is not clean, so you failed on making correct quotes and single-quotes.
I'm making a program that shows several descriptions taken from a database (MySQL):
echo "<input type=\"submit\" id=\"Boton" . $i . "\" value=\"Mostrar Descripcion\""
. " onclick=cambiarBoton(" . $i . ", \"" . $row["descripcion"] . "\") />";
echo "<div class=\"entrada-descripcion\" id=\"Descripcion" . $i . "\"> </div>";
where $row["descripcion"] is the access to the description and $i is an identifier because i'm using loops. That code generates a button that i must press to show the description. The javascript function "cambiarBoton" is the one that do that changes:
function cambiarBoton(i, d){
if (document.getElementById("Boton"+i).value == "Mostrar Descripcion"){
document.getElementById("Boton"+i).value = "Ocultar Descripcion";
document.getElementById("Descripcion"+i).innerHTML = d;
}else if(document.getElementById("Boton"+i).value == "Ocultar Descripcion"){
document.getElementById("Boton"+i).value = "Mostrar Descripcion";
document.getElementById("Descripcion"+i).innerHTML = "";
}
}
Ok, but there is a problem in the program and this is that i can't pass the description to the function in javascript and this doesn't works. How can i do this?
(I must do this without using AJAX)
I answer to the question I think you're asking is:
<?php echo "<script>var data = ".$row["description"].";</script>"; ?>
The problem was in the call of the function "cambiarBoton" in the code PHP,
it should have been this:
echo "<input type=\"submit\" id=\"Boton" . $i . "\" value=\"Mostrar Descripcion\""
. " onclick=\"cambiarBoton(" . $i . ", '" . $row["descripcion"] . "')\" />";
And then it works.
I have a contact page with a HTML Form.
I use jQuery to validate the fields. Then use jQuery's .ajax() method to send the information to the php file that processes and sends out the email via the mail() method.
Inside the PHP File i have an IF statement that checks to see if a POST variable is set that way I can put all form processing on one page for multiple forms through out the website.
I get all variables and do another form field validation with PHP. Then I build out the HTML Email and it's headers.
If the PHP Validation is successful I then send the email via the mail() method. Then I check if the mail() method was successful and if so I send another "auto reply" email.
Inside of the IF statements that check to see if the mail() method was successful I echo a success or error message with json_encode().
When the user clicks the submit button on the form I have it set to return false so it stays on the same page and have a message displayed upon send success.
Both emails send successfully from the form. Except my .ajax() method does not receive the success or error message from the php files' json_encode().
I removed the return false in the jQuery .click and have tried a standard PHP echo inside the php IF statement that checks if the post isset() and I couldn't get it to print to the browser. nor does the json_encode print to the browser. However, when I put the PHP echo outside of the IF statement it printed just fine. Which confuses me because it obviously goes inside that if statement to send the email but won't echo. What am I doing wrong?
jQuery
$("#contactFormSubmit").click(function(){
// validate and process form here
// NAME VALIDATION
var name = $("input#nameField").val();
if (name == "") {
$("input#nameField").focus();
$("input#nameField").css("border","1px solid red");
alert("nameFieldError");
return false;
}
// EMAIL VALIDATION
var formEmail = $("input#emailField").val();
if (formEmail == "" || !validateEmail(formEmail)) {
$("input#emailField").focus();
$("input#emailField").css("border","1px solid red");
alert("emailFieldError");
return false;
}
// PHONE VALIDATION
var phone = $("input#phoneField").val();
var phoneReg = "/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/";
if (phone == "" || !isValidUSPhoneFormat(phone)) {
//alert("phone is wrong");
$("input#phoneField").focus();
$("input#phoneField").css("border","1px solid red");
alert("phoneFieldError");
return false;
}
var message = $("textarea#messageField").val();
var subject = $("#subjectField").val();
var dataString = 'name='+ name + '&email=' + formEmail + '&phone=' + phone + '&subject=' + subject + '&message=' + message + '&submitContactForm=1';
$.ajax({
type: "POST",
url: "process-form.php",
data: dataString,
dataType:"json",
success: function(response) {
if(response.status === "success") {
alert("success");
// do something with response.status or other data on success
} else if(response.status === "error") {
alert("error");
// do something with response.status or other data on error
}
},
error: function(xhr,errmsg) { alert(errmsg); }
});
return false;
});
PHP File
<?php
include "includes/functions.php";
if(isset($_POST['submitContactForm'])) {
$nextEventDay = getDay();
$eventToShow = "";
$dayToShow = "";
$dateToShow = "";
if ($nextEventDay == "Sunday" || $nextEventDay == "Monday" || $nextEventDay == "Tuesday" || $nextEventDay == "Wednesday" ||$nextEventDay == "Thursday" || $nextEventDay != "Friday" || $nextEventDay != "Saturday") {
$eventToShow = "thurEvent";
$dayToShow = "THU";
$dateToShow = getNextThursdayDate();
}
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$messageBody = $_POST['message'];
$sendTo = "xxxx#xxxx.com";
$confirmTo = $email;
//HTML EMAIL
$headers = "From: " . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
//COMPANY EMAIL
$message5 = '<html><style type="text/css">table, td, tr, th{border:none !important;border-color:#111 !important;border-collapse:collapse;}a{color:#c92626 !important;}.title{color:#aaa;}</style><body style="background-color: #111;color:#ddd;border:2px solid #333;">';
$message5 .= '<table rules="all" style="background-color:#111;border:none !important;width:100%;" cellpadding="10" border="0">';
$message5 .= '<tr style="border:none;"><td style="border:none;padding:20px 0px !important;"><img src="images/logo.jpg" alt="Nightclub" style="display:block;margin:0 auto;min-width:260px;max-width:300px;width:50%;" width="260" /></td></tr>';
$message5 .= "<tr style='border:none;'><td style='border:none;'><img src='/images/" . $eventToShow . "-email-next-event.jpg' width='260' style='min-width:260px;max-width:1024px;width:100%;display:block;margin: 0 auto;' /></td></tr>";
$message5 .= "<tr style='border:none;background-color:#161616;'><td style='border:none;' class='title'><strong style='color:#aaa;'>Name:</strong> " . strip_tags($name) . "</td></tr>";
$message5 .= "<tr style='border:none;'><td style='border:none;' class='title' ><strong style='color:#aaa;'>Email:</strong> <span style='color:#c92626 !important;'>" . strip_tags($email) . "</span></td></tr>";
$message5 .= "<tr style='border:none;background-color:#161616;' ><td style='border:none;' class='title'><strong style='color:#aaa;'>Phone:</strong> <span style='color:#c92626 !important;'>" . strip_tags($phone) . "</span></td></tr>";
$message5 .= "<tr style='border:none;'><td style='border:none;' class='title'><strong style='color:#aaa;'>Subject:</strong> " . strip_tags($subject) . "</td></tr>";
$message5 .= "<tr style='border:none;background-color:#161616;' ><td style='border:none;' class='title'><strong style='color:#aaa;'>Message:</strong> " . strip_tags($messageBody) . "</td></tr>";
$message5 .= "<tr style='border:none;'><td style='border:none;'></td></tr>";
$message5 .= "</table>";
$message5 .= "</body></html>";
//CLIENT EMAIL
$areply = '<html><style type="text/css">table, td, tr, th {border:none !important;border-color:#111 !important;border-collapse:collapse;}a {color:#c92626 !important;}.title{color:#aaa;}#date a{color:#fff !important;text-decoration:none;}</style><body style="background-color: #111;color:#ddd;border:2px solid #333;">';
$areply .= "<table rules='all' style='background-color:#111;border:none !important;width:100%;' cellpadding='10' border='0'>";
$areply .= "<tr style='border:none;'><td style='border:none;padding:20px 0px !important;'><img src='images/logo.jpg' alt='Nightclub Ann Arbor' style='display:block;margin:0 auto;min-width:260px;max-width:300px;width:50%;' width='260' /></td></tr>";
$areply .= "<tr style='border:none;'><td style='border:none;'><img src='images/" . $eventToShow . "-email-next-event.jpg' width='260' style='min-width:260px;max-width:1024px;width:100%;display:block;margin: 0 auto;' /></td></tr>";
$areply .= "<tr style='border:none; background:#151515;'><td style='border:none;text-align:justify;background-color:#161616;'><div style='float:left;display:inline-block;background-color:#000;margin:0px 10px 10px 0px;font-size:197%; padding: 25px 30px;'><p id='date' style='margin:0;color:#fff !important;'> \r\n <strong>" . $dateToShow ."</strong></p><p style='margin:0;color:#c92626 !important;'>" . $dayToShow ."</p></div><p style='margin-top:10px;margin-right:15px;'>Thank you for contacting us at Nightclub . We look forward to assisting you. Below is the information that we recieved and we will be contacting you as soon as possible. Thank you again, and we look forward to speaking with you. If you have any additional questions please contact us at our website (<a href='' style='color:#c92626'></a>), give us a call <span style='color:#c92626 !important;'></span>, or send us an Email <span style='color:#c92626 !important;'></span></p></td></tr>";
$areply .= "<tr style='border:none;'><td style='border:none;' class='title'><strong style='color:#aaa;'>Name: </strong>" . strip_tags($name) . "</td></tr>";
$areply .= "<tr style='border:none; background-color:#161616 !important;' ><td style='border:none !important;background-color:#161616 !important;' class='title'><strong style='color:#aaa;'>Email:</strong> <span style='color:#c92626 !important;'>" . strip_tags($email) . "</span></td></tr>";
$areply .= "<tr style='border:none;'><td style='border:none;' class='title'><strong style='color:#aaa;'>Phone:</strong> <span style='color:#c92626 !important;'>" . strip_tags($phone) . "</span></td></tr>";
$areply .= "<tr style='border:none;background-color:#161616;' ><td style='border:none;' class='title'><strong style='color:#aaa;'>Subject:</strong> " . strip_tags($subject) . "</td></tr>";
$areply .= "<tr style='border:none;'><td style='border:none;' class='title'><strong style='color:#aaa;'>Message:</strong> " . strip_tags($messageBody) . "</td></tr>";
$areply .= "<tr style='border:none;'><td style='border:none;'></td></tr>";
$areply .= "</table>";
$areply .= "</body></html>";
$subject2 = "Thank you for your expressed interest ()";
$noreply = "xxx#xxx.com";
$headers2 = "From: " . $noreply . "\r\n";
$headers2 .= "MIME-Version: 1.0\r\n";
$headers2 .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (empty($name) || empty($email) || empty($phone) || !preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[#][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email) || !preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
echo json_encode(array(
'status' => 'error'
//'message'=> 'error message'
));
} else {
$send = mail($sendTo, $subject, $message5, $headers);
}
if($send){
echo json_encode(array(
'status' => 'success'
//'message'=> 'success message'
));
$send2 = mail($email, $subject2, $areply, $headers2);
} else {
echo json_encode(array(
'status' => 'error'
//'message'=> 'success message'
));
}
}
?>
UPDATE
I added the error call back to the jQuery ajax() method and found I was receiving the error I also removed everything from my PHP file except for the:
echo json_encode(array(
'status' => 'success'
//'message'=> 'success message'
));
And I still get the error message from the jQuery ajax() method. So it must be in my jQuery code... I think.
Ok, so thanks to the advice from #PatrickEvans and #MartyMcKeever above, I solved it by looking at the firebug console to find out that one of my custom PHP functions was missing a required argument. This was causing a parse error which was then making my jQuery throw an error but still allow the PHP to process the email.
If you look at the above PHP code there is a method that says $nextEventDay = getDay();
It should have been $nextEventDay = getDay("today");. This was causing all the problems
give #MartyMcKeever's comment an up vote.
I am Trying to escape these darn quotes and stuff in JS. I'm trying to use json_encode. My head hurts from looking at quotes, help???
$list = '';
// some loop here
$message = 'centeredPopup(this.href,"myWindow","500","300","yes")';
$jscode = 'json_encode('.$message.');return false';
$list .= '<p>
<a href="http://www.example.com/something.php?id=' . $id . '" onclick="'
.htmlspecialchars($jscode) . '" >' . $name . '</a></p><br>';
If you want to call json_encode there, you need to do this.
$jscode = json_encode($message).';return false';
You don't even need json_encode for this. You can put this there:
$message = 'centeredPopup(this.href,"myWindow","500","300","yes")';
$jscode = $message.';return false';
$list .= '<p> <a href="http://www.example.com/something.php?id=' . $id . '" onclick="' .htmlspecialchars($jscode) . '" >' . $name . '</a></p><br>';
Try:
$message = 'centeredPopup(this.href,"myWindow","500","300","yes")';
$jscode = json_encode($message);
Otherwise everything else should be OK.