I have three drop down lists and I need to retain selected item in ddl after form is submitted
e.g. When I click 09 in ddl and submit form then it should display 09 in ddl. Pasting my code for your reference.
echo"<option> Select Day</option>";
for ($i = 1; $i <= 31; $i++)
{
if($i<10){
echo "<option>".str_pad($i,2,"0",STR_PAD_LEFT)."</option>";
}
else{
echo "<option>".$i."</option>";
}
}
?>
I don't know what your DDL name attribute is, so I'll pretend
<select name="ddl">
Here's what you code should look like:
echo"<option> Select Day</option>";
for ($i = 1; $i <= 31; $i++)
{
if(isset($_POST['ddl']) && $_POST['ddl'] == $i)
$select = " checked='checked'";
else
$select = "";
if($i<10){
echo "<option " . $select . ">".str_pad($i,2,"0",STR_PAD_LEFT)."</option>";
}
else{
echo "<option " . $select . ">".$i."</option>";
}
}
?>
This is a base example, you should take measure to protect your code against injection.
Related
I am using Linux, Apache, PHP and Postgres for my web application program. I have a web application that returns the content of SQL tables. The user has to log in and type in a valid SQL table name. How can I create a button for each row that is returned and displayed in the web UI? It has to be dynamic because the rows returned vary depending on the table the user enters.
Ultimately I'd like to have the user update the row in the SQL database through the web UI. For now I want a button to appear for each row that is returned. If the button or link was available for every cell (regardless of whether the cell was empty or not), that would work. The rows returned are currently in static alphanumeric text. I'd like there to be a hyperlink or button for each row. I can then have the user go to a new .php web page from that link or button. This page will alow for an update to the SQL table.
How do I create a button or hyperlink in a web page for each SQL row returned and displayed in a PHP web page?
The code below leaves out the authentication web page. Here is my code for the web page that is repeatedly called as it allows users to type in a table name and view it:
<?php
session_start();
$pa = $_POST["table"];
$host = $_SESSION['hostv'];
$port = $_SESSION['portv'];
$dbname = $_SESSION['dbv'];
$credentials = $_SESSION['cv'];
$comp = $credentials;
$db = pg_connect( "$host $port $dbname $credentials" );
$query = 'select * from ' . $pa . ';';
$cc = pg_query($query);
showingfunc($cc);
function showingfunc($cc)
{
/* This code below was copied and modified from razorsql.com */
$i = 0;
echo '<html><body><table><tr>';
if ($cc) {
}
else {
echo '<td>' . "Failure." . $dbname . '</td>';
}
while ($i < pg_num_fields($cc))
{
$fieldName = pg_field_name($cc, $i);
echo '<td>' . $fieldName . '</td>';
$i = $i + 1;
}
echo '</tr>';
$i = 0;
while ($row = pg_fetch_row($cc))
{
echo '<tr>';
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
echo '</tr>';
$i = $i + 1;
}
pg_free_result($cc);
echo '</table></body></html>';
}
?>
<html>
<body>
<form action="repeater.php" method="post">
TableToView: <input type="text" name="table"<br>
<input type="submit">
</form>
Logout
</body>
</html>
Simply add a cell containing your action button.
while ($row = pg_fetch_row($cc))
{
echo '<tr>';
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
//New Code on the line below
echo '<td><a class="edit" href="[YOURLINK]">Edit</a></td>' //You Probably want to add an ID to your query string here
echo '</tr>';
$i = $i + 1;
}
Also, add an empty cell in your header in a similar fashion.
you just need to add one more td tag in your code...
we just passed id of your record with href of a tag.
we just replace your code with easy way
while ($y < $count)
{
$c_row = current($row);
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
replace with :-
while ($y < $count)
{
$c_row = current($row);
echo '<td>' . $c_row . '</td>';
echo '<td>Update</td>';
next($row);
$y = $y + 1;
}
and you will get this id on another page by doing:-
$id=$_REQUEST['cid'];
and pass it to your query so you can get all information which this will have in table.
Please let me know if any concern or if not working... Thank You
I am attempting to populate 0 to many drop downs with the same results from a database table depending on a previous selection. it is working fine when 0 and 1 are selected but not when i am attempting to insert the result set into subsequent select elements. i am assuming it is a problem with the $row array position.
$homePlayers = "SELECT first_name, last_name, player_id FROM players WHERE team_name LIKE '$homeTeam%'";
$homePlayersQuery = mysqli_query($dbc, $homePlayers);
if (!$homePlayersQuery) {
echo 'err';
} else {
for ($i = 1; $i <= $homeTeamScore; $i++) {
echo "<select name='select-home-scorer-$i'>";
while ($row = mysqli_fetch_array($homePlayersQuery)) {
echo current($row);
echo "<option value='" . $row['player_id'] . "'>" . $row['first_name'] . " " . $row['last_name'] . "</option>";
}
echo "<option value='og'>Own Goal</option></select><br/>";
}
}
}
The problem you experience is that mysqli_fetch_array() reads data of the result set once. When you have read the last record, subsequent calls to mysqli_fetch_array() will return false.
You have 2 choices.
Firstly, you can read the records into an array and repeatedly parse the array.
while ($row = mysqli_fetch_array($homePlayersQuery)) {
$resultsArray[] = $row;
}
for ($i = 1; $i <= $homeTeamScore; $i++) {
foreach($resultsArray as $resultItem) {
do_something_here();
}
}
}
As a second option, you could rewind the pointer to the mysql result set so that you can start reading the results again.
for ($i = 1; $i <= $homeTeamScore; $i++) {
// Rewind the pointer to the start of the results
mysqli_data_seek($result, 0);
while ($row = mysqli_fetch_array($homePlayersQuery)) {
$resultsArray[] = $row;
}
}
hope someone can help me with my problem.
Im generating a Form with a dynamic amount of Dropdown/
while($length<count($spalten)) {
echo '<TD>
<SELECT ID="col['.$length.']" name="col['.$length.']">';
$selected = 0;
echo $length;
foreach( $spalten AS $value){
if($selected != $length){
echo '<OPTION value='.$value.'>'.$value.'</OPTION>';
}
else{
echo '<OPTION SELECTED value='.$value.'>'.$value.'</OPTION>';
}
$selected++;
}
echo ' </SELECT></TD>';
$length++;
} ...
And than i try to get the values for them:
for( $i = 0; $i < $cols; $i++){
$insert = $insert.', $col'.$i.'='.$r[$i].' ' ;
}
but $col'.$$i.' does not work of course, but i dont know how to handle that? how can i get the value of "$col0" and get it by $col$i???
hope someone knows
Thx so far
// EDIT
yeah sorry
$insert = is sopposed to be a MySQL Insert Query
$q_fillreport = new DB_QS;
$q_fillreport->query("INSERT INTO $typname
SET reportid = $Id
$insert;");
}
and $col1 -> $colX are $_POST values from the
<SELECT ID="col['.$length.']" name="col['.$length.']">';
I am trying to make a function in PHP which writes out a table, and looks in the database to find what cells should have info. the grid will always be the same size, but the content may be in different places.
I've gotten it to be able to look in the database, though it seems to only highlight the first cell, rather than the correct coordinates.
require("sql.php");
$sql = <<<SQL
SELECT *
FROM `maps`
WHERE `objpresent` = 1
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
} // ran the query
$xobj = array();
$yobj = array();
while($row = $result->fetch_assoc()){
//echo $row['x'] . $row['y'] . $row['object'] . '<br />';
$xobj[] += $row['x'];
$yobj[] += $row['y'];
}// get the rows
//find whether the row is obstructed
for($a=0; $a<=20-1; $a++) //rows (y)
{
for($i=0; $i<=25-1; $i++) //cols (x)
{
echo "<td>"; //between these write the apt content
// if (empty($xobj[$i]) || empty($yobj[$a]) ){
// echo '0';
//} //detect whether there is even a record for this space
if(!empty($xobj[$i]))
{
if(!empty($yobj[$a]))
{
echo $xobj[$i]; //debug
if($xobj[$i] == $i)
{
//echo $xobj[$i];
echo "A";
}
}
}
//echo "<td><img src='emptysym.png'></img></td>";
echo "</td>"; //add textual descriptions for now, add icons later
}
echo "</tr>";
}
this is my current(though rather messy) code.
if there is a row with the column x saying 2, and the column y saying 3, then it should put a letter at (2,3.
is it possible to fix this, or is there a better method for this?
Use a 2-dimensional array whose indexes are the x and y values from the database:
$xyobj = array();
while($row = $result->fetch_assoc()){
$xyobj[$row['x']][$row['y']] = true;
}
Then your output loop should be:
for ($y = 0; $y < 20; $y++) {
echo '<tr>';
for ($x = 0; $x < 25; $x++) {
echo '<td>';
if (isset($xyobj[$x][$y])) {
echo 'A';
}
echo '</td>';
}
echo '</tr>';
}
i need a help
in php i have a select box like this
<?php
$perpage = 5;
$total = 128;
$num = ceil( $total / $perpage );
$i = 1;
echo "<FORM NAME=\"form1\">";
echo "Select <SELECT NAME=\"select\" onChange=\"goto(this.form)\" SIZE=\"1\" >";
echo "<OPTION VALUE=\"\">----Select Page----";
for($i = 1; $i <= $num; $i++)
{
$sel = $i;
$goto = ($i - 1) * $perpage;
if($goto == 0) { $goto = ''; }
echo "<OPTION VALUE=\"http://localhost/CI_doctrine/blog/filmnews/" . $goto . "\">" . $i . "";
}
echo "</SELECT> Page";
echo "</FORM>";
?>
javascript code is here
function goto(form) {
var index = form.select.selectedIndex;
if (form.select.options[index].value != "0") {
window.location = form.select.options[index].value;
form.select.selected = form.select.options[index].value;
}
}
the code is working fine but i want to change the selected option to set the selected number after the page redirection but here iam getting the "select page" as the selected option
any help appreciated.
thank you from your friend.
Once you redirect, that page is unloaded and a new page loaded (even if it has the same items). When the new page loads, you want to do:
window.onload = function() {
var i, options = form.select.options, curUrl = window.location.href;
for (i = 0; i < options.length; i++) {
if (options[i].value == curUrl) {
form.select.selectedIndex= i;
break;
}
}
}
This will select the current URL. Make sure the URLs in the select options are full URLs (including http://). Also, window.onload is the DOM1 way. You should probably use a real library to deal with this.
Alternatively, you can also select the right input in PHP using the same basic approach.