I currently have a drop down box than when one of the options is selected it will echo-
"Your Favourite Car Is (option)
What I need to do now is change this so its a text box but the user can only type in one of the options within the array and if another one was chosen it would say you cannot have this as one of the choices and it would also be able to type in more than one so theoretically it could echo-
"Your Favourite Car is Mazda, Nissan, Renault!"
Here is the code i have now for the drop box that i have working.
<form method="post">
<div id="dropdown">
<?php
if(isset($_POST['cars']))
{
$mycar=$_POST['cars'];
}
else
{
$mycar="";
}
$array1 = array('Volkswagen' , 'Renault' , 'Land Rover');
echo' <select name="cars" onchange="this.form.submit()">';
foreach($array1 as $cars)
{ ?>
<option value="<?php echo $cars; ?>" <?php if($mycar==$cars) echo "
"selected='selected'"; ?> ><?php echo $cars; ?></option>
<?php
}
echo'</select>
</div></form>';
?>
<div id="result">
<?php
echo "Your favourite car is $mycar";
?>
</div>
EDIT: I have attempted this and what i currently have always echo's "this car isnt among the selection" and nothing else and nothing i enter into the text box seems to effect this
here is the code i have
<?php
$cars = array("Volkswagen","Renault","Land Rover");
?>
<form action="array.php" method="post">
<center> <input type="text" name="cars" id="cars" />
<input type="submit" /> </center>
<?php
if (in_array($_POST, $cars)) {
echo "Your Favourite Car is $_POST";
}
else {
echo "This car is not among the selection";
}
?>
</form>
You can easily control the user input by checking it before:
<form method="post">
<div id="dropdown">
<?php
// Car types
$carTypes = array('Volkswagen' , 'Renault' , 'Land Rover');
$wrongCarChoosen = false ;
if(isset($_POST['cars'])) {
$mycar = $_POST['cars'];
if (!in_array($mycar, $carTypes)) {
$wrongCarChoosen = true ;
}
}
else {
$mycar = "";
}
echo' <select name="cars" onchange="this.form.submit()">';
foreach($carTypes as $carName){ ?>
<option value="<?php echo $cars; ?>"
<?php
if($mycar == $carName) echo "
"selected='selected'"; ?> >
<?php echo $carName;
?></option>
<?php
}
echo'</select>
</div></form>';
?>
<div id="result">
<?php
if ($wrongCarChoosen) {
echo "Your choice ".$mycar." is not contained in ".implode($carTypes, ',') ;
}
else {
echo "Your favourite car is $mycar";
}
?>
</div>
You must edit the form to accept multiple values. And you can check the user values before you echo the text.
<form method="post">
<div id="dropdown">
<?php
$array1 = array('Volkswagen' , 'Renault' , 'Land Rover');
$error = false;
if(isset($_POST['cars']))
{
$mycar=$_POST['cars'];
foreach ($mycar as $car)
{
if (!in_array($car, $array1))
{
$error = $car;
}
}
}
else
{
$mycar=Array();
}
echo' <select name="cars[]" multiple="multiple">';
foreach($array1 as $cars){ ?>
<option value="<?php echo $cars; ?>" <?php if(in_array($cars, $mycar)) echo "\"selected='selected'"; ?> ><?php echo $cars; ?></option>
<?php
}
echo'</select>
<input type="submit" name="submit" value="submit" />
</div></form>';
?>
<div id="result">
<?php
if ($error===false) echo "Your favourite car is ".implode(', ', $mycar);
else echo $error . ' is not contained by ' . implode(', ', $array1);
?>
</div>
Related
I am trying to update a database record using a submit button, however the form that information is being taken from has itself been populated by another submit button.
The PHP is below:
if (isset($_POST['edit']))
{
$editUser = User::locate($_POST['userSelect']);
$setCompany = Company::findCompany();
$exCompanyId = $editUser->user_company_id;
$isAdmin = $editUser->user_admin;
$eUserFirstname = $editUser->user_firstname;
$eUserLastname = $editUser->user_lastname;
$eUserUsername = $editUser->user_username;
$eUserEmail = $editUser->user_email;
$eUserCompany = $editUser->user_company;
$adminArray = array("Yes","No");
if ($exCompanyId > NULL)
{
$exCompany = Company::locateId($exCompanyId);
$companyValue = $exCompany->company_name;
}
else
{
$companyValue = "";
}
if ($isAdmin > 0)
{
$userAdmin = $adminArray[0];
}
else
{
$userAdmin = $adminArray[1];
}
}
if (isset($_POST['confirm']))
{
$updateUserRecord = User::locate($eUserUsername);
$newCompanyValue = $_POST['setCompany'];
$isAdmin = $_POST['userAdmin'];
$newCompany = Company::locate($newCompanyValue);
$companyId = $newCompany->company_id;
$updateUserRecord->user_company_id = $companyId;
$updateUserRecord->user_admin = $isAdmin;
$editUser->updateUserAdmin();
$message = "You have successfully updated the user account";
echo "<script type='text/javascript'>alert('$message');</script>";
}
The HTML code is below:
<form action="" method="post">
<p>
<label>First Name:</label>
<label><?php echo $eUserFirstname ?></label>
</p>
<p>
<label>Last Name:</label>
<label><?php echo $eUserLastname ?></label>
</p>
<p>
<label>Username:</label>
<label><?php echo $eUserUsername ?></label>
</p>
<p>
<label>Email Address:</label>
<label><?php echo $eUserEmail ?></label>
</p>
<p>
<label>User Company:</label>
<label><?php echo $eUserCompany ?></label>
</p>
<p>
<label>Set Company:</label>
<select name="setCompany">
<option><?php echo $companyValue ?></option>
<?php
foreach ($setCompany as $srow)
{
?>
<option id="<?=
$srow->company_id
?>">
<?=
$srow->company_name
?>
</option>
<?php
}
?>
</select>
</p>
<p>
<label>Administrator:</label>
<select name="userAdmin">
<option><?php echo $userAdmin ?></option>
<?php
foreach ($adminArray as $arow)
{
?>
<option>
<?=
$arow
?>
</option>
<?php
}
?>
</select>
</p>
<input type="submit" name="cancel" value="Cancel">
<input type="submit" name="confirm" value="Confirm">
</br>
</form>
From my investigations so far the variables aren't transferring to the 2nd if statement, and I'm not sure how to make them available to it.
Store the first form's submission in hidden input fields alongside your echo statements. For example: <input type="hidden" name="field_name" value="<?php echo htmlspecialchars($field); ?>">
Or, alternatively, update the DB after first form posts.
I'm trying to show an error message if a checkbox has not been selected. I've managed to get it done input field, but unsure of how to get it done with a checkbox. This is what I have so far:
<?php
# check if data has been posted
if (!empty($_POST)) {
# keep track validation errors
$author_error = null;
$categories_error = null;
# keep track of post values
$author = $_POST['author'];
$categories = $_POST['categories'];
# validate input
if (empty($author)) {
$author_error = 'Please select author';
$valid = false;
}
if (empty($categories)) {
$categories = 'Please select categories';
$valid = false;
}
# if data is valid, insert into the database
if ($valid) {
}
}
?>
<div class="control-group <?php if (!empty($author_error)){ echo 'error'; } ?>">
<label class="control-label">Author</label>
<div class="controls">
<select name="author" id="author">
<option value="">Select one</option>
<?php $sql2 = 'SELECT id, name FROM author';
foreach ($dbConnection->query($sql2) as $data2) { ?>
<option value="<?php echo $data2['id']; ?>"
<?php if(isset($author) && $author == $data2['id']) { echo 'selected'; } ?>>
<?php echo $data2['name']; ?>
</option>
<?php } ?>
</select>
<?php if (!empty($author_error)) { echo '<span class="help-inline">' . $author_error . '</span>'; } ?>
</div>
</div>
<div class="control-group <?php if (!empty($categories_error)){ echo 'error'; } ?>">
<fieldset>
<legend class="control-label">Categories:</legend>
<?php $sql3 = 'SELECT id, name FROM category';
foreach ($dbConnection->query($sql3) as $data3) { ?>
<div class="controls">
<label for="category<?php echo($data3['id']);?>">
<input type="checkbox" name="categories" id="categories" value="<?php echo($data3['id']); ?>"
<?php if(isset($_POST['categories']) && in_array($data3['id'], $_POST['categories'])) { echo 'checked'; } ?>>
<?php echo($data3['name']); ?>
</label>
</div>
<?php } ?>
</fieldset>
<?php if (!empty($categories_error)) { echo '<span class="help-inline controls">' . $categories_error . '</span>'; } ?>
</div>
Where am I going wrong with the categories field?
Check it like:
isset($_POST['categories'])
On line #62 you are checking if $categories_error is empty but you are using variable $categories for storing error message on line #19
I'm developing a webpage with a select list that contains images.
I already have this:
When I select an image name in the list the image will be displayed in the div below.
<?php
// Create connection
$con=mysqli_connect("******","***","***","charts");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<form method="post" action="index.php" id="nano" name="nano">
<p>
<select name="SelectBox" id="SelectBox" onchange="this.form.submit()">
<?php if($_POST['submitted'] == true){ ?>
<?php
$result = mysqli_query($con,"SELECT * FROM Nano WHERE IMAGE_NAME ='". $_POST['SelectBox']."'");
while($row = mysqli_fetch_array($result))
{ ?>
<option selected="selected" value="<?php echo $row['IMAGE_NAME'] ?>">
<?php echo $row['IMAGE_PARAMETER'] ?>
</option>
<?php } ?>
<?php echo $_POST['SelectBox']; ?></option>
<?php } else{ ?>
<?php
$result = mysqli_query($con,"SELECT TOP * FROM Nano");
while($row = mysqli_fetch_array($result))
{
?>
<option selected="selected" value="<?php echo $row['IMAGE_NAME'] ?>">
<?php echo $row['IMAGE_PARAMETER'] ?>
</option>
<?php
$var1 = $row['IMAGE_NAME']; ?>
<?php
}
?>
<?php } ?>
<option value="" disabled="disabled"> -------- </option>
<?php
$result = mysqli_query($con,"SELECT * FROM Nano");
while($row = mysqli_fetch_array($result))
{ $values[] = $row['IMAGE_NAME'];
?>
<option value="<?php echo $row['IMAGE_NAME'] ?>">
<?php echo $row['IMAGE_PARAMETER'] ?>
</option>
<?php }?>
</select>
<input type="hidden" name="submitted" id="submitted" value="true" />
</p>
<?php if($_POST['submitted'] == true){ ?>
<p><img src="Images\Nano\<?php echo $_POST['SelectBox']?>" width="953" height="600" /></p>
<?php }else { ?>
<p><img src="Images\Nano\<?php print_r($values[0]) ?>" width="953" height="600" /></p>
<?php } mysqli_close($con);?>
</form>
</div>
I want when I move down in the select list the picture will change and not when I click on it in the select list.
In your case, you have to bind hover event to option, but there is no way to do what you want using native select control. The native one only answers when you click a different option from previous.
However, you can simulate a select control using html&css&js, that way when your cursor move down the simulated option(which might be a div or something), you can bind event handlers to it and display the name.
This question already has answers here:
PHP: on select change, post form to self
(3 answers)
Closed 9 years ago.
I have a working drop down box but im not sure how to make it so that if they chose "volksvagen" it would display
"Your Favourite Car is Volksvagen!"
and so on for each of the options, here is the code for the drop down box.
<div id="dropdown">
<?php
$array1 =
array('Volkswagen' , 'Renault' , 'Land Rover');
echo' <select name="cars">';
foreach($array1 as $cars){
echo'<option value="'.$cars.'">'.$cars.'</option>';
}
echo'</select>';
?>
</div>
javascript not used..try it..!!
<form method="post">
<div id="dropdown">
<?php
if(isset($_POST['cars']))
{
$mycar=$_POST['cars'];
}
else
{
$mycar='';
}
$array1 = array('Volkswagen' , 'Renault' , 'Land Rover');
echo' <select name="cars" onchange="this.form.submit()">';
foreach($array1 as $cars){ ?>
<option value="<?php echo $cars; ?>" <?php if($mycar==$cars) echo "selected='selected'"; ?> ><?php echo $cars; ?></option>
<?php
}
echo'</select>
</div></form>';
echo 'your favourite car is : '; echo $mycar;
?>
<div id="dropdown">
<?php
$array1 =
array('Volkswagen' , 'Renault' , 'Land Rover');
echo' <select name="cars" onchange="display_message(this.value);">';
foreach($array1 as $cars){
echo'<option value="'.$cars.'">'.$cars.'</option>';
}
echo'</select>';
?>
<span id="message"></span>
</div>
<script>
function display_message($selected_value)
{
document.getElementById('message').innerHTML = 'Your Favourite Car is'+$selected_value+'!';
}
</script>
<body onload="showCar()">
<div id="dropdown">
<?php
$array1 =
array('Volkswagen' , 'Renault' , 'Land Rover');
echo' <select name="cars" id="cars" onchange="showCar()">';
foreach($array1 as $cars){
echo'<option value="'.$cars.'">'.$cars.'</option>';
}
echo'</select>';
?>
<div id="demo"> </div>
<script>
function showCar() {
var car = document.getElementById('cars').value;
document.getElementById("demo").innerHTML="Your Favourite Car is "+car+"!";
}
</script>
</div>
</body>
Try This
I have a text field which the user can enter comma separated list and then php converts it to a drop select list however I want a condition that will show a text input field if only a single value is entered. I tried the code below but it is only returning the select box even with a single entry. How can I achieve this condition?
<?php $listval = explode(",",$vals);
if(is_array($listval)) { ?>
<select name="valuelist">
<?php
foreach($listval as $value) {
echo '<option>'.$value.'</option>';
} ?>
</select>
<?php }else{ ?>
<input type="text" size="10" name="valuelist" value="<?php echo $vals; ?>" />
<?php } ?>
explode will always return an array. So therefor is_array will always be true.
Change your if statement to this
if(sizeof($listval) > 1)
try This
<?php $listval = strpos(',',$vals)?explode(",",$vals):$vals;
if(is_array($listval)) { ?>
<select name="valuelist">
<?php
foreach($listval as $value) {
echo '<option>'.$value.'</option>';
} ?>
</select>
<?php }else{ ?>
<input type="text" size="10" name="valuelist" value="<?php echo $vals; ?>" />
<?php } ?>
Please try to use the follwing code:
<?php $listval = explode(",",$vals);
if(count($listval) > 1) { ?>
<select name="valuelist">
<?php
foreach($listval as $value) {
echo '<option>'.$value.'</option>';
} ?>
</select>
<?php } elseif(count($listval) == 1) { ?>
<input type="text" size="10" name="valuelist" value="<?php echo $vals; ?>" />
<?php } ?>
As #Kris indicated, you can check with,
count($array)
or
sizeof($array)
<?php $listval = explode(",",$vals);
$array_count = count($listval);
if($array_count > 1) { ?>
<select name="valuelist">
<?php
foreach($listval as $value) {
echo '<option>'.$value.'</option>';
} ?>
</select>
<?php }else{ ?>
<input type="text" size="10" name="valuelist" value="<?php echo $vals; ?>" />
<?php } ?>