Append Select List value to URL - html

I have a form that is nothing more than a select list that is displaying records from a database and a button. All I want to do is when they select an option and click Submit, it takes them to a page that deletes the record in question. To delete it - I need the tech_id (from the select list) to be appended to the URL. I've done it the way I would with a text field, but that didn't work. Any suggestions?
<form method="post" id="form1" name="form1" action="delete-tech.php?tech_id=<?php echo $_POST['technician']; ?>">
<p>Choose Technician to Delete:
<select name="technician" id="technician" title="technician">
<?php
do {
?>
<option value="<?php echo $row_getTechs['tech_id']?>"><?php echo $row_getTechs['tech_name']?></option>
<?php
} while ($row_getTechs = mysqli_fetch_assoc($getTechs));
$rows = mysqli_num_rows($getTechs);
if($rows > 0) {
mysqli_data_seek($getTechs, 0);
$row_getTechs = mysqli_fetch_assoc($getTechs);
}
?>
</select>
</p>
<p>
<input name="submit" type="submit" id="submit" value="Delete Technician">
</p>
</form>

The error lies in the coding construct.
I have slightly changed the position of a code segment following while statement as follows(i.e. placed it before the do phrase):
<form method="GET" id="form1" name="form1" action="delete-tech.php">
<p>Choose Technician to Delete:
<select name="technician" id="technician" title="technician">
<?php
$rows = mysqli_num_rows($getTechs);
if($rows > 0) {
mysqli_data_seek($getTechs, 0);
$row_getTechs = mysqli_fetch_assoc($getTechs);
do {
?>
<option value="<?php echo $row_getTechs['tech_id']?>"><?php echo $row_getTechs['tech_name']?></option>
<?php
}while ($row_getTechs = mysqli_fetch_assoc($getTechs));
}
?>
</select>
</p>
<p>
<input name="submit" type="submit" id="submit" value="Delete Technician">
</p>
</form>
It should append the information(list-value) from the form to the URL as you desired. Check it out.
As you say it's not working there is another way using JavaScript:
<form method="post" id="form1" name="form1" action="delete-tech.php">
<p>Choose Technician to Delete:
<select name="technician" id="technician" title="technician">
<?php
$rows = mysqli_num_rows($getTechs);
if($rows > 0) {
mysqli_data_seek($getTechs, 0);
$row_getTechs = mysqli_fetch_assoc($getTechs);
do {
?>
<option value="<?php echo $row_getTechs['tech_id']?>"><?php echo $row_getTechs['tech_name']?></option>
<?php
}while ($row_getTechs = mysqli_fetch_assoc($getTechs));
}
?>
</select>
</p>
<p>
<input name="submit" type="button" id="submit" value="Delete Technician" onclick="location.href='delete-tech.php?tech_id='+document.getElementById('technician').value">
</p>
</form>

Related

Show the selected value everytime in the dropdown list

I need to display the selected category value displayed all the time. I have a list of category values and adding it to drop-down list from an array.
<form action="#" method="post">
<select name="dropDown" id="drop_down_id">
<option value=""> Select CATEGORY</option>
<?php
foreach($decoded as $key => $value ){
foreach($value as $key1 => $value1 ){
?> <option value="<?php echo $value1;?>"><?php echo $value1;?></option><?php
}
}
?>
<input type="submit" name="submit" value="Submit"/>
</form>
I see that you use post for form so you can use POST to know last selected:
$dropDown=htmlentities($_POST['dropDown'], ENT_QUOTES, "UTF-8");
And then just simple make if statement and its done:
if($dropDown==$value1){$selected='selected';}else{$selected=null;}
result:
<?php
$dropDown=htmlentities($_POST['dropDown'], ENT_QUOTES, "UTF-8");
?>
<form action="#" method="post">
<select name="dropDown" id="drop_down_id">
<option value=""> Select CATEGORY</option>
<?php
foreach($decoded as $key => $value ){
foreach($value as $key1 => $value1 ){
if($dropDown==$value1){$selected='selected';}else{$selected=null;}
echo '<option value="'.$value1.'" '.$selected.'>'.$value1.'</option>';
}
}
?>
<input type="submit" name="submit" value="Submit"/>
</form>
You can also store it in PHP SESSSION http://php.net/manual/en/function.session-start.php

trouble appending/combining two variables

Here's my code, It's a forum, there are posts with dislike buttons,like buttons, and a text box for commenting with each individual post, check the screenshot below. I'm trying to put the $comments variable, which are the comments itself, at the end of $posts so it displays below the posts properly like the like buttons and comment box are displayed.
I'm trying to just get the comments displayed underneath the comment box. If I call displayComments(the function in $comments definition) in the for each loop then the comments get displayed separately from the posts I've tried combining the $comments at the end of $posts with this operator +, and I've tried making a new variable equal to the both of them. Here's the code(not the full file).
<?php
include("connect.php");
include("check.php");
include("Trying.php");
include("Comment.php");
if(isset($_POST['username'])){
if (isset($_POST['post'])) {
if ($_FILES['postimg']['size'] == 0)
{
$postbody = $_POST['postbody'];
$loggedInUserId = check::isLoggedIn();
if (strlen($postbody) > 160 || strlen($postbody) < 1)
{
die('Incorrect length!');
}
connect::query('INSERT INTO dry_posts VALUES (null, :postbody, NOW(),\'\',0,0)', array(':postbody'=>$postbody));
}
}
if (isset($_POST['comment'])) {
$postid = $_GET['postid'];
$commentbody = $_POST['commentbody'];
if (strlen($commentbody) > 160 || strlen($commentbody) < 1) {
die('Incorrect length!');
}
connect::query('INSERT INTO comments VALUES (null,:comment, NOW(), :postid)',array(':comment'=>$commentbody,':postid'=>$postid));
//}
}
$dbposts = connect::query('SELECT * FROM dry_posts ORDER BY id DESC');
$posts = "";
$comments = Comment::displayComments($p['id']);
foreach($dbposts as $p){
if (!connect::query('SELECT post_id FROM dry_likes WHERE post_id=:postid', array(':postid'=>$p['id']))) {
$posts .="<img src='".$p['postimg']."'>".htmlspecialchars($p['body'])."
<form action='try.php?postid=".$p['id']."' method='post'>
<input type='submit' name='like' value='Like'>
<span>".$p['likes']." likes</span>
<input type='submit' name='dislike' value='Dislike'>
<span>".$p['dislikes']." dislikes</span>
</form>
<hr /></br />
;
<form action='try.php?postid=".$p['id']."' method='post'>
<textarea name='commentbody' rows='3' cols='50'></textarea>
<input type='submit' name='comment' value='Comment'>
</form>
<hr /></br />
";
//Comment::displayComments($p['id']);
}
else{
$posts .="<img src='".$p['postimg']."'>".htmlspecialchars($p['body'])."
<form action='try.php?postid=".$p['id']."' method='post'>
<input type='submit' name='like' value='Like'>
<span>".$p['likes']." likes</span>
<input type='submit' name='dislike' value='Dislike'>
<span>".$p['dislikes']." dislikes</span>
</form>
<hr /></br />
<form action='try.php?postid=".$p['id']."' method='post'>
<textarea name='commentbody' rows='3' cols='50'></textarea>
<input type='submit' name='comment' value='Comment'>
</form>
<hr /></br />
";
//Comment::displayComments($p['id']);
}
}
?>
<form action='try.php' class = "forum" method="post" enctype="multipart/form-data">
<textarea name="postbody" rows="4" cols="60" class = "text"></textarea>
<br />Upload an image:
<input type="file" name="postimg">
<input type="submit" name="post" value="Post">
</form>
<div class="posts">
<?php echo $posts;
?>
</div>
Here's the function I'm calling in the $comments variable
public static function displayComments($postid)
{
$comments = connect::query('SELECT * FROM comments WHERE post_id=:postid',array(':postid'=>$postid));
foreach($comments as $comment)
{
echo $comment['comment']."<hr />";
}
}
I know this won't help a lot of people but I'm just not sure what to do, How do you suggest I get everything to display inline, should rearrange my code?

Joomla 1.5 Database Query example needed

I have made a query, using mysql_* way, that select's some values from a table, where row's id is 1. Then I put all those values in variables so I can echo them in to my form. How can I do the same thing using Joomla's database query way ?
Here is an example of my code that working, using mysql_*:
<?php // DATABASE QUERY
$query="SELECT countdown_module, hometeam_header
FROM jos_gm_nextmatch
WHERE id = 1";
$result=mysql_query($query);
// DATABASE VARIABLES
$countdown_module = mysql_result($result,$i,"countdown_module"); ?>
$hometeam_header = mysql_result($result,$i,"hometeam_header"); ?>
<form action="" method="post" name="form">
<input name="countdown_module" value="<?php echo $countdown_module ?>" type="text" />
<input name="hometeam_header" value="<?php echo $hometeam_header ?>" type="text" />
<input name="submit" type="submit" value="UPDATE" />
</form>
OK I found it!!! Here is an example...
<?php // DATABASE QUERY
$db = JFactory::getDBO();
$query="SELECT countdown_module, hometeam_header
FROM jos_gm_nextmatch
WHERE id = 1";
$db->setQuery($query);
$rows = $db->loadObjectList();
$itemrow = $rows[0];
// DATABASE VARIABLES
$countdown_module = $itemrow->countdown_module;
$hometeam_header = $itemrow->hometeam_header; ?>
<form action="" method="post" name="form">
<input name="countdown_module" value="<?php echo $countdown_module ?>" type="text" />
<input name="hometeam_header" value="<?php echo $hometeam_header ?>" type="text" />
<input name="submit" type="submit" value="UPDATE" />
</form>

mySQL query returns only one result

I am trying to get each row in a table to appear as part of a survey. The following code is returning only the first row in the table (so users can see only one question). I've been over and over this and can't see what I'm doing wrong. Would much appreciate any input.
Thank you!
function getQuestions ($dbc) <!--$dbc=database connection--> {
$query = "SELECT * FROM survey_questions" <!--survey_questions=table--> ;
$result = #mysqli_query ($dbc, $query);
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC) ) {
$body = $row ['question_body'] <!--question_body=row in table--> ;
echo '
<div class="entry"> <!--user entry form-->
<h3 class="qTitle">'.$body.'</h3>
<form action="index.php" method="post">
<input type="text" name="answer" size="85" />
<input type="submit" value="Submit" name="submit" />
<input type="hidden" name="questionid" value="questionid" />
<input type="hidden" name="submitted" value="1" />
</form>
</div>
';
}
}
First of all you don't need to echo so much using php...
You have to use // or /* */ to comment in PHP and not <!----> cuz that's for HTML
Secondly coming to your code..
Why you are using?
function getQuestions($dbc) //I dont know what this is doing here, why you are wrapping your code in a function???
you can simply write like this (use echo to print out your question):
<?php
$result = mysqli_query($dbc, "SELECT * FROM survey_questions");
while ($row = mysqli_fetch_array ($result) ) {
?>
<div class="entry"> <!--user entry form-->
<h3 class="qTitle"><?php echo $row['whatever']; ?></h3>
<form action="index.php" method="post">
<input type="text" name="answer" size="85" />
<input type="submit" value="Submit" name="submit" />
<input type="hidden" name="questionid" value="questionid" />
<input type="hidden" name="submitted" value="1" />
</form>
</div>
<?php
}
?>
The results are now two entry forms (an improvement) but still not displaying the text of the two rows from the database. Instead of the row text, I get the > character where the text should be. Here is the updated code, adapting the suggestion of #Mr. Alien:
function getQuestions($dbc) {
$result = mysqli_query($dbc, "SELECT * FROM survey_questions");
while ($row = mysqli_fetch_array ($result) ) {
echo '
<div class="entry">
<h3 class="qTitle">'. $row['survey_questions'].'></h3>
<form action="index.php" method="post">
<input type="text" name="answer" size="85" >
<input type="submit" value="Submit" name="submit" >
<input type="hidden" name="questionid" value="questionid" >
<input type="hidden" name="submitted" value="1" >
</form>
</div>
';
}
}

no mysql insert if header() is added

I'm trying to get my form to submit and then use header() to redirect.
MYSQL update works fine as long as I DON'T include header('location: setup2.php'); on line 9. When I add it, the redirect works, but the MYSQL doesn't update. Ideas?
<?php
$link;
#mysql_select_db(stevensp_beattrack) or die( "Unable to select database");
$inst=$_POST['inst'];
$id=$_SESSION['user_id'];
if(isset($_POST['submit'])) {
$query = "UPDATE users SET inst='$inst' WHERE id=$id";
mysql_query($query);
header('location: setup2.php'); //adding this line stops the update from working
}
?>
<HTML>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<select name="inst">
<option value="None">Select a Specialty:</option>
<option value="Voice">Voice</option>
<option value="Guitar">Guitar</option>
</select><br/><br/>
<input type="submit" name="submit" value="Next"/>
</form>
</HTML>