combine option field results to text field - php

I tried searching this and it is probably a really simple solution but I can't figure out how to combine an option field with a text field in html/php/js. Basically I just need the user to be able to select whether it is http or https and then type in the domain. It should then be submitted through _GET as a single variable.
<select>
<option value="http://">http://</option>
<option value="https://">https://</option>
</select>
<input class="span4" id="domain" name="domain" type="text">
<button class="btn btn-success" type="submit">Submit</button></form>
Any input would be appreciated.

I would combine them AFTER the submit...
HTML Page (You need to name your SELECT):
<select name="type">
<option value="http://">http://</option>
<option value="https://">https://</option>
</select>
<input class="span4" id="domain" name="domain" type="text">
<button class="btn btn-success" type="submit">Submit</button></form>
PHP Page:
$type = $_GET['type'];
$name = $_GET['domain'];
$combined = $type . $name;
Assuming you really "meant"
submitted through _GET as a single variable
You would end up with ie. https:// as $type and www.example.com as $name ... then the variable $combined should have a value of https://www.example.com

Select needs a name.
<form>
<select name="protocol">
<option value="http://">http://</option>
<option value="https://">https://</option>
</select>
<input class="span4" id="domain" name="domain" type="text">
<button class="btn btn-success" type="submit">Submit</button>
</form>

Related

Cannot echo variables after form submittiom

So I am submitting a form to the same page. I have used the following code to check whether the form was submitted, and to avoid 'undefined variables' errors during the first page reload.
if (isset($_POST['submit'])){
// Get search variables
$pName = $_POST['pname'];
$pLocation = $_POST['plocation'];
$pPrice = $_POST['pprice'];
if (isset($_POST['ptype'])){
$pType = $_POST['ptype'];
}
echo "pType";
}
However, I cannot echo the php variables onto the page. I am guessing this is because the form is refreshed when it is sent by post, so the variables are lost.
How can I fix this problem?
This is the html form
<form method="post" action="../html/searchpage.php">
<div id="searchborder">
<input type="text" id="pname" name="pname" placeholder=" Property name">
<input type="text" id="plocation" name="plocation" placeholder=" Property location">
<input type="number" id="pprice" name="pprice" placeholder=" Property price">
<div id="ptypeholder">
<div id="ptypebox">
<select name="ptype">
<option value="" disabled selected>Post type</option>
<option value="buy">Buy</option>
<option value="rent">Rent</option>
</select>
</div>
</div>
<button type="submit"><img src="../images/search.png"></button>
</div>
</form>
I am trying to display 'property posts' into html cards from a database using php in this page. The form is the search bar for the property posts.
Thanks!
I don't think $_POST['submit'] is being set because the button needs a name ie
<input type="submit" name="submit" value="submit">
also you have a few other syntax errors, structure, may be this will help :-
<form method="post" action="searchpage.php">
<input type="text" id="pname" name="pname" placeholder="Property name">
<input type="text" id="plocation" name="plocation" placeholder="Property location">
<input type="number" id="pprice" name="pprice" placeholder="Property price">
<select name="ptype">
<option value="" disabled selected>Post type</option>
<option value="buy">Buy</option>
<option value="rent">Rent</option>
</select>
<input type="submit" name="submit" value="submit">
and you don't HAVE to assign the post values to $vars you can use them directly -
<?php
if (isset($_POST['submit'])){
// Get variables
//$pName = $_POST['pname'];
//$pLocation = $_POST['plocation'];
//$pPrice = $_POST['pprice'];
echo $_POST['ptype'].'<br>';
echo $_POST['pname'].'<br>';
echo $_POST['plocation'].'<br>';
echo $_POST['pprice'].'<br>';
}
?>
Needs more work to make it check all posts are set or validation but works as a basic starting point
Also in original you posted echo "pType"; will only echo the text pType not the value

query data from database base on option value

i want to fetch data from database into HTML table base on below option value
<form method="POST">
<label>Status</label>
<select class="form-control" name="selection_list">
<option value="">All</option>
<option value="A">A: Not Processed</option>
<option value="B">B: Partially Processed</option>
<option value="C">C: Completely Processed</option>
</select>
<input name="submit" type="submit" class="btn btn-success btn-sm" value="Generate">
</form>
if i select "All", php will select data from table where status = "All"
if i select "A: Not Processed", php will select data from table where status = "A"
...so on
how to writing code in HTML & PHP? please help....
use an id to select field:
<form method="POST">
<label>Status</label>
<select class="form-control" name="selection_list" id="list_id">
<option value="">All</option>
<option value="A">A: Not Processed</option>
<option value="B">B: Partially Processed</option>
<option value="C">C: Completely Processed</option>
</select>
<input name="submit" type="submit" class="btn btn-success btn-sm" value="Generate">
</form>
<?php
if($_POST['submit'])
{
$list_id=$_POST['list_id'];
//write the select query you need to process with WHERE status=$list_id;
}
?>
This is pretty basic stuff.
Official already answered it, but instead of id, you'll have to use the name attribute.
Not to mention that there are security issues with this method.

how to merge form data passed by get method with current get string?

For example I am at url like this:
localhost/somewhat/?action=active
And I have html form:
<form>
<div class="navbar-form navbar-right" name="x1">
<select class="form-control" name="limit">
<option selected value="10">10</option>
<option value="20">20</option>
<option value="50">50</option>
</select>
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
On submit I recive url like
localhost/somewhat/?action=active
But I want to recieve this url in format:
localhost/somewhat/?action=active&limit=20;
I have an idea to do this with hidden inputs and foreach get element do echo of these hidden inputs. But it's a wrong way because $_GET['key'] can be array data also..
So, how to be?
<input type="hidden" name="get_key[array_inside_get_key][]" value="123"/>,
solution is here: https://gist.github.com/eric1234/5802030

foreach loop doesn't work properly

I really don't know what is wrong with this script.. I really thought everything was right, but somehow my strpos syntaxs doesn't work properly or something. The $_POST['category'] is a select list with multiple selections permitted. So that's why I put it in an array, but maybe it is incorrect?
$cat_array = $_POST['category'];
foreach($cat_array as $key => $value )
{
if(strpos($value, 'n_') !== false)
{
// Do something about the new categories.
} else {
// work with existing categories
}
}
the html - I also have a jquery that handles the add category fields. The n_(number)-(value) is created by jquery.
<div>
<label for="category">Category</label>
<select name="category" size="10" multiple="MULTIPLE">
<option class="cat_1" value="1">Cars</option>
<option class="cat_2" value="2">Lego</option>
<option class="cat_3" value="3">Country</option>
<option class="cat_4" value="4">School</option>
<option class="cat_5" value="5">Cooking</option>
<option class="cat_6" value="n_6-test">test</option>
<option class="cat_7" value="n_7-Buuh">Buuh</option>
</select> <br>
<input type="text" name="new_cat" value="" size="40" maxlength="120" placeholder="Category Name"><input class="plus" name="" type="button" value="Add Category">
<p class="plus_comment"></p>
</div>
Your $_POST['category'] is not an array. After seeing your var_dump result, it must be a string only. If it as an array, the var_dump should display like this,
array (size=1)
1 => string '5' (length=1)
So, check your HTML code whether it passes an array.
EDIT: Change the category to category[]
try to your select list name
category[]
To make your category actually an array you have to append brackets to the category like this: category[]
So your HTML woul look like this
<div>
<label for="category">Category</label>
<select name="category[]" size="10" multiple="MULTIPLE">
<option class="cat_1" value="1">Cars</option>
<option class="cat_2" value="2">Lego</option>
<option class="cat_3" value="3">Country</option>
<option class="cat_4" value="4">School</option>
<option class="cat_5" value="5">Cooking</option>
<option class="cat_6" value="n_6-test">test</option>
<option class="cat_7" value="n_7-Buuh">Buuh</option>
</select> <br>
<input type="text" name="new_cat" value="" size="40" maxlength="120" placeholder="Category Name">
<input class="plus" name="" type="button" value="Add Category">
<p class="plus_comment"></p>
</div>

Passing two variables via GET to PHP using HTML form

I'm trying to pass two variables (a search query (String) and a column name (also a String)) via a HTML form. The first string is entered into a text box and the second is selected from a drop-down menu.
Obviously this method doesn't work:
<h3>Search for a Customer</h3>
<form action="search.php" method="get">
<input type="text" id="sString">
<select name="sField">
<option value="Name">Name</option>
<option value="HostID">Host ID</option>
<option value="OrderNumber">Order Number</option>
<option value="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
You need to give your <input> field a name in order for this to work. Unnamed inputs won't get passed through the form post.
You don't have a 'name' parameter on your sString input boxes. The name parameter is what gets sent back to the server, not the field's ID:
<input type="text" id="sString" name="sString">
and then in PHP
$search = $_GET['sString'];
You need to use attribute name instead of value.
<h3>Search for a Customer</h3>
<form action="search.php" method="get">
<input type="text" id="sString" name="sString"/>
<select name="sField">
<option name="Name">Name</option>
<option name="HostID">Host ID</option>
<option name="OrderNumber">Order Number</option>
<option name="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
Try:
<form action="search.php" method="get">
<input type="text" name="sString" id="sString" value="">
<select name="sField">
<option value="Name">Name</option>
<option value="HostID">Host ID</option>
<option value="OrderNumber">Order Number</option>
<option value="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
You should be able to access form entered variables by $_GET array in search.php. To see what is going on try var_dump($_GET);

Categories