Good day every one, i am trying to check if a radio button were clicked, and iwant the value of that clicked radio button to pass on a variable, i will used that variable to compare records in database with the same values from it. and display all records on list box??
but when i try to run this code nothings happen
<td><input type="radio" name="1stChoice" value="TESDA" ></td><br>
<td align = "center">
<select name="course1">
<?php
include('dbconnection.php');
if(isset($_POST['1stChoice'])) {
if($_POST['1stChoice'] == 'TESDA') {
$choose='TESDA';
} elseif($_POST['1stChoice'] == 'CHED') {
$choose='CHED';
}
}
$mysql_select=mysql_query("select * from courses where Institution = '$choose' ",$mysql);
while($row=mysql_fetch_array($mysql_select))
{
?>
<option><?php $row['Program']; ?></option></td>
</select>
<?php } ?>
</tr>
<td width="20%">2nd choice:</td>
<td><input type="radio" name="2ndChoice" value="CHED" ></td>
<td><input type="radio" name="2ndChoice" value="TESDA" ></td><br>
<td align = "center">
<select name="course2">
<?php
include('dbconnection.php');
if(isset($_POST['2ndChoice'])) {
if($_POST['2ndChoice'] == 'TESDA') {
$choose='TESDA';
} elseif($_POST['2ndChoice'] == 'CHED') {
$choose='CHED';
}
}
$mysql_select=mysql_query("select * from courses where Institution = '$choose' ",$mysql);
while($row=mysql_fetch_array($mysql_select))
{
?>
<option><?php $row['Program']; ?></option></td>
</select>
<?php } ?>
I tested your code and as far as I can see, you're not echoing your <?php $row['Program']; ?> which should read as <?php echo $row['Program']; ?>
This is for both your <option> tags.
I also didn't notice any form tags <form></form>, so you will need to add those if you're not presently using them.
Using a submit button could be useful also. Although I'm not sure if you're using JS/jQuery with your code.
<input type="submit" name="submit" value="Submit">
Here is what I used to test it with, along with a few additions/modifications:
(I added form tags, a submit button and the echo for the <select> tags)
<form action="" method="post">
<td><input type="radio" name="1stChoice" value="TESDA" ></td><br>
<input type="submit" name="submit" value="Submit">
<td align = "center">
<select name="course1">
<?php
include('dbconnection.php');
if(isset($_POST['1stChoice'])) {
if($_POST['1stChoice'] == 'TESDA') {
$choose='TESDA';
} elseif($_POST['1stChoice'] == 'CHED') {
$choose='CHED';
}
}
$mysql_select=mysql_query("select * from courses where Institution = '$choose' ",$mysql);
while($row=mysql_fetch_array($mysql_select))
{
?>
<option><?php echo $row['Program']; ?></option></td>
</select>
<?php } ?>
</tr>
<td width="20%">2nd choice:</td>
<td><input type="radio" name="2ndChoice" value="CHED" ></td>
<td><input type="radio" name="2ndChoice" value="TESDA" ></td><br>
<td align = "center">
<select name="course2">
<?php
include('dbconnection.php');
if(isset($_POST['2ndChoice'])) {
if($_POST['2ndChoice'] == 'TESDA') {
$choose='TESDA';
} elseif($_POST['2ndChoice'] == 'CHED') {
$choose='CHED';
}
}
$mysql_select=mysql_query("select * from courses where Institution = '$choose' ",$mysql);
while($row=mysql_fetch_array($mysql_select))
{
?>
<option><?php echo $row['Program']; ?></option></td>
</select>
</form>
<?php } ?>
This is not a php work. IF your html is good, your browser will check the checked radio button. See! In your code, name must start with a letter or a _(underscore).
So, replace all name='2ndChoice' with
<td><input type="radio" name="ck_2ndChoice" value="CHED" ></td>
<td><input type="radio" name="ck_2ndChoice" value="TESDA" ></td><br>
and all name='1stChoice' with name='ck_1stChoice'
Related
I have a plenty of list of writers and publishers which are to be inserted in mysql through PHP, but every time I have to select between "Add Writer" and "Add Publisher" manually after inserting every writer/publisher details.
I want the selected values doesn't get changed automatically on inserting values in Mysql, (until it changed by user) i.e., selected option remains same (chosen by user) even after inserting the values, until changed by user.
index.php
<?php
include "dbConfig.php";
/*Adding Writer*/
if(isset($_POST['addwrt']))
{
$writerid=$_POST['writerid'];
$wname=$_POST['wname'];
$wcity=$_POST['wcity'];
$resultw=mysqli_query($db,"insert into wdetails(writerid,wname,wdesg,salutation,wcity)values('$writerid','$wname','$wdesg','$salut','$wcity')");
}
/*Adding Publisher*/
else if(isset($_POST['addpubl']))
{
$publisherid=$_POST['publisherid'];
$pname=$_POST['pname'];
$pcity=$_POST['pcity'];
$resultp=mysqli_query($db,"insert into pdetails(publisherid,pname,pdesg,pcity)values('$publisherid','$pname','$pdesg','$pcity')");
}
?>
<body>
<select id="stf">
<option value="cf" selected="selected" />Select the Field
<option value="addwriter" />Add Writer
<option value="addpublisher" />Add Publisher
</select>
<form method="post" name="libraryAdd">
<div id="addwriter" style="display:none">
<table>
<tr>
<td>Writer ID<span style="color:red"><b>*</b></span></td>
<td>W <input type='text' name="writerid"></td>
</tr>
<tr>
<td>Writer Name<span style="color:red"><b>*</b></span></td>
<td><input type="text" name="wname" /></td>
</tr>
<tr>
<td>Writer City<span style="color:red"><b>*</b></span></td>
<td><input type="text" name="wcity" /></td>
<td><input name="addwrt" type="submit" value="ADD Data"></td>
</tr>
</table>
</div>
<div id="addpublisher" style="display:none">
<table>
<tr>
<td>Publisher ID<span style="color:red"><b>*</b></span></td>
<td>P <input type='text' name="publisherid"></td>
</tr>
<tr>
<td>Publisher Name<span style="color:red"><b>*</b></span></td>
<td><input type="text" name="pname" /></td>
</tr>
<tr>
<td>Publisher City<span style="color:red"><b>*</b></span></td>
<td><input type="text" name="pcity" /></td>
<td><input name="addpubl" type="submit" value="ADD Data"></td>
</tr>
</table>
</div>
</form>
</body>
.js file linked to the index.php
$(document).ready(function() {
$('#stf').change(function(){
var value=$(this).val();
if(value === "addwriter"){
$("#addwriter").show('slow');
$("#addpublisher").hide('slow');
}
else if(value === "addpublisher"){
$("#addpublisher").show('slow');
$("#addwriter").hide('slow');
}
else {
$("#addwriter").hide('slow');
$("#addpublisher").hide('slow');
}
});
});
dbConfig.php
<?php
//Database credentials
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'library';
//Connect and select the database
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
?>
The reason this happens is that in your select you have:
<option value="cf" selected="selected" />Select the Field
which means that the select will always start with "Select the Field" selected.
If you want the last selected option to remain selected after form submission, you should change the select code to:
<option value="cf" <?php if (!isset($_POST['addwrt']) && !isset($_POST['addpubl'])) echo 'selected="selected"'; ?> >Select the Field
<option value="addwriter" <?php if (isset($_POST['addwrt'])) echo 'selected="selected"'; ?> >Add Writer
<option value="addpublisher" <?php if (isset($_POST['addpubl'])) echo 'selected="selected"'; ?> >Add Publisher
You will also need to make the following changes to ensure the appropriate input boxes appear. Change
<div id="addwriter" style="display:none">
<div id="addpublisher" style="display:none">
to:
<div id="addwriter" <?php if (!isset($_POST['addwrt'])) echo 'style="display:none"'; ?> >
<div id="addpublisher" <?php if (!isset($_POST['addpubl'])) echo 'style="display:none"'; ?> >
I have a form for input order, there are 2 fields, branch and customer model. Before we click button "search", we must select branch and customer fields. After I filled the field, then click button search, the data appears. The problem is, after the data appears, where the condition branch and customer models are filled, I want to create another search, then I changed branch field only (customer model field was same with the previous search), change branch field from 'A' to 'B' and click button search, the data not appears / empty (whereas the data exists in database). Branch field,the field when I changed into B, after I click button search, the branch filled changed into
'SELECT' (branch option are SELECT -> A , B).
I'm confused with this problem.. here is the code :
<?php
if ($_POST)
{
$_SESSION['data_id'] = $_POST['data_id'];
$_SESSION['branch'] = $_POST['branch'];
$_SESSION['customer_model'] = $_POST['customer_model'];
}
?>
<form id="search" class="formular" method="post" action="search_data.php" style="z-index:1;">
<table width="100%">
<tr>
<td>Data ID :</td>
<td><input type="text" name="data_id" value="<?php echo $_SESSION['data_id'];?>"></td>
</tr>
<tr>
<?php
$branch = "'".str_replace(",", "','", $_SESSION['moi_status_UserBranch'])."'";
$check_data = $db->GetAll("select cab_id, cabang_name from company_profile where cab_id in(".$branch.")");
?>
<td>Branch :</td>
<td>
<select id="branch" name="branch">
<option value="0">Select</option>
<?php foreach($check_data as $row): ?>
<option <?php echo ($_SESSION['branch'] == $row['CAB_ID']) ? "selected" : ""; ?> value="<?php echo $row['CAB_ID']; ?>"><?php echo $row['CABANG_NAME']; ?></option>
<?php endForeach; ?>
</select>
</td>
</tr>
<tr>
<td width="110">Customer Model</td>
<td>
<select id="customer_model" name="customer_model" class="readonly validate[required]">
<option <?php echo ($_SESSION['customer_model'] == '0') ? "selected" : ""; ?> value="0">Pilih</option>
<option <?php echo ($_SESSION['customer_model'] == 'I') ? "selected" : ""; ?> value="I">Personal</option>
<option <?php echo ($_SESSION['customer_model'] == 'C') ? "selected" : ""; ?> value="C">Corporate</option>
</select>
</td>
</tr>
</table>
<input type="submit" id="btnsubmits" name="btnsubmit" class="button" value="Search" />
<a onclick="mypopup();" style="text-decoration:underline; cursor:pointer; font-weight:bolder">Add New</a>
</form>
<script>
function mypopup()
{
<?php unset($_SESSION['regional_saved']); ?>
mywindow = window.open("choose_custtype.php" ,"Item","width=700px,height=350px,resizable=1,scrollbars=1,top=150,left=200");
mywindow.moveTo(200, 200);
}
Im trying to display my database value into the textbox using drop down menu. which is i did and it is displaying. the problem here is that when i choose an item in the drop down list, it goes back to the first choice or last choice, the explanation i got was, my loop is selecting all of the items in the field causing the drop down menu to go back to the first choice when i click on other items. can you help me with the code on how to stop going back to the first choice when i select other options. Here is my whole code. i also use functions.
home.php
<?php
session_start();
include('dbconnect.php');
include('functions.php');
if(isset($_POST['brandname'])){
$id = $_POST['brandname'];
$result = mysql_query("SELECT * FROM tblstore WHERE brandname = '$id'");
while($row = mysql_fetch_array($result)){
$price = $row['price'];
$stocks = $row['stocks'];
}
}
?>
<html>
<body>
<form method="POST" name="">
<table align="center">
<tr>
<td>Choose here:</td>
<td>
<select name = "brandname" onchange = "this.form.submit()">
<?php dropdown() ?>
</select>
</td>
</tr>
<tr>
<td>Quantity:</td>
<td><input type="text" name="qty" id="qty" value="" /></td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price" id="price" value="<?php echo $price ?>" disabled/></td>
</tr>
<tr>
<td>Stocks:</td>
<td><input type="text" name="stocks" id="stocks" value="<?php echo $stocks ?>" disabled/></td>
</tr>
<tr>
<td>Total:</td>
<td><input type="text" name="total" id="total" disabled/></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
<div align = "center">
hi' <?php echo $userRow['username']; ?> Sign Out
</div>
</body>
</html>
functions.php
<?php
function dropdown(){
$all = mysql_query("SELECT * FROM tblstore");
while($row = mysql_fetch_array($all)){
echo "<option value = '".$row['brandname']."' selected='selected'>" .$row['brandname'] . "</option>";
}
}
feel free to edit the whole code.. im a beginner in php and learning my way to it. thanks
Can add the multiple option if you need to select multiple
<select name="brandname" multiple>
<option value="Select">Select</option>
<?php
do {
?>
<option value="<?php echo $row['brandname']?>"> <?php echo $row['brandname'] ?></option>
<?php
} while ($row = mysql_fetch_assoc($all));
?>
</select>
I'm trying to create a changing drop down list in php, but without a database. Here's what I came up with so far
<body>
<form method = "post">
<table>
<tr>
<td>
<p>Name</p>
</td>
<td><input type="text" name="Name" /></td>
</tr>
<tr>
<td><p>State</p>
</td>
<td>
<select name = "State">
<option value = "PA"> Pennsylvania</option>
<option value = "CA"> California</option>
<option value = "AZ"> Arizona</option>
<option value = "NY"> New York</option>
<option value = "FL"> Florida</option>
</select>
</td>
</tr>
<td><input type="submit" name = "formSubmit" value="Submit" /></td>
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['formSubmit']))
{
if(empty($_POST['Name']))
{
echo("You forgot your name");
}
else
{
}
}
?>
</body>
I had planned to use the 'else' statement to generate my second drop down list, but so far nothing I've tried has worked. I've looked all over for ideas, but most of the information I've come across has dealt with databases. This isn't a database, it's a single PHP program. Maybe I have the wrong idea of how this works. Should I try calling a function that creates the form ahead of time or am I completely off base for what I'm attempting?
To do so you need javascript
There is already a question posted related to yours
javascript-dynamic-drop-down-box-update
jQuery would give you a true dynamic result without having to reload the page. However if you want to use PHP to add what is in the textbox to the list of values then you can use PHP session as an array to hold the values like this.
<?php
session_start();
if(isset($_POST['formSubmit']))
{
if(empty($_POST['Name']))
{
echo("You forgot your name");
}
else
{
$name = $_POST['Name'];
if(isset($_SESSION['List']))
{
$index = count($_SESSION['List']);
$_SESSION['List'][$index+1] = $name;
}
else
{
$_SESSION['List'][0] = $name;
}
}
}
?>
<body>
<form method = "post">
<table>
<tr>
<td>
<p>Name</p>
</td>
<td><input type="text" name="Name" /></td>
</tr>
<tr>
<td><p>State</p>
</td>
<td>
<select name = "State">
<option value = "PA"> Pennsylvania</option>
<option value = "CA"> California</option>
<option value = "AZ"> Arizona</option>
<option value = "NY"> New York</option>
<option value = "FL"> Florida</option>
<?php
if(isset($_SESSION['List']))
{
foreach($_SESSION['List'] as $name)
{
echo '<option value="'.$name.'">'.$name.'</option>';
}
}
?>
</select>
</td>
</tr>
<td><input type="submit" name = "formSubmit" value="Submit" /></td>
</td>
</tr>
</table>
</form>
</body>
I've a doubt. I've 3 textboxes and each is having checkboxes next to it. I want to display
the values of only those textboxes whose respective checkboxes are clicked. Following is the attached HTML and PHP codes:
<html>
<head>
</head>
<body>
<form name="f" method="post" action="4.php">
<table>
<tr>
<th> Facility </th>
</tr>
<tr>
<td><input type="text" name="a1" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Hostel"></td>
</tr>
<tr>
<td><input type="text" name="b1" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Transport"></td>
</tr>
<tr>
<td><input type="text" name="c1" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Food"></td>
</tr>
<tr>
<td colspan="3"><input type="submit" value="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
and below is the PHP part.
<?php
$a=$_POST['a1'];
$b=$_POST['b1'];
$c=$_POST['c1'];
$facilityArray = $_POST['facility'];
$facility = "";
if(count($facilityArray) > 0)
{
foreach($facilityArray as $fac)
{
$facility .= " " . $fac;
}
}
echo $facility; echo "<br>";
echo $a; echo "<br>";
echo $b; echo "<br>";
echo $c;
?>
With the help of following codes I am able to display all the values of checked checkboxes. I am also able to display the values of all the textboxes. But I actually want to display the values of only those textboxes whose respective checkboxes are clicked. I know it may be a very basic question but please help me grow in PHP. Thanks in advance... :(
Your textboxes should also be in an array post to achieve this.
To achieve this change the input lines as:
<td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
From php you'll be getting the posted textboxes in an array as:
$textbox=$_POST['textboxes'];
You should then loop through the checkboxes array and if the corresponding checkbox is "on" (clicked), then display the textboxes value. To do this you would also need a counter to make sure you are on the same array index for both checkboxes and textboxes:
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
I've also added a name to your submit button so you only check the form when it is submitted.
Your page should now look something like this:
<?php
if(isset($_POST['submit']))
{
$textbox=$_POST['textboxes'];
$facilityArray = $_POST['facility'];
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
}
?>
<form name="f" method="post" action="4.php">
<table>
<tr>
<th> Facility </th>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td colspan="3"><input name="submit" type="submit" value="submit" /></td>
</tr>
</table>
</form>
UPDATE:
To make sure that the $_POST variable exists before assigning it to a variable we use the isset(). In your case just update the php segment as:
<?php
if(isset($_POST['submit']))
{
if(isset($_POST['textboxes']))
{
$textbox=$_POST['textboxes'];
if(isset($_POST['facility']))
{
$facilityArray = $_POST['facility'];
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
}
}
}
?>
Where the only changes are the addition of another two if statements that take a boolean flag from the isset() function according to whether the $_POST variable has been posted successfully
if(isset($_POST['textboxes']))
AND
if(isset($_POST['facility']))