PHP trying to calculate total of variables from an array - php

So I am doing the same grocery list I did before in JScript for an assignment but now in PHP. When I try to calculate the total it gives me a strange number.
$result is a calculation from product price multiplied by the quantity (waarde & aantal, its Dutch).
so when I do $total += $result;
And then I echo that, I get a very strange result.
To me it looks like it does not go by all the results. I tried using the $i index for it. But that does not work. What am I missing?
(Very new to PHP just learned a bit of the basic of JavaScript)
The expected outcome is all the totals shown in the table added together to create the total: 15.76
The result now is 31.984.85.98, what even is that magical number? Might be that I do something wrong with the number format, looking into that now too. (Also is it normal to share the code as I did? Apparently it's not really for PHP, I guess because it being a server-side thing) I have a CodePen of the original JavaScript version of it : https://codepen.io/3lly/pen/oNxaPKg maybe for clear view, you can see what I mean. The totals of all the total column cells.
table {
margin-top: 20px;
display: inline-block;
}
th, td , input {
border: 2px solid #FFB000;
padding: 2px;
color: black;
}
body {
text-align: center;
}
h1 {
color: #FFB000;
font-family: impact;
text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}
p {
color: white;
}
div {
display: inline-block;
}
#add {
display: inline-block;
padding: 10px 20px;
background: orange;
box-shadow: -3px 3px black, -2px 2px black, -1px 1px black;
border: 1px solid orange;
}
#add:hover {
background: green;
color: white;
}
input[type="text"], input[type="number"] {
background-color: skyblue;
}
input[type="number"]:hover {
background-color: black;
}
#totaal {
border: 2px solid #FFB000;
background-color: #282828;
padding: 5px;
color: skyblue;
}
#totaal:hover{
background-color: green;
color: white;
}
<head>
<link rel="stylesheet" href="style.css">
<title>Boodschappenlijst</title>
</head>
<body>
<h1> Boodschappenlijst </h1>
<div id="container"></div>
<table>
<tr>
<?php
$headerTexts = ['Name', 'Prijs', 'Aantal', 'Totaal'];
for($i=0;$i<count($headerTexts);$i++) {
echo "<th>" . $headerTexts[$i] . "</th>";
}
?>
</tr>
<?php
for($i=0;$i<count($products);$i++) {
//result calculations
$total = 0;
$result = number_format($products[$i]['waarde'] * $products[$i]['aantal'],2);
$total += $result;
echo $total;
//echo "<pre>" . is_int($products['waarde']) . "</pre>";
//Table Rows
echo "<tr>";
echo "<td>" . $products[$i]['omschrijving'] . "</td>" .
"<td>" . $products[$i]['waarde'] . "</td>" .
"<td>" . $products[$i]['aantal'] . "</td>" .
"<td>" . $result . "</td>";
echo "</tr>";
}
?>
</tr>
</table>
<p>Naam</p>
<form>
<input type="text" name="item" id="naam" /><br />
<p>Aantal</p>
<input type="text" name="quantity" id="qty" /><br />
<p>Prijs</p>
<input type="text" name="price" id="prijs" /><br/><br />
<input type="button" value="Add Product" onclick="updateTable()" id="add"><br /><br />
</form>
<div id="totaal"></div>
<!-- <script src="script.js"></script>-->
</body>
</html>

So I pushed the results of the total calculation of each products to the array which I did not do before like so :
$products[$i]['total'] = $products[$i]['waarde'] * $products[$i]['aantal'];
Next I did the calculation like so:
$total += $products[$i]['total'];
Within the loop. And that fixed it :D It was very simple apperently !

Related

How to align form and text generated by function?

I'm developing a webpage that has many forms with some text close to form. This form and text are generated by a function, but I get this
As you can see the forms and the text are not aligned with forms and text below. I would like to have this:
(this work because I put spaces manually). This is how I create forms.
function add_form($product,$name,$productp)
{
$productpt = "X";
echo $name;
echo '<input type="text" name="'. $product. '" id="'. $product. '" style="font-size:12pt;height:30px"/> x'.$productp. '€
price:'.$productpt.'€<br>';
}
echo '<form method="post" name="form1" id="form1" autocomplete="off">'; //start form
echo '<div class="leftpane">'; //start leftpane
echo '<h1>';
echo '<font size="6" color="red"> ALIMENTS </font><br>';
//ADD ELEMENTS IN MY MENU
//The first argument is the form name and id, the second is the name that will be printed(to the left of the form), the third argument is the price.
$menu[] = new Menu("pasta", "Pasta", 2);
$menu[] = new Menu("spaghetti", "Spaghetti", 1.50);
$menu[] = new Menu("pizza", "Pizza", 5);
$menu[] = new Menu("chicken_wings_x4", "Chicken Wings X4", 4.50);
$menu[] = new Menu("cheeseburger", "Cheeseburger", 6);
$menu[] = new Menu("Sandwich", "Sandwich", 2);
$menu[] = new Menu("hamburger", "Hamburger", 4.50);
$menu[] = new Menu("stuff", "stuff", 15.50);
//FOR EACH ELEMENT CREATE A FORM
for($i=0; $i<count($menu); $i++){
add_form($menu[$i]->form_name, $menu[$i]->name, $menu[$i]->price);
}
echo '</h1>';
echo '</div>'; //Close leftpane
...Do Others stuff in middlepane and in rightpane
To be clearer in html, for example the first form will be:
<input type="text" name="pasta" id="pasta" style="font-size:12pt;height:30px"/> x2€ price:X€<br>
I thought to divide in 3 parts my leftpane and to place $name to the left, the form to the middle and the price to the right. But the problem is that i create form in function, so I do not know how to do it.
This is my css:
<style>
input[type=text] {
width: 10%;
margin: 7.5px 0;
box-sizing: border-box;
}
input[type=submit].class1 {
width: 50%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
}
input[type=submit].class2 {
width: 50%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
}
body, html {
width: 100%;
height: 100%;
margin: 0;
}
.container {
width: 100%;
height: 100%;
}
.leftpane {
width: 33%;
height: 100%;
float: left;
border-collapse: collapse;
}
.middlepane {
width: 33%;
height: 100%;
float: left;
border-collapse: collapse;
}
.rightpane {
width: 33%;
height: 100%;
position: relative;
float: right;
border-collapse: collapse;
}
form {
display: inline-block;
}
h1 {
font-size: 19.5px;
}
</style>
I would suggest you to use a table. https://www.w3schools.com/html/html_tables.asp
Or you could wrap every section into a div-container and define its width.
I think your output code would really benefit from some additional HTML structure with classes that allow you to control their layout in a consistent way with CSS. I would update the add_form() function:
function add_form($product,$name,$productp) {
$productpt = "X";
echo '<div class="form-wrap">';
echo '<span class="form-name">' . $name . '</span>';
echo '<input type="text" name="'. $product. '" id="'. $product. '"/> <span class="form-label">x'.$productp. '€</span> <span class="form-price">price:'.$productpt.'€</span>';
echo '</div>';
}
Then with the output having more targetable HTML structure you can do this with CSS:
.form-wrap input {
font-size: 12pt;
height: 30px
}
.form-name {
display: inline-block;
width: 100px;
margin-right: 20px;
}
.form-label {
display: inline-block;
margin-left: 5px;
margin-right: 20px;
}
<div class="form-wrap">
<span class="form-name">Something</span>
<input type="text" name="something" id="somethingId" />
<span class="form-label">x1€</span>
<span class="form-price">price:1€</span>
</div>
<div class="form-wrap">
<span class="form-name">Another one</span>
<input type="text" name="anotherone" id="somethingId2" />
<span class="form-label">x2€</span>
<span class="form-price">price:2€</span>
</div>
<div class="form-wrap">
<span class="form-name">Number Three</span>
<input type="text" name="thirdone" id="somethingId3" />
<span class="form-label">x3€</span>
<span class="form-price">price:3€</span>
</div>
Try this.
function add_form($product,$name,$productp)
{
$productpt = "X";
echo "<table><tr>";
echo "<td width='20%'>" . $name . "</td>";
echo "<td width='20%'><input type='text' name='". $product . "' id='" . $product . "' style='font-size:12pt;height:30px;' /></td>";
echo "<td width='20%'>x" . $productp . "€</td>";
echo "<td width='20%'>price:" . $productpt . "€</td>";
echo "</tr></table>";
}

search database with php POST and mysql_fetch_ array

I'm trying to create a search form to search through my mysql database.
I succeded to to create the form with one text field and a submit buttom. BUT..
When I leave the text field blank and hit the submit button all results appear from the specific database table on my search.php site.
When i write anything in the text field (text that is included in the names from the database) and hit the submit button no search results appear at all.
As you can see in the code I've been trying to list the search results in two diferent ways. The first one reacts as described above. The last one does not list any search results at all.
Do anybody know why? Do I have a typo somewhere or do I miss anything ind the code?
Thanks in advance.
This is my index.php
<! DOCTYPE html>
<html>
<head>
<meta charset ="UTF-8">
<title> Townin </title>
<link rel="stylesheet" type="text/css" href="Townin/style.css">
<style>
body {background-color: white;}
header{background-color:#6ab47b;
border-color:#599a68;
}
header h1, h2 {margin-left: 20px;
color: white;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
h1, h1 {margin-left: 20px;
color: gray;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
p {margin-left: 20px;
color: gray;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
nav a, nav a:visited {
color: white;
background-color: #599a68;}
nav a.selected, nav a:hover{color: black;
background-color:#6ab47b;}
nav {
text-align:center;
padding: 0 0;
margin:20px 0 0;
height: 50 px;
background-color:#599a68;
}
nav ul {
list-style:none;
margin: 0px 10px;
padding: 0px;
}
nav ul li {
display: inline-block;
border-right: 1px solid #6ab47b;
text-align: center;
width: 250px;
padding:0px 0px;
}
nav ul li a {
display: block;
height: 40px;
width: 100%;
line-height:50px;
background-color: #599a68;
}
li a:hover {
background-color: #6ab47b;
}
a:link {
text-decoration: none;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
color:gray;
}
a:visited {
color:gray;
}
.button {
background-color:#6ab47b;
color: #fff;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
font-weight: bold;
margin: 0px 20px;
padding:20px 20px;
border-radius: 100 %;
}
.button:visited {
color:white;
}
#bar-knap {
display: inline-block;
max-width: 100%;
border-radius:10%;
height:40%;
width:20%;
margin: 10px 10% 10px 10%;
border-style:solid;
border-color:gray;
}
#spise-knap {
display: inline-block;
max-width: 100%;
border-radius:10%;
height:40%;
width:20%;
margin:10px 10% 10px 10%;
border-style:solid;
border-color:gray;
}
footer {
background-color: #6ab47b;
color: white;
height: 70px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
text-align: center;
margin: 20px auto 0px auto;
}
</style>
</head>
<body>
<?php include ("Header2.php"); ?>
<a href="barer.php" >Barer
<img src="img/barknap.jpg" alt="Barer" id="bar-knap">
</a>
<a href="spisesteder.php" >Spisesteder
<img src="img/spiseknap.png" alt="Spisesteder" id="spise-knap">
</a>
<!--Search form-->
<p style="text-align:center;"> Søg mellem barer </p>
<br />
<br />
<form method="post" action="search.php">
<input type="text" name="search" />
<input type="submit" name="submit" value=" Search ">
</form>
<br />
<br />
<footer>
Natalie
</footer>
</body>
</html>
And this is the search.php
<?php
//Connection to phpmyadmin
//Step1
$db = mysql_connect("host","username","password");
if (!$db) {
die("Database connection failed miserably: " . mysql_error());
}
//Step2
$db_select = mysql_select_db("databasename",$db);
if (!$db_select) {
die("Database selection also failed miserably: " . mysql_error());
}
$search = $_POST[search];
//SQL statement to select what to search
$sql="SELECT * FROM Brugerdatabase
WHERE 'Navn' like '%$search%' OR
'Mad genre' like '%$search%' OR
'Beliggenhed' like '%$search%'
ORDER BY Navn ASC";
// Run sql statement
$result = mysql_query($sql, $db) or die(mysql_error());
//Find out how many matches
$number= mysql_num_rows($result);
$pagetitle ="Search Results";
?>
<!doctype html>
<html lang="da">
<head>
<meta charset ="UTF-8">
<title>Søge resultater</title>
<link rel="stylesheet" type="text/css" href="style.css">
<style>
table {
border-collapse: collapse;
width: 80%;
margin:0 5%;
}
table th, td {
border-bottom:1px solid gray;
height: 50px;
vertical-align: center;
padding: 15px;
text-align: left;
}
tr:hover{background-color:#f5f5f5}
fieldset {
width: 60%;
margin:0 20%;
box-align: center;
}
</style>
</head>
<body>
<?php include ("Header2.php"); ?>
<!--Advanceret søge funktion-->
<p style="text-align:center;"> Søge resultater</p>
<?php
//------------------------------------
//This code inside the lines list the results when nothing is typed in the search field.
//Creates a loop to loop through results
while($row = mysql_fetch_array($result)){
echo "<table><tr><td>"
. $row['1'] .
"</td><td>"
. $row['4'] .
"</td><td>"
. $row['5'] .
"</td><td>"
. $row['6'] .
"</td><td>"
. $row['7'] .
"</td><td>"
. $row['8'] .
"</td></tr></table>";
}
//-----------------------------------
//-------------------------------
//This code inside the lines does not list anything at all..
// loop through results and get variables
while ($row=mysql_fetch_array($result)){
$navn =$row["Navn"];
$genre =$row["Mad genre"];
$beliggenhed =$row["Beliggenhed"];
}
// Tabel with search results
print " <tr>
<td>
<form method=\"post\" action=\"confirmdelete.php\">
<input type=\"hidden\" name=\"sel_record\" value=\"$id\">
<input type=\"submit\" name=\"delete\" value=\" Delete \">
<form>
<form method=\"post\" action=\"updateform.php\">
<input type=\"hidden\" name=\"sel_record\" value=\"$id\">
<input type=\"submit\" name=\"update\" value=\" Edit \">
</form>
</td>
<td><strong>$navn</strong><br />
Mad genre: $genre<br />
Beliggenhed: $beliggenhed</td>
</tr>";
print "</tr></table></body></html>";
//----------------------------
mysqli_close($db);
?>
</body>
</html>
You need to encase your array keys -in this example with single quotes- so:
$search = $_POST['search'];
You should also real_escape string your key with:
$search = mysql_real_escape_string($_POST[search]);
...As a minimum (read below)
Your search SQL you should not encase your column names, so rewrite them as:
$sql="SELECT * FROM Brugerdatabase
WHERE Navn like '%$search%' OR
`Mad genre` like '%$search%' OR
Beliggenhed like '%$search%'
ORDER BY Navn ASC";
You SHOULD encase the middle column within backticks (`) because the column name contains a space. quotes should only go around values, not column or table names.
Now, read again the line LIKE '%$search%' OR ... if $search is empty then this will return a string of <anyvalue><null><anyvalue> ==> %%, so this will return any of the columns that are not NULL because they contain something.
Your code detailed below does not output anything to the browser to display so you will never have anything to show for your query:
//This code inside the lines does not list anything at all..
// loop through results and get variables
while ($row=mysql_fetch_array($result)){
$navn =$row["Navn"];
$genre =$row["Mad genre"];
$beliggenhed =$row["Beliggenhed"];
}
Finally, you really, REALLY should be looking into using MySQLi rather than the standard MySQL as that has been Deprecated and removed from current/future versions of PHP. It's use is really not a good idea and it's riddled with flaws and holes.

Dropdown menu wont stick to table

I have a CSS related problem, that would look easy, but I have been slamming my head against the wall for the last hour, since the tweaks arround i tried are not applying.
For some odd reason, the select dropdown menu wont stick into the cell, but is placed above the table.See picture below
The loop code to generate the table is build up as followed: (i didnt bother posting the php cause thats not relevant)
echo '<tr class="items">';
echo '<td id="cell11">'.$Date.'</td>';
echo '<td id="cell21">'.$link.'</td>';
echo '<td id="Fright1">'.MakeUserRoleDropdown($StatusId).'</td>';
echo '</tr>';
The CSS is pretty basic:
select { width: 367px; font-family: Verdana; font-size: 12px; color: #666; background: white; border: 1px solid #CECECE; padding: 4px;}
.dropdownrole { width:150px;}
#cell11 { padding-left: 10px; padding-right: 10px; width: 200px; text-align:left;}
#cell21 { padding-left: 10px; padding-right: 10px; width: 200px; text-align:left;}
#Fright1 { padding-left: 10px; padding-right: 10px; width: 200px; text-align:right;}
table.items tr { height: 32px; background:url(../images/tableRow.png); border-top: 1px solid #CCC; padding: 0; margin: 0;}
.items { width: 100%; border-collapse: collapse; color: ##797979; font-family: Verdana; font-size: 11px; }
table { border-spacing:2px;}
dropdownrole is in this case te class applied to the dropdown menu.
As you see, the dropdown wont apply to the row and just is put above the table (even in the code it's not even in the cell) And I cant wrap my head arround why. I made cells already bigger in size (height and width) but no luck.
Did i do something wrong within the CSS?
EDIT
Per request of the dropdown function.
function MakeUserRoleDropdown($StatusId){
echo "<select name='StatusId' class='dropdownrole'>";
$results = LoadRoles();
while($row = mysql_fetch_array($results)){
echo "<option value='".$row['SgroupId']."' ";
if($StatusId == $row['SgroupId']){
echo "selected='selected'";
};
echo ">".$row['SgroupUser']."</option>";
}
echo "</select>";
}
And here is the output in HTML.
I Think the problem is, that you call MakeUserRoleDropdown which contains echos
you proceed to use this method in this string: echo '<td id="Fright1">'.MakeUserRoleDropdown($StatusId).'</td>';
Alter your MakeUserRoleDropdown to this:
function MakeUserRoleDropdown($StatusId){
$str = '';
$str .= "<select name='StatusId' class='dropdownrole'>";
$results = LoadRoles();
while($row = mysql_fetch_array($results)){
$str .= "<option value='".$row['SgroupId']."' ";
if($StatusId == $row['SgroupId']){
$str .= "selected='selected'";
};
$str .= ">".$row['SgroupUser']."</option>";
}
$str .="</select>";
return $str;
}
And you should be good.

Warning: Cannot modify header information - headers already sent by (output started at *****) in ***** on line ***** [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I am currently encountering a problem that keeps giving me the error on the question title. here is my code, can anyone help me check what is wrong and how to fix it? Thank you so much.
The following part is the bottom part of a php file including "Header":
echo'<table>';
while ($result = mysql_fetch_array($sql, MYSQL_ASSOC)){
echo '<tr>';
echo '<td style="width:142px">';
echo $result['instrument_id'];
echo '</td>';
echo '<td style="width:142px">';
echo $result['instrument'];
echo '</td>';
echo '<td style="width:142px">';
echo $result['type'];
echo '<br>';
echo $result['amount'].' shares';
echo '</td>';
echo '<td style="width:142px">';
echo $result['opening_rate'];
echo '<br>';
$fp = fopen ("http://finance.yahoo.com/d/quotes.csv?s='".$result['instrument']."'&f=nsb2b3p2ophg.csv","r");
//this uses the fgetcsv function to store the quote info in the array $data
$data = fgetcsv ($fp, 1000, ",");
echo ($data[3]);//current rate
echo '</td>';
echo '<td style="width:142px">';
echo $result['opening_value'];
echo '<br>';
$value = $data[3] * $result['amount'];
$currentvalue = number_format($value, 2, '.', '');
echo $currentvalue;//current value
echo '</td>';
echo '<td style="width:142px">';
echo $result['open_time'];
echo '</td>';
echo '<td style="width:142px">';
if($data[3] == 0){
echo '<input type="submit" name="close'.$result['instrument_id'].'" value="Close" disabled="disabled"/>';}
else{
echo '<input type="submit" name="close'.$result['instrument_id'].'" value="Close"/>';}
echo '</td>';
echo '</tr>';
fclose ($fp);
$name = 'close'.$result['instrument_id'];
$close = isset($_POST[$name]);
if($close){
$instrumentid = $result['instrument_id'];
$_SESSION['instrumentid'] = $instrumentid;
$instrument = $result['instrument'];
$_SESSION['instrument'] = $instrument;
$amount = $result['amount'];
$_SESSION['amount'] = $amount;
header("Location:order_sell.php");
}
}
echo'</table>';
When I include a file on the top, which is the background file, only including html elements, it will give me the error message like Warning: Cannot modify header information ...
here is the code for the included file:
<?php
session_start();
// checks if $_SESSION['userid'] exists
$areYouLoggedIn = isset($_SESSION['username']);
?>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
/* background*/
body { background: url(images/background2.png) center center fixed no-repeat;
webkit-background-size:cover;
moz-bacground-size:cover;
-o-background-size:cover;
background-size:cover;}
/* top menu layout*/
.Topmenu{
width:1000px;
background: #00AEEF;
border-radius: 3px 3px 3px 3px;
}
/* top menu cell layout*/
.Tabcells{ width:200px;
text-align:center;
}
/* top menu cell text layout*/
/* top menu cell text layout*/
.Tabtexteffect
{ text-decoration: none;
color:white;
font-family:Arial;
font-size:15px;
}
.Tabtexteffect:hover
{
text-decoration: none;
color:white;
font-family:Arial;
font-size:20px;
}
.Paragraph {text-align:justify;
text-justify:distributed-all-lines;
text-align-last:justify;
font-family:arial;
font-size:12pt;
width:1000px;
}
.Box {
width:950px;
text-align:left;
background: -webkit-linear-gradient(90deg, #F0E68C, #FFFFE0) repeat scroll 0 0 transparent;
background: -moz-linear-gradient(90deg, #F0E68C, #FFFFE0) repeat scroll 0 0 transparent;
border: 1px solid #AAAAAA;
border-radius: 10px 10px 10px 10px;
box-shadow: 0 0 15px #AAAAAA;
}
.Box2 {
width: 200px;
text-align: left;
background: -webkit-linear-gradient(90deg, #FFFA8C, #FFFFE0) repeat scroll 0 0 transparent;
background: -moz-linear-gradient(90deg, #FFFA8C, #FFFFE0) repeat scroll 0 0 transparent;
border: 1px solid #AAAAAA;
border-radius: 10px 10px 10px 10px;
box-shadow: 0 0 15px #AAAAAA;
}
.Button {
background: -moz-linear-gradient(90deg, #0459B7, #08ADFF) repeat scroll 0 0 transparent;
background: -webkit-linear-gradient(90deg, #0459B7, #08ADFF) repeat scroll 0 0 transparent;
border: 1px solid #093C75;
border-radius: 3px 3px 3px 3px;
box-shadow: 0 1px 0 #FFFFFF;
color: #FFFFFF;
cursor: pointer;
font-family: Arial,sans-serif;
font-size: 12px;
font-weight: bold;
margin-right: -16px;
margin-top: 16px;
padding: 5px 10px;
text-decoration: none;
text-shadow: 0 1px 1px #333333;
text-transform: uppercase;
}
</style>
</head>
<body>
<form action="HomePage.php" method="POST">
<div style="width:1000px; margin:0 auto; min-height:100%;">
<div>
<img style="width:25%;" src="images/Mock Apprentice.png" alt="Mock Apprentice"></img>
<?php
if($areYouLoggedIn==false){
echo'<table cellpadding="0" align="right" style="position: absolute; left: 940px; top: 15px;">';
echo'<tr>';
echo'<td width="130" align="center"><font face="Arial" size="3"><input type="button" class="Button" value="Sign In""/></font></td>';
echo'<td width="1" bgcolor=black><BR></td>';
echo'<td width="100" align="center"><font face="Arial" size="3"><input type="button" class="Button" value="Sign Up""/></font></td>';
echo'</tr>';
echo'</table>';
}
if($areYouLoggedIn == true){
echo'<table cellpadding="0" align="right" style="position: absolute; left: 950px; top: 40px;">';
echo'<tr>';
echo'<td width="130" align="center"><font face="Arial" size="4" color="#00AEFF">Welcome';
echo'&nbsp';
echo'<font color="#FFD700">';
echo($_SESSION['username']);
echo'</font>';
echo'</font></td>';
echo'<td width="1" bgcolor=black><BR></td>';
echo'<td width="100" align="center"><font face="Arial" size="4"><a style="text-decoration: none; color:#00AEFF;" href="Logout.php">Logout</a></font></td>';
echo'</tr>';
echo'<table>';
}
?>
<HR color="#00AEEF">
</div>
<div>
<table width="1020" height="30" class="Topmenu">
<tr>
<td width="200" height="30" class="Tabcells">Intro</td>
<td width="200" class="Tabcells">How to use</td>
<td width="200" class="Tabcells">Education Center</td>
<td width="200" class="Tabcells">Trading Platform</td>
<td width="196" class="Tabcells">Contact Us</td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
Thank you so so so much!
Try replacing header("Location:order_sell.php"); with
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=order_sell.php">';
exit;
Whatever you output - being HTML only or output from PHP: Unless you do some tricking with the ob_*() functions you lose the ability to send headers the moment you output anything.
This is why they are called headers: In the HTTP protocol they preceed every single piece of payload body - including whitespace such as empty lines before a <?php tag.
The typical way to tackle this would be to regroup your logic as to first calculate what headers you need, then create your output, via echo(), printf(), include() or whatever.
In PHP header functions must be called before any output is sent from the server. This includes echo calls as well as HTML info before the <?php ?> block.
You can't send a header after any text has been sent to the browser:
header("Location:order_sell.php");
Needs to be before ANY text/output/whitespace is sent/echo'ed to the browser.
When using header("Location:someLocation") be sure that you don't have any character outputted to the browser, otherwise you'll get the error Cannot modify header information. More on this at: http://php.net/manual/en/function.header.php

How to use jquery to perform actions on PHP loop results?

I've searched and can't seem to make sense of the answers I've found. Grateful for any help!!
Goal: Reveal selected message detail in section#details below the listed message headers in section#info.
Problem:
The following code alerts a result but won't fadeIn();, (or show();, or ...anything).
The following code is only grabbing the value of the last result in the PHP while loop.
php/html/jquery/javascript:
<section id="info">
<?php
$user = $session->username;
$q = sprintf("SELECT * FROM mail WHERE UserTo = '%s' ORDER BY SentDate DESC",
mysql_real_escape_string($user));
$getMail = mysql_query($q, $link) or die(mysql_error());
if(mysql_num_rows($getMail) == 0) {
echo "<p>you have no mail</p>";
}
else {
?>
<form id="inbox" class="mail">
<fieldset>
<ul>
<li style="border: 2px solid purple; width: 100%;">
<span style="display: inline-block; border: 1px solid black; width: 8%; margin-left: 13%;">Status</span>
<span style="display: inline-block; border: 1px solid black; width: 15%;">From</span>
<span style="display: inline-block; border: 1px solid black; width: 45%;">Subject</span>
<span style="display: inline-block; border: 1px solid black; width: 16%;">Time</span>
</li>
<?php
while($mail = mysql_fetch_object($getMail)) {
$status = $mail->status;
$mailId = $mail->mail_id;
$from = $mail->UserFrom;
$subject = $mail->Subject;
$received = $mail->SentDate;
$theMessage = $mail->Message;
?>
<li class="outerDiv" style="border: 2px dotted purple;">
<button style="display: inline;" class="viewButton">View</button>
<button style="display: inline;">Delete</button>
<?php
echo "<span style='border: 1px solid red;'>" . $mail_id . "</span>";
echo "<span style='display: inline-block; width: 8%; border: 1px solid red;'>" . $status . "</span>";
echo "<span style='display: inline-block; width: 15%; border: 1px solid red;'>" . $from . "</span>";
echo "<span style='display: inline-block; width: 45%; border: 1px solid red;'>" . $subject . "</span>";
echo "<span style='display: inline-block; font-size: x-small; width: 17%; border: 1px solid red;'>" . $received . "</span>";
?>
</li>
<?php }
} ?>
</ul>
</fieldset>
</form>
</section>
<section id="details">
<div class="theMessage" style="display: none;"><?php echo $theMessage; ?></div>
<script type="text/javascript">
$(document).ready(function() {
$(".outerDiv").click(function(e) {
if($(e.target).is(".viewButton")) {
alert($(document).find(".theMessage").text()); //this works
$(document).find(".theMessage").text().fadeIn(1000); //this doesn't work
var theMessage = $(document).find(".theMessage").text();
theMessage.fadeIn(1000); //this doesn't work
}
});
return false; (sometimes prevents default..sometimes not?
});
</script>
</section>
p.s. the crazy colors and borders are/were for temp layout purposes. also, the delete button will obviously have functionality... once I can figure this out.
Instead of
$(document).find(".theMessage").text().fadeIn(1000);
use
$('.theMessage').fadeIn(1000);
Starx is correct, but I figured I'll give an explanation as well.
$('.theMessage').fadeIn(1000);
In case you don't understand why, take a look at http://api.jquery.com/text/ . The text() method only returns a string, not an actual HTML element that you can manipulate (in this case fadeIn). So text() is good to get or set the contents of an element, but to animate you need to call the methods directly on the $('.theMessage') element.

Categories