PHP Echo Form Action URL Issue - php

I have this code:
echo " <form action='https://test.com/forum/index.php?/search/&q=".
$row['record_meta_keywords'] ."
&type=cms_records7&search_and_or=and&search_in=titles'> ";
echo " <select name='/search/&q=". $row['record_meta_keywords'] ."
&type=cms_records7&search_and_or=and&search_in=titles'> ";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['record_meta_keywords'] ."'>" .
$row['record_meta_keywords'] ."</option>";
}
echo " </select> ";
echo " <br><br> ";
echo " <input type='submit'> ";
echo " </form> ";
?>
But, when I choose an option in the dropdown, the URL is being displayed like this:
https://test.com/forum/index.php?search=%26q%3DHowell+Lanes+%26type%3Dcms_records7%26search_and_or%3Dand%26search_in%3Dtitles
The problem is I need the URL to be displayed like this in order for it to work properly:
https://test.com/forum/index.php?/search/&q=Howell%20Lanes%20&type=cms_records7&search_and_or=and&search_in=titles
I tried urlencode but that didn't seem to work either.
How do I solve this problem?

Related

PHP MySQL keep selected option from dropdown menu when menu is select from database

I have a problem with my page. I was trying to solve it by a lot of tutorials but i don´t know how to make it work. Simply put i have a database of objects. When i select object, page will redirect to another where are shown all informations about the object. But i need to keep the selected option in drop down menu. There is 110 objects so if i select object number 25, informations will show but the drop down menu wont stay on number 25. Can somebody help me with it?
<form action="dbobj2.php" method="post" name="form1">
Zoznam objektov<br>
<?php
include('System/connect.php');
$sql = "SELECT Objekt FROM DBObj";
$result = mysqli_query($db,$sql);
echo "<select name='Objekt'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Objekt'] . "'>" . $row['Objekt'] . "</option>";
}
echo "</select>";
?>
<input name="btnSubmit" type="submit" value="Vyber">
</form>
<?php
echo $_POST['Objekt'];
echo "<hr>";
$strSQL = "SELECT * FROM DBObj WHERE Objekt = '".$_POST['Objekt']."' ";
$objQuery = mysqli_query($db,$strSQL);
$objResult = mysqli_fetch_array($objQuery);
$imgRes=$objResult['URLobr'];
echo '<img src="http://page.sk.sk/imgs/'.$imgRes.'" alt="obj" height="600" width="800"/>';
echo "<hr>";
echo $objResult['Text'];
?>
First page php
<form action="dbobj2.php" method="post" name="form1">
Zoznam objektov<br>
Vyberte si zvolený objekt z menu a stlačte tlačidlo výber<br>
<?php
include('System/connect.php');
$sql = "SELECT Objekt FROM DBObj";
$result = mysqli_query($db,$sql);
echo "<select name='Objekt'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Objekt'] . "'>" . $row['Objekt'] . "</option>";
}
echo "</select>";
?>
Check whether the post value matches the object id in the loop.
$selcted = $_POST['Objekt'] == $row['Objekt'] ? ' selected' : '';
If it does $selected is set to " selected" and added to the option.
echo "<option value='" . $row['Objekt'] . "'" . $selected . ">" . $row['Objekt'] . "</option>";
You can send that value $_POST['Objket'] via a variable let us say $selected_value and in the option tag you can write
<option value='" .$row['Objekt']. "' '.if($row['Objket']==$selected_value) echo selected.'>" .$row['Objekt']. "</option>
When you select a option and click on button then it will work otherwise no.
if(isSet($_POST['Objekt']))
{
echo $_POST['Objekt'];
echo "<hr>";
$strSQL = "SELECT * FROM DBObj WHERE Objekt = '".$_POST['Objekt']."' ";
$objQuery = mysqli_query($db,$strSQL);
$objResult = mysqli_fetch_array($objQuery);
$imgRes=$objResult['URLobr'];
echo '<img src="http://page.sk.sk/imgs/'.$imgRes.'" alt="obj" height="600" width="800"/>';
echo "<hr>";
echo $objResult['Text'];
}

Placing id/option value from dropdown selection into variable PHP, MySQL HTML

I have a dropdown list that is filled with some values from a MySQL table.
The issue i'm facing is that I cannot place the ID from the selected value into a variable.
This is my code up until now:
<?php
mysql_connect('localhost', 'confidential', 'confidential');
mysql_select_db('mydb');
$sql = "SELECT zone_naam FROM zone";
$result = mysql_query($sql);
echo "<select name='zone_1' id='zone_1'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['idzone'] . "'>" . $row['zone_naam'] . " </option>";
}
echo "</select>";
?>
I would think that I could get the value from $row['idzone'] by using the code below.
if(isset($_POST['submit'])) {
$zone_1 = $_POST['zone_1'];
$richting = $_POST['richting'];
$zone_2 = $_POST['zone_2'];
}
I've tried several things, but I cannot come to a solution.
If I want to do the same thing in HTML with self set data like below it always works, but whenever I want to use PHP for this purpose I seem to fail.
<select name="zone">
<option value="1">Zone1</option>
<option value="2">Zone2</option>
<option value="3">Zone3</option>
</select>
I hope you all understand what I mean and can help me to find the cause of this problem.
Best regards,
Rudibwoyyy
#Rudi
Then David is correct. If you submit the HTML from your post
<select name="zone">
<option value="1">Zone1</option>
<option value="2">Zone2</option>
<option value="3">Zone3</option>
</select>
you can use, according the select name "zone", the variable $_POST['zone'], assuming you are using <form method="post">, (otherwise it's $_GET['zone']) will contain the value (1, 2 or 3 respectively) of the selected entry.
If this doesn't work for you, check if the generated HTML Code hat the correct name.
Below is my code that works!! Thanks for all the tips
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('confidential');
$sql = "SELECT * FROM zone";
$result = mysql_query($sql);
echo "<form type='submit' class='form-inline' action='' method='POST'>
<select name='zone_1' id='zone_1' class='form-control mb-2 mr-sm-2 mb-sm-
0'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='". $row['idzone'] . "'>" . $row['zone_naam'] . "
</option>";
}
echo "</select>";
echo "<form type=\"submit\" method=\"POST\"> ";
echo " <select name=\"richting\" class='form-control mb-2 mr-sm-2 mb-sm-0'> ";
echo " <option value=\"<-->\"> <--> </option> ";
echo " <option value=\"<--\"> <-- </option> ";
echo " <option value=\"-->\"> --> </option> ";
echo "</select> ";
$result2 = mysql_query($sql);
echo "<select name='zone_2' class='form-control mb-2 mr-sm-2 mb-sm-0'>";
while ($row = mysql_fetch_array($result2)) {
echo "<option value='" . $row['idzone'] . "'>" . $row['zone_naam'] . "
</option>";
}
echo "</select>";
echo "<br><br>";
echo "<input type='submit' value='Submit' name='submit' id='submit' class='btn btn-primary btn-sm'>";
echo "</form>";
if(isset($_POST['submit'])) {
$zone_1 = $_POST['zone_1'];
$richting = $_POST['richting'];
$zone_2 = $_POST['zone_2'];
}
?>

Delete individual lines/rows with PHP button

What I am trying to do is have a PHP page display the contents of a MySQL database and and each line it displays, it give me a delete button so I can delete individual rows. I have the code kind of working. The code delete the lines of code, but not the one I select, instead it deletes the last line and not the one I tell it to delete.
Here is a screenshot, I know it does not look pretty, I was going to clean it up after I get the code working.
HTML:
<?php
// Delete php code
if(isset($_POST['delete_series']))
{
// here comes your delete query: use $_POST['deleteItem'] as your id
$delete = $_POST['delete_series'];
$delete_sql = "DELETE FROM `Dropdown_Series` where `id` = '$delete'";
if(mysql_query($delete_sql, $conn))
{
echo "Row Deleted </br>";
echo "$delete";
};
}
//Insert Code
if(isset($_POST['add_series']))
{
$insert_sql = "INSERT INTO dropdown_series (series) VALUES ('$_POST[add_series]' )";
if(!mysql_query($insert_sql, $conn))
{
die ('Error: ' . mysql_error());
}
echo "Series Added <br><br>";
}
?>
<br />
<h1>Add New Series Title</h1>
<h3>Add New: </h3><br />
<form name = action="add_series.php" method="POST">
<input type = "text" name = "add_series" required/><br /><br />
<input type ="submit" name="submit" value="Add">
</form>
<h3>Current Series: </h3><br />
<?php
//Delete button on each result of the rows
$query = mysql_query("SELECT id, series FROM Dropdown_Series"); // Run your query
// Loop through the query results, outputing the options one by one
echo "<form action='' method='POST'>";
while ($row = mysql_fetch_array($query))
{
echo $row['series'] . " ";
echo $row['id'];
echo "<input type='hidden' name='delete_series' value=' " . $row['id'] . "' />";
echo "<input type='submit' name='submit' value='Delete'>";
//echo $row['series'] . " ". $row['id'] . "<input type='submit' value='delete'>";
echo "<br />";
}
echo "</form>";
?>
Your loop will result in multiple delete_series inputs - within the same form.
You could create separat forms for each option:
while ($row = mysql_fetch_array($query))
{
echo "<form action='' method='POST'>";
// ...
echo "<input type='hidden' name='delete_series' value=' " . $row['id'] . "' />";
echo "<input type='submit' name='submit' value='Delete'>";
// ...
echo "</form>";
}
I believe that a very quick answer would be
while ($row = mysql_fetch_array($query))
{
echo "<form action='' method='POST'>";
echo $row['series'] . " ";
echo $row['id'];
echo "<input type='hidden' name='delete_series' value=' " . $row['id'] . "' />";
echo "<input type='submit' name='submit' value='Delete'>";
//echo $row['series'] . " ". $row['id'] . "<input type='submit' value='delete'>";
echo "<br />";
echo "</form>";
}
If I were you I would prefer injecting some javascript in the logic, and perform the Delete request using an ajax call.
you can refer to this tutorial to get more understanding of what I am refering to https://scotch.io/tutorials/submitting-ajax-forms-with-jquery

How do i populate a dropdown list by selecting the value from another dropdown list?

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

how to use a link to download a file in php

I am using the following code to fill the drop-down box and select a file and download it. Its working perfect.But i tried using every file to display as link and download it on clicking the link.
echo "<form id=\"form\" name=\"psform\" action=\"download_logic.php\" method=\"post\"><label>Select File:&nbsp</label><select name=\"file\" >";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['location'] . "'>" . $row['location'] . "</option>";
}
echo "</select></label>";
echo"<br>";
echo "<input id=\"submit\" type=\"submit\" name=\"filesubmit\" value=\"Download\" /> </form>";
its giving me errors ....any help please....
I am using following code:
while ($row = mysql_fetch_array($result)) {
echo "" . $row['fileshare'] . "";
}
Here is the error in the <a> tag*** Error (unexpected T_ENCAPSED_AND_WHITESPACE)*
this should work:
echo '' . $row['fileshare'] . '';
I'm really sorry I can't tell you why it happens, but if you change all of the \" to ' it should work for you.
echo "<form id='form' name='psform' action='download_logic.php' method='post'><label>Select File:&nbsp</label><select name='file' >";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['location'] . "'>" . $row['location'] . "</option>";
}
echo "</select></label>";
echo"<br>";
echo "<input id='submit' type='submit' name='filesubmit' value='Download' /> </form>";
As for the next part, you can't add a variable to a string just by writing
"variable: . $take['variable']"
If you wish to add variables to string close the writen part of the string and then add the variable, like this:
"variable: ". $take['variable']
Here is how it sould look like:
while ($row = mysql_fetch_array($result)) {
echo "<a href='download_logic.php?f=". $row['location'] . $row['fileshare'] ."'>" . $row['fileshare'] . "</a>";
}

Categories