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"];
}
}
Related
I'm displayed a dropdown list of initials that come from a table. I insert "--" at the start before getting the initials from the table.
It goes:
AB
CD
EF
I want it to select the default. $init is the defaulkt
The code below works for '--' but not for the initials.
On the While, if I remove the if statement and just leave the 'else'
echo "<option value='" . $row['id'] . "'>" . $row['init'] . " ";
it is OK.
The first half of the if statement should be the same but with 'selected added.
I don't understand why it is not working.
All I get is "--", not the initials, or any other following input.
Thanks for your help.
<?php
echo $init;
// Drop down list initials
$conn=createconnection();
$sql = "SELECT * FROM employees WHERE lef='0' AND man='1' ORDER BY init";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "Updated by and date<br>";
echo "<select name='select_initial' id='select_initial'>";
if ($init==="--"){
echo "<option value='" . '0' . "'selected>" .'--' . " </option>";
} else {
echo "<option value='" . '0' . "'>" . '--' . " </option>";
}
// output data of each row
while($row = $result->fetch_assoc()) {
if ($init===$row('init')) {
echo "<option value='" . $row['id'] . "'selected>" . $row['init'] . " </option>";
} else {
echo "<option value='" . $row['id'] . "'>" . $row['init'] . " </option>";
}
}
echo "</select>";
//echo "<br><br>";
}
$conn->close();
?>
I found if I used square brackets round the $row function it worked:
if ($init===$row['init']) {
I've a very simple database, a list of 8 languages and the values they can have is Y or N.
What I need is to output a checkbox for each languages and, if the value is Y, the checkbox must be checked, otherwise it must be empty.
This is the code I'm using for each single language, but I would like to know if there's a better way to obtain the same result.
if ($language=='N'){
echo "<input type='checkbox' name='" . $rowlang["english"] . "' value='" . $rowlang["english"] . "'> English<br>";
} else {
echo "<input type='checkbox' name='" . $rowlang["english"] . "' value='" . $rowlang["english"] . " checked'> English<br>";}
//What I've tried to do is to build an array of the languages and use a foreach
$languages = array($rowlang["czech"],$rowlang["english"],$rowlang["german"],$rowlang["slovak"],$rowlang["russian"],$rowlang["french"],$rowlang["spanish"],$rowlang["italian"]);
foreach($languages as $language)
if ($language=='N'){
echo "<input type='checkbox' name='" . $language . "' value='" . $language . "'> " . $rowlang . "<br>";
}else{
echo "<input type='checkbox' name='" . $language . "' value='" . $language . "' checked> " . $rowlang . "<br>";
}
which is almost working, the problem is that I can't echo the single language, because with this code I'm getting "array".
The problem is that trying to use $rowlang as part of the echo is a problem as it is an array.
Instead you can create an array of the languages you want to output, this also gives the name to be displayed as the value. So use a foreach() over this array and check in the $rowlang array to see if it is set. Rather than repeat the whole HTML, this just sets the checked attribute.
// Need to expand this array for all the countries you need
$languages = array("german" => "Germany","english" => "English");
foreach($languages as $language => $label) {
if ($rowlang[$language]=='N'){
$checked = '';
}else{
$checked = ' checked';
}
echo "<input type='checkbox' name='" . $language . "' value='" . $language . "'$checked>" . $label . "<br>";
}
This will output something like
<input type='checkbox' name='german' value='german'>Germany<br>
<input type='checkbox' name='english' value='english' checked>English<br>
First create an associative array and store the languages and their values in it:
$languages = array('czech' => 'N','english' => 'Y','german' => 'N','slovak' => 'N','russian' => 'N','french' => 'N','spanish' => 'Y','italian' => 'N' );
then:
foreach($languages as $key => $value) {
if ($value=='N'){
echo "<input type='checkbox' name='" . $key . "' value='" . $value . "'> " . $key . "<br>";
}
else{
echo "<input type='checkbox' name='" . $key . "' value='" . $value . "' checked> " . $key . "<br>";
}
}
the result will be:
if it is array you should use $language->language_name or $langage['language_name'] as of your array type
Without wasting time on a long intro, I have a major issue: I am currently stuck at one particular place in my project where a user can search for a job by entering keywords. After doing so, user will see matching jobs posted with an option to apply by clicking a button that is next to that job.
Here is the problem: how do I make each button correspond to THAT particular job and post the applicants details to an appropriate row in the database, say, where Job_ID = xyz?
This was the approach that I tried - assigning a unique Job_ID to a button based on the job that the button is linked to (making that the "name" of the button), but I have no idea how to pass that on and actually use it to achieve a result.
Help would be seriously appreciated. I have been stuck on this for a while now.
Here is my PHP code (embedded inside HTML) that outputs the search results in a table:
<?php
if (!empty($_POST['search-submit'])) {
if (!preg_match("[/^$|\s+/]", $keywords)) {
echo "<tr><td>" . "<b>Job Title</b>" . "</td><td>" . "<b>Job Description</b>" . "</td><td>" . "<b>Job Date</b>" . "</td></tr>";
foreach ($search_query as $row) {
echo "<tr><td>" . $row['JobTitle'] . "</td><td>" . $row['JobDescription'] . "</td><td>" . $row['JobDate'] . "</td><td>" . '<input class = "btn btn-primary btn-sm" type = "submit" value = "Apply" name = ' . $row['Job_ID'] .'>' ."</td></tr>";
}
} elseif (preg_match("[/^$|\s+/]", $keywords)) {
echo "<p>" . "No matches found." . "</p>";
}
}
?>
The solution was to add a form for each loop of the result produced from the search query and add a hidden field with the Job_ID attribute as its value.
Based on which button is clicked, the value assigned to the hidden field would be posted and later on accessible by default queries such as $_POST['name_of_field'].
<?php
if (!empty($_POST['search-submit'])) {
if (!preg_match("[/^$|\s+/]", $keywords)) {
echo "<tr><td>" . "<b>Job Title</b>" . "</td><td>" . "<b>Job Description</b>" . "</td><td>" . "<b>Job Date</b>" . "</td></tr>";
foreach ($search_query as $row) {
echo '<form method = "post" action = "">';
echo '<input type = "hidden" name = "applyjob_id" value = "' . $row['Job_ID'] . '"> ';
echo "<tr><td>" . $row['JobTitle'] . "</td><td>" . $row['JobDescription'] . "</td><td>" . $row['JobDate'] . "</td><td>" .
'<input class = "btn btn-primary btn-sm" name = "applyjob" type = "submit" value = "Apply" >' ."</td></tr>";
echo '</form>';
}
} elseif (preg_match("[/^$|\s+/]", $keywords)) {
echo "<p>" . "No matches found." . "</p>";
}
}
if (!empty($_POST['applyjob'])) {
$applyjob_id = isset($_POST['applyjob_id']) ? $_POST['applyjob_id'] : '';
$stick_info = "UPDATE `userjob` SET `Seeker_ID` = '$seekerjobid' WHERE `userjob`.`Job_ID` = '$applyjob_id'";
mysqli_query($conn, $stick_info);
}
?>
I am trying to display the list of products depending on the categories they are under in my program. So far this is what my code looks like:
$query = "SELECT products_name, categories_desc, id, price
FROM products, categories
WHERE products.categories_id = categories.categories_id";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)){
echo "<b>" . $row['categories_desc'] . "</b><br>";
while($row=mysql_fetch_array($result)){
echo "<input type='checkbox' name='" . $row['products_name'] . "' value='" . $row['price'] . "'>" . $row["products_name"] . "</br>";
}
}
But obviously, I am doing something wrong. It only displays the first category then all the products. Help, anyone? Thanks in advance!
You don't need the second loop. It is what it's wrong in your program.
It should be something like:
$query = "SELECT products_name, categories_desc, id, price
FROM products, categories
WHERE products.categories_id = categories.categories_id";
$result = mysql_query($query);
$last_category='';
while($row=mysql_fetch_array($result)){
if ($last_category!=$row['categories_desc']){
echo "<b>" . $row['categories_desc'] . "</b><br>";
$last_category=$row['categories_desc'];
}
echo "<input type='checkbox' name='" . $row['products_name'] . "' value='" . $row['price'] . "'>" . $row["products_name"] . "</br>";
}
I have a form that posts to a PHP page. This is a snippet of the page. The goal is to read out a SQL table and then you click on the text values and they are editable. You then edit them and submit and it updates the SQL table. I basically need a way to strip the letter from the beginning of the id. but the letter is important because it defines in what column the new information goes into. Also if there is a different language or method to do this, I am open to suggestions.
<script type="text/javascript">
function exchange(id){
var ie=document.all&&!window.opera? document.all : 0
var frmObj=ie? ie[id] : document.getElementById(id)
var toObj=ie? ie[id+'b'] : document.getElementById(id+'b')
toObj.style.width=frmObj.offsetWidth+7+'px'
frmObj.style.display='none';
toObj.style.display='inline';
toObj.value=frmObj.innerHTML
}
</script>
<?php
$result = mysqli_query($con,"SELECT * FROM List") or die(mysql_error());
var_dump($_POST);
echo "<table id=list>
<tr id='list_header'>
<th class='list'>ID</th>
<th class='list'>Description</th>
<th class='list'>Winner</th>
</tr><form name='edit' action='inde.php?id=prizes' method='post'>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='list'><span id='n" . $row['Number'] . "' onclick='exchange(this.id)'>" . $row['Number'] . "</span><input name='n" . $row['Number'] . "b' id='n" . $row['Number'] . "b' class='replace' type='text' value='" . $row['Number'] . "' style='display:none;'></td>";
echo "<td class='list'><span id='d" . $row['Number'] . "' onclick='exchange(this.id)'>" . $row['Description'] . "</span><input name='d" . $row['Number'] . "b' id='d" . $row['Number'] . "b' class='replace' type='text' value='" . $row['Description'] . "' style='display:none;'></td>";
echo "<td class='list'><span id='w" . $row['Number'] . "' onclick='exchange(this.id)'>" . $row['Winner'] . "</span><input name='w" . $row['Number'] . "b' id='w" . $row['Number'] . "b' class='replace' type='text' value='" . $row['Winner'] . "' style='display:none;'></td>";
echo "<td></td>";
echo "</tr>";
}
echo "</table><input type='submit' value='Save Changes'></form>";
?>`
From what I understand you have a string id and you want to do the following:
if(strlen($id)>1){
$important=substr($id, 0, 1); //that gets the important stuff
$id = ($id, 1, strlen($id)); //that's id without the first char
}
else{
$important=$id;
$id=""; //empty string
}
You may want to try Ajax to send the modified values to the server one by one instead of all together at the end.