I have a problem regarding while loop. I have a option tag and hidden value inside the loop, the option tag work correctly for the dropdown list but the hidden value is not match with the selected dropdown list.
This is my code :
<?php
session_start();
require 'config.php';
$option1 = '';
$idseason='';
$season = '';
$query1 = "select id,description from codemaster_local where codeclass = 'season' and description like '%1st%' group by description";
$result1 = mysqli_query($doa,$query1);
while($row1 = mysqli_fetch_array($result1)) {
$option1 .= "<option value='".$row1['description']."'>".$row1['description']."</option>";
$hidden = "<input type=hidden name=id value='".$row1['id']."'>";
}
if(isset($_POST['submit'])) {
$season = $_POST['season'];
$idseason = $_POST['id'];
echo "Season :"; echo $season;echo "<br>";
echo "ID : ";echo $idseason;
}
?>
<html>
<head>
<link href="style/style.css" rel="stylesheet" type="text/css">
<link href="style/sty.css" rel="stylesheet" type="text/css">
<link href="style/dropdown.css" rel="stylesheet" type="text/css">
</head>
<body>
<form class="form-style-9" action="" method="post">
<table>
<tr><td><b>Season:</b></td>
<td> <select name="season" class="select-css">
<?php echo $option1; ?>
</select>
<?php echo $hidden; ?>
</td>
</tr>
</table><br>
<input type="submit" value="Search" name="submit">
</form>
</body>
</html>
For example, the id for 1st season 2005 should be 24800 but instead it choose the last id 30539. I select any of the season also still choose the last id. Is there any way to fix this?
$hidden is being overwritten upon every loop within your while. Resulting in one hidden field with the name: id, containing the value of the last row id retrieved. That is the value you receive upon submission.
while($row1 = mysqli_fetch_array($result1)) {
$option1 .= "<option value='".$row1['description']."'>".$row1['description']."</option>";
$hidden = "<input type=hidden name=id value='".$row1['id']."'>";
}
I suggest you change the option's value attribute to $row1['id']:
while($row1 = mysqli_fetch_array($result1)) {
$option1 .= "<option value='".$row1['id']."'>".$row1['description']."</option>";
}
This way, $_POST['season'] will then hold the corresponding id for the season selected.
Try putting the id in the option tag instead of a hidden tag:
while($row1 = mysqli_fetch_array($result1)) {
$option1 .= "<option value='".$row1['description']."' id='".$row1['id']."'>".$row1['description']."</option>";
}
Related
I did a form which adds record to database and it shows in HTML table.
One of the column in form and database is "country". I would like to insert a right flag icon beside the "country" column and I'd like to make it from form. I added example photo
Could you help me with any idea?
Form
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style2.css">
</head>
<body>
<?php
if (isset($_GET["date"]) ){
$date = $_GET["date"];
$country = $_GET["country"];
$city = $_GET["city"];
$place = $_GET["place"];
if( empty ( $date ) || empty ($country) || empty ($city) || empty ($place)){
echo "Wypelnij wszystkie pola";
} else {
$conn = new mysqli("localhost", "root", "", "tour");
$odp = $conn->query("INSERT INTO trasy (date, country, city, place)
VALUES ('$date', '$country', '$city', '$place')");
header("Location: index5.php");
if ($odp) {
echo "Dodano koncert";
}else{
echo "Nie udalo sie dodac";
}
$conn->close();
}
}
?>
<div id="login">
<form method="GET" action="index5.php">
<table id="customers">
<input name="date" type="date" placeholder="Date..."><br>
<input name="country" type="text" placeholder="Country..."><br>
<input name="city" type="text" placeholder="City..."><br>
<input name="place" type="text" placeholder="Place..."><br>
<input type="submit" value="OK">
</table>
</form>
</div>
</body>
</html>
HTML table
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style3.css">
</head>
<body>
<table id="customers">
<tr>
<th>Date</th>
<th>Country</th>
<th>City</th>
<th>Place</th>
<th>Info</th>
</tr>
<?php
$conn = new mysqli("localhost", "root", "", "tour")
or die ("Błąd");
$wynik = $conn->query("SELECT * FROM trasy ORDER BY date DESC");
if ($wynik->num_rows > 0){
while ( $wiersz = $wynik->fetch_assoc() ){
echo "<tr>";
echo "<td>" . $wiersz["date"] . "</td>";
echo "<td>" . $wiersz["country"] . "</td>";
echo "<td>" . $wiersz["city"] . "</td>";
echo "<td>" . $wiersz["place"] . "</td>";
echo "<td>" . $wiersz["info"] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "Nic tu nie ma";
}
$conn->close();
?>
</table>
</body>
</html>
I will address only your question but keep in mind you have all sort of wrong things with your code starting with security.
I will just point you in direction what I think is best for you to go.
First of all upload all flags in one directory on server. You can use img or you can use CSS svg like this one for example.
Second, don't make people type there country in input field at all (that way you make sure values for same country will be the same), make that a drop-down with pre-populated all countries and assign a value to each.
Example of value for CSS icons that I provided link for:
If user chooses Germany from drop-down you store in country field value: gr.
That way when you call your country you can do that like this:
echo '<td><span class="flag-icon flag-icon-' . $wiersz["country"] . '"></span></td>';
Which will produce for Germany:
<td><span class="flag-icon flag-icon-gr"></span></td>
You can do same thing just with HTML img tags. Just storing the flag value as img src.
I believe this is best way to go. Other that that you would need to have a whole lot of PHP switch statements.
EDIT:
Additionally if you want to have country name and flag store both of them as value in select and on database input separate it and store it in two columns like this:
<select id="country">
<option value="Croatia | hr">Croatia</option>
<option value="Germany | gr">Germany</option>
<select>
$country = mysqli_real_escape_string($_GET['country']);
$pieces = explode(" | ", $country);
$countryname = $pieces[0]; // piece1
$flag = $pieces[1]; // piece2
update
Can anyone explain to me why I am getting duplicate messages instead of one?
how can I change my code so that when I type a comment and press "Comment" button, it will only display one message instead of duplicates! When I have one comment boxes it doesn't show duplicate comments, but if I have more than one then it starts duplicating!
COMMENT.INC.PHP
include 'cdbh.inc.php';
function setComments($con)
{
if (isset($_POST['commentSubmit'])) {
$uid = mysqli_real_escape_string($con,$_POST['uid']);
$date = mysqli_real_escape_string($con,$_POST['date']);
$message = mysqli_real_escape_string($con,$_POST['message']);
$sql = "INSERT INTO comments (uid, date, message) VALUES ('$uid','$date','$message')";
$result = mysqli_query($con,$sql);
}
}
function getComments($con)
{
$sql = "SELECT * FROM comments";
$result = mysqli_query($con,$sql);
while ($row=mysqli_fetch_assoc($result)) {
echo $row['uid'];
echo ":";
echo $row['message']."<br><br>";
}
}
page code
<?php
date_default_timezone_set('America/Los_Angeles');
include 'comment.inc.php';
include("connection.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="comment.css" rel ="stylesheet">
</head>
<body>
<?php
$sql="Select * from tbl_images";
$result=mysqli_query($connection,$sql);
while ($row=mysqli_fetch_array($result)) {
?>
<img src="images/<?php echo $row['images_name'] ?>" width="200px" height="200px">
<?php
echo "<form method ='POST' action ='".setComments($con)."'>
<input type ='hidden' name ='uid' value='unknown'>
<input type ='hidden' name ='date' value='".date('Y-m-d H:i:s')."'>
<textarea name='message'></textarea>
<button type ='submit' name='commentSubmit'>Comment</button>
</form>";
}
getComments($con);
?>
</body>
</html>
Maybe you are submiting all your forms instead of one..
check your database in order to know from what img comes each message.
If you have other code like javascript, you should post it.
I am trying to build a simple form which queries a database, grabs a list of email addresses and then creates a table based on the results. What I would like it to do is retain the checked boxes after a submission but am having trouble figuring it out based on the way I've created my table. I can do it no problem if I manually build the table but that defeats the purpose. Here is the code I am working with, again the only change I would like it to do is retain the checked boxes.
<html>
<head>
<title>Test</title>
<link rel="stylesheet" type="text/css" href="style/style.css"/>
</head>
<body>
<?php include('include/connect.php'); ?>
<h1>This is a test</h1>
<div class="emailform">
<form action="" method="post">
<table id="emails">
<?php
while($row = $result->fetch_assoc()) {
unset($email);
$email = $row['Email'];
?>
<tr><td><input type="checkbox" name="select[]" value="<?php echo $email;?>"/><?php echo $email; ?></td></tr>
<?php
}
?>
</table>
<br/><br/>
<input id="manual" type="text" name="select[]"><br/><br/><br/>
<button type="submit" name="SubmitButton">Select Email Addresses</button>
</form>
</div>
<?php
if(isset($_POST['SubmitButton'])){
if(isset($_POST['select'])){
$shift = $_POST['select'];
if (count($shift) > 1 ){
$list = implode(", ", $shift);
echo $list;
} else {
echo "$shift[0] <br/>";
}
}
}
?>
</body>
</html>
Help would be appreciated, thanks
Just check if the current email in the loop exists in $_POST['select'], if it is, you check it, if it is not, clear the check. This check will be displayed in the input checkbox as <?php echo $checked;?> :
<?php
while($row = $result->fetch_assoc()) {
unset($email);
$email = $row['Email'];
// IF EMAIL EXISTS IN $_POST, CHECK IT.
$checked = "";
if(isset($_POST['select'])){
$shift = $_POST['select'];
$list = implode(", ", $shift);
if (strpos($list,$email)===false)
$checked = ""; // EMAIL NOT IN $_POST.
else $checked = "checked"; // EMAIL IS IN $_POST.
}
?>
<tr><td><input type="checkbox" name="select[]" <?php echo $checked;?>
value="<?php echo $email;?>"/><?php echo $email; ?></td></tr>
<?php
}
?>
Check if $row['Email'] isn't empty, then output "checked" attribute.
<?php
while($row = $result->fetch_assoc()) {
unset($email);
$email = $row['Email'];
?>
<tr><td><input type="checkbox" name="select[]" value="<?php echo $email;?>"<?php if($row['Email'] != false) { echo ' checked'; } ?>><?php echo $email; ?></td></tr>
<?php
}
?>
i have a website i am constructing for a school project, in which i already have various webpages where i get tabular data from my database through the use of While and Foreach.
But on this page in question, i am attempting to retrieve data from various tables, in order for a user to choose a category, and a text and submit.
But for some reason it isn't outputting any data. If i attempt with only one table and without using the table.field method, and only typing the fields, it works.
But from what i know, to retrieve from various tables i have to do so.
Can annione help me out on this?
<html>
<head>
<script type="text/javascript" >
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
mysql_connect("localhost","root","") or die("problema na conexao");
mysql_select_db("trabalho1");
$query = "SELECT texto.titulo,texto.ID,categoria.categoria,categoria.id FROM categoria,texto";
$results = mysql_query($query) or die(mysql_error());
echo"<center>";
echo "<table border='2'>\n";
echo "<form id='formulario' name='post' method='post' onsubmit='return validar(this) action='../inserir/inseretexto.php>'";
echo "<button type='submit'>Submeter</button>";
echo "<tr><td colspan ='2'>Historico de Newsletters</td><td colspan='2'>Enviar Newsletter</td></tr>";
echo "<tr><td colspan='2'>Texto </td><td colspan='2'>Categoria</td></tr>";
while ($row = mysql_fetch_assoc($results)) {
foreach ($row as $campo=>$valor) {
if ($campo == "texto.titulo") {
echo "<tr><td>'".$valor."'</td>";
}
if ($campo == "texto.ID") {
echo "<td><input type='radio' name='nome' value='".$valor."'></td></tr>";
}
if ($campo == "categoria.categoria") {
echo "<td>'".$valor."'</td>";
}
if ($campo=="categoria.id") {
echo "<td><input type='radio' name='nome' value='".$valor."'></td></tr>";
}
}
}
echo "</form>";
echo "</table>";
echo "</center>";
?>
</body>
</html>
Added: Since both tables have a field called id, it won't let me simply put the field names, i have to also put the table name like i did. And yes, i have verified and both tables are populated with data, they work fine on other pages.
This is what I suggest (I didn't consider much code optimization).
<?php
$data = array();
mysql_connect('localhost', 'root', '') or die('problema na conexao');
mysql_select_db('trabalho1');
// Use the same field-names/aliases: id, info
$query = 'SELECT ID AS id, titulo AS info FROM texto';
$results = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($results)) {
$data[] = $row;
}
// Use the same field-names/aliases: id, info
$query = 'SELECT id, categoria AS info FROM categoria';
$results = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($results)) {
$data[] = $row;
}
?>
<html>
<head>
<script type="text/javascript">
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<center>
<table border='2'>
<form id='formulario' name='post' method='post' onsubmit='return validar(this) action='../inserir/inseretexto.php>
<tr><td colspan ='2'><button type='submit'>Submeter</button></td></tr>
<tr><td colspan ='2'>Historico de Newsletters</td><td colspan='2'>Enviar Newsletter</td></tr>
<tr><td colspan='2'>Texto </td><td colspan='2'>Categoria</td></tr>
<?php
foreach ($data as $row) {
echo '<td><input type="radio" name="nome" value="' . $row['id'] . '></td></tr>' .
'<tr><td>' . $row['id'] . '</td>';
}
?>
</form>
</table>
</center>
</body>
</html>
PS: Read about my response about double quotations. Should I use curly brackets or concatenate variables within strings?
I am stuck on the dropdown box value calculation. anyone can help me on this problem ?
I have two dropdown boxes on my webpage, and the values that in the dropdown boxes are retrived from the mysql database.
Dropdown box A displays two different values, which are Chassise_Name and Chassise_price, and The dropdown box B displays the quantity of the item.
Dropdown box "A" Dropdown box "B"
SuperServer 7036A-T(Black) $90 3
Superserver 7036A-T(Black) is retrieved from the Chassis_Name;
$90 is retrieved from the chassis_Price;
3 is retrieved from the Quantity in other table.
Question:
How can I take only the price on the dropdown box A multiply the number in the dropdown box B?
Here is my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head><title></title></head>
<body>
<form action="test2.php" method="post">
<?php
mysql_connect("host", "users", "pass") or die(mysql_error());
mysql_select_db("a4202648_wang") or die(mysql_error());
echo "<table border='1'>";
echo "<tr><td>";
$result = mysql_query("SELECT Chassis_Name, Chassis_Price FROM Chassis where Chassis_Form_Factor='ATX'")
or die(mysql_error());
echo '<select name="Chassis" onChange="change()">';
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo '<option Name="Chassis">',$row['Chassis_Name'],' ',$row['Chassis_Price'],' ','</option>';
}
echo '</select>';
echo "</td>";
echo "<td>";
$result= mysql_query("SELECT Number FROM Quantity")
or die(mysql_error());
echo '<select name="Quantity" Value="Quantity" onChange="change()" >';
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result)) {
// Print out the contents of each row into a table
echo '<option Name="Quantity">',$row['Number'],'</option>';
}
echo '</select>';
echo "</td></tr>";
echo "</table>";
?>
<input type="submit" />
</form>
</body>
</html>
I reduced the code a bit for readability but you'll want to add id="chassis" and id="quantity" so that you can reference that dom object when doing your summation.
Be sure to put the <script> in the head of your document.
<script type="text/javascript">
$('document').ready( function() {
function change() {
var quantity = $('#quantity').val();
var chassis = $('#chassis').val();
var sum = quantity * chassis;
$('#the_sum').val(sum);
}
});
</script>
<form action="test2.php" method="post">
<?php
echo '<select id="chassis" name="Chassis" onChange="change()">';
echo '<option value="{$row['Chassis_Name']}">{$row['Chassis_Name']} {$row['Chassis_Price']} </option>';
echo '<select id="quantity" name="Quantity" Value="Quantity" onChange="change()" >';
echo '<option value="{$row['Number']}">{$row['Number']}</option>';
?>
<input type="text" value="" id="the_sum" />
<input type="submit" />
</form>
</body>
</html>