i have a search form whose action attribute contains an id to be passed in the URL and form is submitted from get method for user to get the search results to be shared or bookmarked.
Now the problem i am facing is, that id i have passed in action is not displaying in the URL but all other input field's values are appearing fine.
This is my form:
<form action="trails.php?pname=<?php echo $getName;?>" id="filter" method="GET" name="filter">
<select class="multiselect form-control" multiple="multiple" name='activity[]' id="activity">
<?php
$toaSql = mysql_query("select * from type_of_activity where cat_id='1'");
while ($toaRow = mysql_fetch_array($toaSql)) {
echo "<option value='" . $toaRow['typeOfActivity'] . "'><img style='width: 21px; margin-left: 5px;' src='home/images/" . $toaRow['catImg'] . "'> " . $toaRow['typeOfActivity'] . "</option>";
}
?>
</select>
<select class="multiselect form-control" multiple="multiple" name='activity[]' id="activity">
<?php
$toaSql = mysql_query("select * from type_of_activity where cat_id='2'");
while ($toaRow = mysql_fetch_array($toaSql)) {
echo "<option value='" . $toaRow['typeOfActivity'] . "'><img style='width: 21px; margin-left: 5px;' src='home/images/" . $toaRow['catImg'] . "'> " . $toaRow['typeOfActivity'] . "</option>";
}
?>
</select>
<select class="multiselect form-control" multiple="multiple" name='activity[]' id="activity">
<?php
$toaSql = mysql_query("select * from type_of_activity where cat_id='3'");
while ($toaRow = mysql_fetch_array($toaSql)) {
echo "<option value='" . $toaRow['typeOfActivity'] . "'><img style='width: 21px; margin-left: 5px;' src='home/images/" . $toaRow['catImg'] . "'> " . $toaRow['typeOfActivity'] . "</option>";
}
?>
</select>
</form>
trail.php page:
if(isset($_GET['pname'])){
$getName = $_GET['pname'];
// print_R($getName);
if($getName=='video'){
$queryVideo = "SELECT * FROM `contributevideo` WHERE status='active' " ;
//echo $queryVideo;
}elseif($getName=='album'){
$queryAlbum = "SELECT * FROM `contributeimage` WHERE status='active' AND category='album'" ;
}
}
Trails.php page run the sql queries according to the page name it gets from the url, but in url its not finding any pname variable so the queries are not executing.
Where i am doing wrong please guide.
Thanks
Why are you describing parameter as URL-encoded?
<form action="trails.php?pname=<?php echo $getName;?>" id="filter" method="GET" name="filter">
// Existing form
Since your form's method is 'GET', you can just describe a hidden input.
<form action="trails.php">
<input type="hidden" name="pname" value="<?php echo $getName; ?>"/>
// Existing form
It should work.
The issue with your code is that you set the form to submit the values using the GET method. This means that the values from the form fields will be passed in the URL, overwriting (removing) your pname value.
To fix it just change the method of the form to POST and the PHP code to access activities through $_POST['activities'], it should work then.
Related
I am trying to retrieve data field from a database and store them in a dropdown box so that they correlate to each other which i have done but when I put them into a dropdown box they appear but with no spaces between them and I want the words separated.
Any help much appreciated.
<form name="FrmAmend" method="post" action="amenddvd2.php">
Select from the list below<br>
<select name="Film" t[enter link description here][1]itle="Select DVD">
<option value="" selected disabled>Select DVD</option>
<?php
$result = mysqli_query($con, "SELECT `DVD ID`, `Title`, `Certificate` FROM tbldvd;");
while ($row = mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['DVD ID'] . $row['Title'] . $row['Certificate'] . "'>" . $row['DVD ID'] . $row['Title'] . $row['Certificate'] . "</option>";
}
?>
</select><br>
<a href="menu.php"</a>Return To Main Menu<br>
<input type="submit" name="Submit" value="Select DVD">
</a>
</form>
It will be better you do it this way
<form name="FrmAmend" method="post" action="amenddvd2.php">
Select from the list below<br>
<select name="Film" t[enter link description here][1]itle="Select DVD">
<option value="" selected disabled>Select DVD</option>
<?php
$result = mysqli_query($con, "SELECT `DVD ID`, `Title`, `Certificate` FROM tbldvd;");
while ($row = mysqli_fetch_assoc($result)) {
?>
<option value=' <?php echo"$row['DVD ID'] $row['Title'] $row['Certificate']" ?> ' selected disabled>Select DVD</option>
<?php
}
?>
</select><br>
<a href="menu.php"</a>Return To Main Menu<br>
<input type="submit" name="Submit" value="Select DVD">
</a>
</form>
concat a space between the vars
echo "<option value='" . $row['DVD ID'] . $row['Title'] . $row['Certificate'] . "'>" . $row['DVD ID'] . ' ' . $row['Title'] . ' ' . $row['Certificate'] . "</option>";
When I load my page, the value of the variable, $v_UpdateONE, is "Select Version". When I select a version, the value goes blank.
I need to grab the selected value for use in a DB update statement.
Thank you for any assistance. -James
<FORM METHOD="post" ACTION="Update.php" WIDTH="50">
<?php
$avQuery = "SELECT $v_software1 FROM version_master.vermas_availableversions WHERE $v_software1 IS NOT NULL ORDER BY SortCol DESC";
$a_AvailVers = mysql_query($avQuery);
#_Version dropdown box
echo "<select NAME='AvailVersONE' ONCHANGE=submit()>";
echo "<option>Select Version</option>";
while ($row = mysql_fetch_array($a_AvailVers)) {
echo "<option value='" . $row['$v_software1'] . "'>" . $row[$v_software1] . "</option>";
}
echo "</select>";
$v_UpdateONE = $_POST['AvailVersONE'];
echo $v_UpdateONE;
?>
</FORM>
You have an error in
value='" . $row['$v_software1'] . "'
Since $v_software1 is in single quotes, it will be literal $v_software1.
Try removing the quotes -
value='" . $row[$v_software1] . "'
You need to post before you can read $_POST data.
Form File
<FORM METHOD="post" ACTION="Update.php" WIDTH="50">
<?php
$avQuery = "SELECT $v_software1 FROM version_master.vermas_availableversions WHERE $v_software1 IS NOT NULL ORDER BY SortCol DESC";
$a_AvailVers = mysql_query($avQuery);
#_Version dropdown box
echo "<select NAME='AvailVersONE' id='AvailVersONE' ONCHANGE=submit()>";
echo "<option>Select Version</option>";
while ($row = mysql_fetch_array($a_AvailVers)) {
echo "<option value='" . $row['$v_software1'] . "'>" . $row[$v_software1] . "</option>";
}
echo "</select>";
?>
<button type="submit"> <!-- this will draw a submit button -->
</FORM>
then on your Update.php
<?php
$v_UpdateONE = $_POST['AvailVersONE'];
echo $v_UpdateONE;
?>
Sometimes, the ID needs to be filled up (browser dependent)
<form action='main.php' method='POST'>
<select name="Category" class='listbox'>
<?php $cat = mysql_query("select cName from category");
while($drop = mysql_fetch_array($cat))
{
echo '<option value="' . $drop['Category'] . '">' . $drop['cName'] . '</option>';
}
?>
<input type='text' name='search' class='namebox'>
<input type='submit' name='submit' value='Search' class='submitbox'></select>
</select>
i am trying to use this form to create a search engine from my database but just cant get the value from the drop down menu.
$submit = $_POST['submit'];
if($submit)
{
$search = $_POST['search'];
$catval = $_POST['Category'];
echo $catval ;
$searchval = mysql_query("select * from item where iname like '%$search%'and cId in (select cId from category where cName = '$catval')");
while($info = mysql_fetch_array($searchval))
{
echo "Item Name: " . $info['iName'];
}
}
so when i try to search using this method i get no results.
You have placed the selects closing tag wrong :) and I assume you remembered to close your form aswell?
<form action='main.php' method='POST'>
<select name="Category" class='listbox'>
<?php $cat = mysql_query("select cName, Category from category");
while($drop = mysql_fetch_array($cat))
{
echo '<option value="' . $drop['Category'] . '">' . $drop['cName'] .'</option>';
}
?>
</select>
<input type='text' name='search' class='namebox'>
<input type='submit' name='submit' value='Search' class='submitbox'>
</form>
<form action='main.php' method='POST'>
<select name="Category" class='listbox'>
<?php $cat = mysql_query("select cName from category");
while($drop = mysql_fetch_array($cat))
{
echo '<option value="' . $drop['Category'] . '">' . $drop['cName'] .
'</option>';
}
?>
</select>
<input type='text' name='search' class='namebox'>
<input type='submit' name='submit' value='Search' class='submitbox'>
</form>
That should do the trick. Your </select> was in the wrong place.
In the meantime, you might want to look into PDO and bound values instead of using mysql() as it's depreciated (I.E. don't use it anymore) and insecure.
Still getting used to stackoverflow excuse my rookie-ness.. :)
Have an SQL query that returns a data put into a table in php. I want this table to be used for purchasing.
My idea was to use the product id, meaning i would use a dynamic php variable (not sure if I'm doing that right now) believe I saw a post something like $.$varaible$.$ it wasn't very clear and was a different subject.
My code is as follows:
$result = mysqli_query($con, "SELECT * FROM Product WHERE Type = 'Game'");
?>
<div class="wrapper">
<h1 class="headGame">Buy Some Games Man</h1>
</div>
<br />
<div class="wrapper">
<?php
echo
"<table border='1'>
<tr>
<th> Name </th>
<th> Picture </th>
<th> Console </th>
<th> Description </th>
<th> Price </th>
<th> Amount </th>
</tr>";
echo '<form id="gamesOrder" action="purchase.php">';
while($row = mysqli_fetch_array($result)) {
$id = $row['Pd_Key'];
echo"<tr>";
echo"<td>" . $row['Name'] . "</td>";
echo"<td>" . '<img class="prdPic" src="'. $row['Picture']. '">' . "</td>";
echo"<td>" . $row['Type2'] . "</td>";
echo"<td>" . $row['Description'] . "</td>";
echo"<td>" . $row['Price'] . "</td>";
echo"<td>" . '<input type="number" min="0" max="100"; name="'.$id.'" value=0>' . "</td>";
echo"</tr>";
}
echo '<input type="submit" value=" BUY ">';
echo '</form>';
?>
When I click the submit it changes the url but nothing happens, it doesn't redirect.
Any advice on how to get this entire process to work. The variable being used in a purchasing form, via a php file ie (purchase.php) and the variable to use for this.
EDIT - Had minor errors, but still not 100% on variable %id, won't that get redefined each loop, how can I have it dynamic so it can be used in a form to identify what the user wants to buy.
Now redirects but not to purchase.php
URL is ~/purchase.php?1=0&2=0&3=0&4=0&5=0&6=0&7=0&8=0&9=0&10=0&11=0&12=0&13=0&14=0&15=0&16=0&17=0&18=0&19=0&20=0&21=0
Thanks you legends you!! =D
You are missing the closing form caret:
echo '<form id="gamesOrder" action"purchase.php"';
should be:
echo '<form id="gamesOrder" action="purchase.php">';
Also you concatenation for ID is incorrect:
echo"<td>" . '<input type="number" min="0" max="100"; name=".$id." value=0>' . "</td>";
should just be:
echo"<td>" . '<input type="number" min="0" max="100"; name="id" value="' .$id. '" value=0></td>';
To access the id in purchase.php use the following code:
$id = isset($_GET['id']) ? $_GET['id'] : null;
And you need to assign the action to the form with an equals sign:
action="myaction.php"
And you don't pass the id right...
echo"<td>" . '<input type="number" min="0" max="100"; name=".$id." value=0>' . "</td>";
should be
echo"<td>" . '<input type="number" min="0" max="100"; name="'.$id.'" value=0>' . "</td>";
Ough, and on form you need to put action =
echo '<form id="gamesOrder" action="purchase.php">';
Ok, first you've got some HTML error, lets see:
In the Form element you need to close it and also include the method (as POST), see:
< form id="gamesOrder" action="purchase.php" method="POST">
To send data using a form you will need to include the data from the data base in form fields like this:
echo '< input type="text" name="myFieldName" value="'.$row['Price'].'">';
Any question let me know...
Cheers.
I have been trying to populate the contents of one drop down list with the selection of an item in another, using php,html and mysql database and Its proved to be a lot harder than i thought for me. I'm trying to get one dropdown list show names of all mobile phones based on company selections in the first drop down list.
The code i've provided below has no errors, but the 2nd dropdown list is not getting populated. the variable '$submittedValue' is not getting the right value and hence '$name' also has no content in it. I can't seem to be able to figure out a reason for this.
what i've done is, to try to use the selected option in the first drop down list, put that into a variable and pass that as a parameter to the 2nd query.
I solution to this would be greatly appreciated.
<?php
$con=mysqli_connect("localhost","root","","dbmobiles");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$options = '';
$submittedValue = '';
$name=" ";
$q1=mysqli_query($con,"select distinct compName from umobile order by compName ASC");
while($row = mysqli_fetch_array($q1)) {
$options .="<option value=". $row['compName'] ." >" . $row['compName'] . "</option>";
}
$menu1 = "<form id='filter' name='filter' method='post' action=' '>
<p><label>Select Company</label></p>
<select name='filter' id='filter'>
" . $options . "
</select>
</form>";
if (isset($_POST["filter"])) {
$submittedValue = $_POST["filter"];
}
echo $menu1;
if ($q2=mysqli_prepare($con,"SELECT DISTINCT mobname FROM umobile WHERE compName=? "))
{
mysqli_stmt_bind_param($q2,"s",$submittedValue);
$s=$submittedValue;
mysqli_stmt_execute($q2);
mysqli_stmt_bind_result($q2,$r);
}
while(mysqli_stmt_fetch($q2)) {
$name .="<option>".$r['mobname']."</option>";
}
echo ".$name.";
$menu2 = "<form id='moblist' name='moblist' method='post' action=' '>
<p><label>Select Mobile</label></p>
<select name='moblist' id='moblist'>
" . $name . "
</select>
</form>";
echo $menu2;
?>
Don't forget to quote .$r['mobname'].
$name .="<option value=\"".$r['mobname']."\" >".$r['mobname']."</option>";
Your $_POST tests only .
if (isset($_POST["filter"])) {
$submittedValue = $_POST["filter"];
}
What about if $_POST["filter"] is NOT set ?
Your code did not take care of this fact. He always runs through the section $menu2.
Your $_POST is as the name implies, of a post. To evaluate a $_POST must be done a post.
<input type="submit" name="Submit" value="Submit" />
for example
$menu1 = "<form id='filter' name='filter' method='post' action=' '>
<p><label>Select Company</label></p>
<select name='filter' id='filter'>
" . $options . "
</select>
<input type=\"submit\" name=\"Submit\" value=\"Send\" />
</form>";
Put all the logic for the second menu in your code if (isset($_POST["filter"])) {
echo $menu1;
if (isset($_POST["filter"])) {
$submittedValue = $_POST["filter"];
if ($q2= ....)
{
mysqli_stmt_bind_param($q2,"s",$submittedValue);
$s=$submittedValue;
mysqli_stmt_execute($q2);
mysqli_stmt_bind_result($q2,$r);
while(mysqli_stmt_fetch($q2)) {
$name .="<option value=\"".$r['mobname']."\" >".$r['mobname']."</option>";
}
$menu2 = "<form id='moblist' name='moblist' method='post' action=' '>
<p><label>Select Mobile</label></p>
<select name='moblist' id='moblist'>
" . $name . "
</select>
</form>";
echo $menu2;
}
}
Update:
You can not use $r['mobname'] with a bind.
mysqli_stmt_bind_result($q2,$r);
....
while(mysqli_stmt_fetch($q2)) {
$name .="<option value=\"".$r['mobname']."\" >".$r['mobname']."</option>";
}
Use it like .
mysqli_stmt_bind_result($q2,$r);
....
while (mysqli_stmt_fetch($q2)) {
$name .="<option value=\"".$r."\" >".$r."</option>";
}
mysqli_stmt_close($q2);
Update 2
you forget to quote that to
$options .="<option value=". $row['compName'] ." >" . $row['compName'] . "</option>";
right
$options .="<option value=\"". $row['compName'] ."\" >" . $row['compName'] . "</option>";
$menu1 = "<form id='filter' name='filter_form' method='post' action=' '>
<p><label>Select Company</label></p>
<select onchange='filter_form.submit()' name='filter' id='filter'>
" . $options . "
</select>
</form>";
there is no submit button in your form , either place a submit button or try to submit on selection on dropdown values.
Only then if($_POST['filter']) will be true and your variable will successfully get assignment