This question already has answers here:
How to echo in PHP, HTML tags
(11 answers)
Closed 5 years ago.
I would like to place a dropdown list inside an echo as i am using it to update data. Here are the codes for php
echo "Staff Name: " . "<input type = 'text' style = 'width: 200px' name ='name' value = '$staff_name' required>";
For HTML, i know that i need to do it this way, but i do not know how to apply to php(echo)
<select name="sbranch_no">
<option value="Intelligence">Intelligence</option>
<option value="Investigation">Investigation</option>
<option value="Manpower">Manpower</option>
<option value="Operations">Operations</option>
<option value="Police National Service">Police National Service</option>
<option value="Planning and Organization Development">Planning and Organization Development</option>
<option value="Service Quality">Service Quality</option>
<option value="Support and Technical">Support and Technical</option>
<option value="Training">Training</option>
</select>
is this what you mean?
<?php
echo '<select name="sbranch_no">
<option value="Intelligence">Intelligence</option>
<option value="Investigation">Investigation</option>
<option value="Manpower">Manpower</option>
<option value="Operations">Operations</option>
<option value="Police National Service">Police National Service</option>
<option value="Planning and Organization Development">Planning and Organization Development</option>
<option value="Service Quality">Service Quality</option>
<option value="Support and Technical">Support and Technical</option>
<option value="Training">Training</option>
</select>';
?>
Or perhaps
<?php
$menu='<select name="sbranch_no">
<option value="Intelligence">Intelligence</option>
<option value="Investigation">Investigation</option>
<option value="Manpower">Manpower</option>
<option value="Operations">Operations</option>
<option value="Police National Service">Police National Service</option>
<option value="Planning and Organization Development">Planning and Organization Development</option>
<option value="Service Quality">Service Quality</option>
<option value="Support and Technical">Support and Technical</option>
<option value="Training">Training</option>
</select>';
echo "some stuff... $menu ...more stuff";
?>
Related
Hello everyone one I want to get this kind of drop down menu which has years need to be selected
For example season 2021/2022, 2020/2021, all these value should be generated from 1990 to 2022
If I understood correctly, you want to populate the select with options with all seasons until now since a specific year?
You could modify a for loop so the years appears in a descending order, and then output the option tag with the "previous year / year" as a value like this:
<div class="season-select-wrapper">
<?php
// Define variables for the select
$currentYear = 2022;
$startYear = 1990;
?>
<label for="season">Select a season</label>
<select name="season" id="season">
<?php for ($year = $currentYear; $year > $startYear; $year--) { $prevYear = $year - 1; ?>
<option value="<?php echo "{$prevYear}/{$year}"; ?>"><?php echo "{$prevYear}/{$year}"; ?></option>
<?php } ?>
</select>
</div>
This would create HTML markup like this:
<div class="season-select-wrapper">
<label for="season">Select a season</label>
<select name="season" id="season">
<option value="2021/2022">2021/2022</option>
<option value="2020/2021">2020/2021</option>
<option value="2019/2020">2019/2020</option>
<option value="2018/2019">2018/2019</option>
<option value="2017/2018">2017/2018</option>
<option value="2016/2017">2016/2017</option>
<option value="2015/2016">2015/2016</option>
<option value="2014/2015">2014/2015</option>
<option value="2013/2014">2013/2014</option>
<option value="2012/2013">2012/2013</option>
<option value="2011/2012">2011/2012</option>
<option value="2010/2011">2010/2011</option>
<option value="2009/2010">2009/2010</option>
<option value="2008/2009">2008/2009</option>
<option value="2007/2008">2007/2008</option>
<option value="2006/2007">2006/2007</option>
<option value="2005/2006">2005/2006</option>
<option value="2004/2005">2004/2005</option>
<option value="2003/2004">2003/2004</option>
<option value="2002/2003">2002/2003</option>
<option value="2001/2002">2001/2002</option>
<option value="2000/2001">2000/2001</option>
<option value="1999/2000">1999/2000</option>
<option value="1998/1999">1998/1999</option>
<option value="1997/1998">1997/1998</option>
<option value="1996/1997">1996/1997</option>
<option value="1995/1996">1995/1996</option>
<option value="1994/1995">1994/1995</option>
<option value="1993/1994">1993/1994</option>
<option value="1992/1993">1992/1993</option>
<option value="1991/1992">1991/1992</option>
<option value="1990/1991">1990/1991</option>
</select>
</div>
I want to select a target country in html.
And this code should return for example "Turkey" from my db.
<?php echo $_SESSION['user']['www']?>
I have a country list, and I use it here in settings_myinfo.php :
<select id="profile-country" name="profile_country">
<option value="Select your Country">Select your Country</option>
<?php include($root."countries_optionlist.php");?>
</select>
countries_optionlist.php :
<option value="Afganistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="American Samoa">American Samoa</option>
<option value="Andorra">Andorra</option>
<option value="Angola">Angola</option>
<option value="Anguilla">Anguilla</option>
<option value="Antigua & Barbuda">Antigua & Barbuda</option>
<option value="Argentina">Argentina</option>
<option value="Armenia">Armenia</option>
<option value="Aruba">Aruba</option>
<option value="Australia">Australia</option>
<option value="Austria">Austria</option>
<option value="Azerbaijan">Azerbaijan</option>
<option value="Bahamas">Bahamas</option>
<option value="Bahrain">Bahrain</option>
<option value="Bangladesh">Bangladesh</option>
<option value="Barbados">Barbados</option>
and more...
I want to add selected tag to target country like this:
<option value="Turkey" selected>Turkey</option>
I did some research on Google and StackOverflow but I couldn't find any solution.
Could you provide me with a suggestion on how to achieve this?
In each option, you can check if the session variable matches that country and add the selected attribute.
<?php $user_country = $_SESSION['user']['www']; ?>
<option value="Afganistan" <?php if ($user_country == 'Afghanistan') echo 'selected'; ?>>Afghanistan</option>
<option value="Albania" <?php if ($user_country == 'Albania') echo 'selected'; ?>>Albania</option>
<option value="Algeria" <?php if ($user_country == 'Algeria') echo 'selected'; ?>>Algeria</option>
...
You can simplify this by putting all the country names in an array. Then you can loop like this:
<?php
$user_country = $_SESSION['user']['www'];
foreach ($countries as $country) { ?>
<option value="<?php echo $country; ?>" <?php if ($user_country == $country) echo 'selected'; ?>>
<?php echo htmlspecialchars($country); ?>
</option>
<?php
}
i'm unable for success in how to show gender and state from below coading
please solve this.
$gender = $html->getElementById("cpBody_rbtnListGender")->getAttribute("value");
$state = $html->getElementById("cpBody_ddlState")->getAttribute("value");
this is for gender
<span id="cpBody_rbtnListGender" class="form-control pull-left radio-input"><input id="cpBody_rbtnListGender_0" type="radio" name="ctl00$cpBody$rbtnListGender" value="MALE" checked="checked" tabindex="3"><label for="cpBody_rbtnListGender_0">Male</label><input id="cpBody_rbtnListGender_1" type="radio" name="ctl00$cpBody$rbtnListGender" value="FEMALE" tabindex="3"><label for="cpBody_rbtnListGender_1">Female</label><input id="cpBody_rbtnListGender_2" type="radio" name="ctl00$cpBody$rbtnListGender" value="TRANSGENDER" tabindex="3"><label for="cpBody_rbtnListGender_2">Transgender</label><input id="cpBody_rbtnListGender_3" type="radio" name="ctl00$cpBody$rbtnListGender" value="OTHER" tabindex="3"><label for="cpBody_rbtnListGender_3">Other</label></span>
and this is for state
<div class="input-group">
<label class="control-label label-fixed">State</label>
<select name="ctl00$cpBody$ddlState" id="cpBody_ddlState" tabindex="15" class="form-control">
<option value="0">--SELECT STATE--</option>
<option value="1">ANDAMAN & NICOBAR ISLANDS</option>
<option value="2">ANDHRA PRADESH</option>
<option value="3">ARUNACHAL PRADESH</option>
<option value="4">ASSAM</option>
<option value="5">BIHAR</option>
<option value="6">CHANDIGARH</option>
<option value="7">CHHATTISGARH</option>
<option value="8">DADRA & NAGAR HAVELI</option>
<option value="9">DAMAN & DIU</option>
<option value="10">DELHI</option>
<option value="11">GOA</option>
<option value="12">GUJARAT</option>
<option value="13">HARYANA</option>
<option value="14">HIMACHAL PRADESH</option>
<option value="15">JAMMU AND KASHMIR</option>
<option value="16">JHARKHAND</option>
<option value="17">KARNATAKA</option>
<option value="18">KERALA</option>
<option value="19">LAKSHADWEEP</option>
<option value="20">MADHYA PRADESH</option>
<option value="21">MAHARASHTRA</option>
<option value="22">MANIPUR</option>
<option value="23">MEGHALAYA</option>
<option value="24">MIZORAM</option>
<option value="25">NAGALAND</option>
<option value="26">ORISSA</option>
<option value="27">PONDICHERRY</option>
<option value="28">PUNJAB</option>
<option selected="selected" value="29">RAJASTHAN</option>
<option value="30">SIKKIM</option>
<option value="31">TAMIL NADU</option>
<option value="32">TELANGANA</option>
<option value="33">TRIPURA</option>
<option value="34">UTTAR PRADESH</option>
<option value="35">UTTARAKHAND</option>
<option value="36">WEST BENGAL</option>
</select>
</div>
state and gender is not showing using getAttribute("value");
You could make use of find.
One option to get the state could be to use the id and get the selected option #cpBody_ddlState option[selected]
$state = $html->find('#cpBody_ddlState option[selected]', 0)->plaintext;
echo $state; //RAJASTHAN
To get the selected radio button you might use #cpBody_rbtnListGender input and loop though the items until the attribute checked matches:
foreach ($html->find('#cpBody_rbtnListGender input') as $input) {
if ($input->hasAttribute("checked")) {
echo $input->getAttribute("value"); // MALE
}
}
idk why you're doing this with php.'
in javascript u can create a p to pass the value to it:
<body onload="pageloaded()">
<p id="display"></p>
in javascript:
var p = document.getElementById("display");
var value = document.getElementById("cpBody_rbtnListGender").value;
function pageloaded(){
p.innerHTML = value;
}
u can do the same for ur other needs
I have setup a function with jQuery that hides and un-hides related select's after a has changed the first one. The problem is that if a user chooses one value and then changes it to another value in a different relates select then the first one he chose stays selected and they both get submitted with the form.
How can I reset the hidden select values if the user changes his mind?
Here's my code:
/*Setup jQuery Function to hide/unhide selects */
<script>
$(document).ready(function(){
var $topSelect = $('select[name="dropdownmain"]');
var $nestedSelects = $('select[name!="dropdownmain"]');
showApplicableSelect();
$topSelect.change(showApplicableSelect);
function showApplicableSelect() {
$nestedSelects.hide();
$('select[name="' + $topSelect.val() + '"]').show();
}
});
</script>
/* Example of the select items and how they are related */
<select class=" form-textbox validate[required]" onChange="change()" name="dropdownmain" id="" title=""> <option selected="selected" value="PLEASE SELECT">PLEASE SELECT</option>
<option value="1">Accommodation and Food Services</option>
<option value="2">Administrative / Support / Waste Management / Remediation Services</option>
<option value="3">Agriculture / Forestry / Fishing / Hunting</option>
<option value="4">Arts / Entertainment / Recreation</option>
<option value="5">Automotive</option>
<option value="6">Construction</option>
<option value="7">Educational Services</option>
<option value="8">Finance and Insurance</option>
<option value="9">Health Care and Social Assistance</option>
<option value="10">Information</option>
<option value="11">Manufacturing</option>
<option value="12">Other</option>
<option value="13">Other Services</option>
<option value="14">Professional / Scientific and Technical Services</option>
<option value="15">Real Estate and Rental and Leasing</option>
<option value="16">Retail Trade</option>
<option value="17">Transportation and Warehousing</option>
</select>
<select class=" form-textbox validate[required]" name="PLEASE SELECT" id="2" title=""> <option value=""> </option> </select>
<select class=' form-textbox validate[required]' name='1' id='' title=''> <option selected='selected' value='PLEASE SELECT'>PLEASE SELECT</option>
<option value='1'>Bakery / Cafes</option>
<option value='2'>Bed and Breakfast Inns</option>
<option value='3'>Carryout restaurants</option>
<option value='4'>Casual Dining Restaurant $$-</option>
<option value='5'>Coffee Shop</option>
<option value='6'>Drinking Places (Alcoholic Beverages)</option>
<option value='7'>Fast-food Restaurants</option>
<option value='8'>Fine Dining Restaurant $$$+</option>
<option value='9'>Full Service Restaurant</option>
<option value='10'>Hotels and Motels</option>
<option value='11'>Other food related services</option>
<option value='12'>Travel Agencies</option>
<option value='27'>Other</option>
</select>
Use jQuery to
reset selected property and
force value to initial value
Code:
$nestedSelects
.prop("selected", false)
.val("-1") // or .val("PLEASE SELECT")
.hide();
I have updated your HTML dropdown initial value from "PLEASE SELECT" to -1.
<option selected='selected' value='-1'>PLEASE SELECT</option>
jsfiddle
Tested form in w3schools :)
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I'm doing a project for my school and I wrote this page but for some reason I get 2 warnings in the end about header info already sent on line 132 but on line 132 all I have is the php tag
I would really appreciate it if I can get some help, I wrote this code in 1 hour but trying to solve this problem for the past 2 hours ...
Finally got tired and decided to ask it
here is my code
<html>
<body>
<form method="POST">
Browse By Author's name starting with :<select name="author">
<option value="">--Select--</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
<option value="F">F</option>
<option value="G">G</option>
<option value="H">H</option>
<option value="I">I</option>
<option value="J">J</option>
<option value="K">K</option>
<option value="L">L</option>
<option value="M">M</option>
<option value="N">N</option>
<option value="O">O</option>
<option value="P">P</option>
<option value="Q">Q</option>
<option value="R">R</option>
<option value="S">S</option>
<option value="T">T</option>
<option value="U">U</option>
<option value="V">V</option>
<option value="W">W</option>
<option value="X">X</option>
<option value="Y">Y</option>
<option value="Z">Z</option>
</select><br><br>
Browse by Title start with :<select name="title">
<option value="">--Select--</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
<option value="F">F</option>
<option value="G">G</option>
<option value="H">H</option>
<option value="I">I</option>
<option value="J">J</option>
<option value="K">K</option>
<option value="L">L</option>
<option value="M">M</option>
<option value="N">N</option>
<option value="O">O</option>
<option value="P">P</option>
<option value="Q">Q</option>
<option value="R">R</option>
<option value="S">S</option>
<option value="T">T</option>
<option value="U">U</option>
<option value="V">V</option>
<option value="W">W</option>
<option value="X">X</option>
<option value="Y">Y</option>
<option value="Z">Z</option>
</select><br><br>
Browse by Publisher start with :<select name="publisher">
<option value="">--Select--</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
<option value="F">F</option>
<option value="G">G</option>
<option value="H">H</option>
<option value="I">I</option>
<option value="J">J</option>
<option value="K">K</option>
<option value="L">L</option>
<option value="M">M</option>
<option value="N">N</option>
<option value="O">O</option>
<option value="P">P</option>
<option value="Q">Q</option>
<option value="R">R</option>
<option value="S">S</option>
<option value="T">T</option>
<option value="U">U</option>
<option value="V">V</option>
<option value="W">W</option>
<option value="X">X</option>
<option value="Y">Y</option>
<option value="Z">Z</option>
</select><br><br>
Browse by Genre:<select name="genre">
<option value="">--Select--</option>
<option value="fiction">Fiction</option>
<option value="non-fiction">non-fiction</option>
<option value="sciencefiction">science fiction</option>
</select><br><br>
Book Award: <select name="bookaward">
<option value="">--Select--</option>
<option value="1">Arthur Ellis Award</option>
<option value="2">Booker Prize</option>
<option value="3">Canadian Jewish Book Awards</option>
<option value="4">Commonwealth Writers Prize</option>
<option value="5">Dayne Ogilvie Prize</option>
<option value="6">Edna Staebler Award</option>
<option value="7">Geoffrey Bilson Award</option>
<option value="8">Gerald Lampert Award</option>
<option value="9">Griffin Poetry Prize</option>
<option value="10">Governor General's Award</option>
<option value="11">Commonwealth Writers Prize</option>
<option value="12">Journey Prize</option>
<option value="13">Lorne Pierce Medal</option>
<option value="14">Wright Awards</option>
<option value="15">Milton Acorn People's Poetry Award</option>
<option value="16">Matt Cohen Award: In Celebration of a Writing Life</option>
<option value="17">Norma Fleck Award</option>
<option value="18">RBC Bronwen Wallace Award for Emerging Writers</option>
<option value="19">Pat Lowther Award</option>
<option value="20">Rogers Writers' Trust Fiction Prize</option>
<option value="21"> Wright Awards</option>
<option value="22">Writers' Trust Distinguished Contribution Award</option>
<option value="23">Writers' Trust Engel/Findley Award</option>
<option value="24">Winterset Award</option>
<option value="25">Hilary Weston Writers' Trust Prize for Nonfiction</option>
<option value="26">Geoffrey Bilson Award</option>
</select><br><br>
<input type="submit" name="submit" value="Browse">
</form>
<?php
error_reporting(E_ERROR|E_WARNING);
$con = mysql_connect("localhost", "root", "");
if (!$con) {
die('Connection Failed: ' . mysql_error());
}
if (mysql_select_db("elibrary",$con)){
if ($_POST['submit']){
$author = $_POST['author'];
$title = $_POST['title'];
$genre = $_POST['genre'];
$publisher= $_POST['publisher'];
$award= $_POST['bookaward'];
if ($author != "") {
$author1 = "author";
$like1 = "like";
$and1 = "and";
}
if ($title != "") {
$title1= "title";
$like2 = "like";
$and2 = "and";
}
if ($genre != "") {
$genre1="genre";
$like3 = "like";
$and3 = "and";
}
if ($publisher != "") {
$publisher1= "publisher";
$like4 = "like";
$and4 = "and";
}
if ($award != "") {
$award1= "bookawards.id";
$like5 = "=";
$and5 = "and";
}
$browse = ("select * from books,bookawards,bookrelations where books.id = bookrelations.bookid and bookawards.id = bookrelations.bookawardsid '".$and1."' '".$author1."' '".$like1."' '".$author."%' '".$and2."' '".$title1."' '".$like2."' '".$title."%' '".$and3."' '".$genre1."' '".$like3."' '".$genre."%' '".$and4."' '".$publisher1."' '".$like4."' '".$publisher."' '".$and5."' '".$award1."''".$like5."''".$award."'");
setcookie("browse",$browse);
header("Location: shop.php");
exit();
}
}
mysql_close();
?>
</body>
</html>
You need to move your PHP code to top of the page (before HTML).
For more detailed answer:
https://stackoverflow.com/a/8028987/1724762
header() will not work after you have already output so much HTML. It has to be called before any output is sent to browser
According to PHP Manual
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
When you write to the output with echo or any function, PHP has already sent the response body.
Which means that you cannot send headers after.
The same apply for html, the html will be displayed and the output has already started so you cannot call header() after.
The same also apply to closing tags ?>. If you have any single space after the closing tag, it will be ignored by the parser (because outside of the closing tag) and the output will start.
So the best solution is to ommit the closing tags, because the parser doesn't need them.
It's like if you were sending an email and add the address after.