I need a solution for this. Here I want to validate form elements. I did it with PHP but what my problem is on clicking submit button validation process takes place but select box value changes to select dept.
FOR EXAMPLE, if you just click submit button without giving student id by only select dept.. the message shows enter studentid but select box value changes to select dept again as it page reloads first..
php:
if($_POST['submit']!='')
{
$c=0;
if($_POST['studid']=='' ) {
$msg_id="Enter stud id";
$c++;
}
if($_POST[studdept]=='') {
$msg_dept="Enter stud dept";
$c++;
}
if($c==0) {
echo "form has been submitted..";
}
}
HTML:
<form id="myform" action="" method="post">
<table>
<tr>
<td> Studid: *</td> <td> <input type="text" name="studid" maxlength="10" value="<?=$_POST[studid]?>"> </td> <td> <?php echo $msg_id; ?> </td> </tr>
<tr>
<td> StudDept: *</td>
<td>
<select name="studdept">
<option value="" selected="selected" >select dept</option>
<option value="cse" >cse</option>
<option value="eee" >eee</option>
<option value="mech" >mech</option>
<option value="ece" >ece</option>
</select>
</td>
<td><?php echo $msg_dept; ?> </td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"> </td>
</tr>
</table>
</form>
can someone help me here?
Thanks in Advance...
You have to generate your <option></option> list through loop (if you're generating Department list from Database Table) where you have to put condition which you had put it for first <option> pair.
Or You can put a Turnary Operator (if you're using static list).
<select name="studdept">
<option value="none" <?php echo ($_REQUEST['studdept']=='none'?'selected="selected"'):'';?> >select dept</option>
<option value="cse" <?php echo ($_REQUEST['studdept']=='cse'?'selected="selected"'):'';?> >cse</option>
<option value="eee" <?php echo ($_REQUEST['studdept']=='eee'?'selected="selected"'):'';?> >eee</option>
<option value="mech" <?php echo ($_REQUEST['studdept']=='mech'?'selected="selected"'):'';?> >mech</option>
<option value="ece" <?php echo ($_REQUEST['studdept']=='ece'?'selected="selected"');:''?> >ece</option>
</select>
And use $_REQUEST instead $_GET or $_POST.
The method of your form is POST and you are trying to get the department using GET <? echo ($_GET['studdept'] == 'cse' ? 'selected="selected"' : '') ?>.
Anyway, you'll need to improve your HTML so you check if any of the deptartments are selected (not just 'cse'). A for/while loop will do the job.
In addition, I would add " " to the post variables, as you did in if($_POST['studid']=='' ) (but you didn't in if($_POST[studdept]==''))
Your specifies that the form data will be submitted using the POST method.
The way that PHP does this is to store all the "posted" values into an associative array called "$_POST".
Be sure to take notice the names of the form data names, as they represent the keys in the "$_POST" associative array.
Use: $_POST['studdept'] instead of: $_GET['studdept']
Beacause you use method POST for send data and you are trying to get value using $_GET['studdept'].
so you have two ways:
(1) just change the method to GET or
(2) change $_GET['studdept'] to $_POST['studdept'].
Related
as the title says, it sends 2 out of 3 values. Im fairly new at php, html coding and the uses of mysql servers so im not sure what the error is.
Btw my project is to make a "Rent a car" site.
First site should be choose car brand, customer and city (sry for swedish, some of the code is in english)
Site#1
https://gyazo.com/86bd55f87f75950013f2c5f3d7466848
code#1
<form action="carsite.php" method="GET">
<table>
<tr>
<th>1.Välj Model</th>
<td>
<p>
<select value="car">
<option disabled selected value="">Välj Model</option>
<?php foreach($res as $row){?>
<option value="<?php echo htmlspecialchars($row->);?>"><?php echo htmlspecialchars($row->make);?></option><?php
}
?>
</select>
</p>
</td>
</tr>
<tr>
<th>2.Välj Kund</th>
<td>
<p>
<select name="customer">
<option disabled selected value="">Välj Kund</option>
<?php foreach($res2 as $row2){?>
<option value="<?php echo htmlspecialchars($row2->id);?>"><?php echo htmlspecialchars($row2->lname);?> <?php echo htmlspecialchars($row2->fname);?></option><?php
}
?>
</select>
</p>
</td>
</tr>
<tr>
<th>3.Välj Stad</th>
<td>
<p>
<select name="city">
<option disabled selected value="">Välj Stad</option>
<?php foreach($res3 as $row3){?>
<option value="<?php echo htmlspecialchars($row3->id);?>"><?php echo htmlspecialchars($row3->city);?></option><?php
}
?>
</select>
</p>
</td>
</tr>
</table>
<input type="submit" value="Sök">
</form>
in my eyes theres nothing wrong. (it sends the id from the tables in mysql)
but at the next site im getting 2/3 values i sent from first page.
Site#2
Notice: Undefined index: car in F:\xamp\htdocs\Slutproject\carsite.php on line 7
12
(12 there is acctually id(customer id) 1 and id 2(city id))
So my problem is why does it send id 2 and 3 but not first one even though they were copy pasted from first id part (the codes of line).
I have tried sending it with GET so i could see the values uptop, still nothing about the car id.
//Mikael
Your first <select> tag has a typo. It should be <select name="car">.
This is the picture where new row will add after user click's on [add more] button..It works fine but i want to send these values using ajax.I can access all textbox values with array map function in ajax except dropdown values.
how I would implement it with this example. Thanks!
<tr>
<td><input type='checkbox' class='case'/></td>
<td>1.</td>
<td> <select name='relation[]' id='relation' >
<option value='brother'>Brother</option>
<option value='sister'>Sister</option>
</select>
</td>
<td><input type='text' id='name' name='name[]'></td>
<td><input type='text' id='school' name='school[]'></td>
<td<select name='medium[]' id='medium'>
<option value='marathi'>marathi</option>
<option value='english'>english </option>
</select>
</td>
<td> <?php
//Get all class data
$query = $db->query("SELECT * FROM `class_master`");
//Count total number of rows
$rowCount = $query->num_rows;
?>
<select name="std[]" id="std" class="form-control focusedInput" >
<option value="">Select Standard </option>
<?php
if($rowCount > 0){
while($row72 = $query->fetch_assoc()){
echo '<option value="'.$row72['class_id'].'">'.$row72['class_name'].'</option>';
}
} else {
echo '<option value="">Class not available</option>';
}
?>
</select>
User form at the starting of the page and and while sending data using ajax just use ("#form_id").serialize() instead of using array map , serialize is the method of jquery. Please read about the method and your problem will be solved.
Any help would be greatly appreciated!!
I am building a php form to update a MySQL database linked to my website.
Part of my form includes list fields where the user can hold ctrl to select multiple values and these are then passed into one MySQL field as comma separated values. See below for three real examples of the data:
3014
3014,3015,3026
3026,3028
The form is updating my database as required, however when the user goes back in to the form to do another update the multiple values they chose aren't pre-selected.
Can anyone help me to figure out why $row_editme['NewsTypeID'] is the field containing the string of values. The field type is a SET.
<td nowrap="nowrap" align="right">NewsTypeID:</td>
<td>
<select name="NewsTypeID[]" size="4" multiple="multiple">
<?php
do {
?>
<option value="<?php echo $row_types['id']?>" <?php if (!(strcmp($row_types['id'], htmlentities($row_editme['NewsTypeID'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>><?php echo $row_types['name']?></option>
<?php } while ($row_types = mysql_fetch_assoc($types)); ?>
</select>
</td>
Try something like this,
<select name="NewsTypeID[]" size="4" multiple="multiple">
<?php
do {
$selected = htmlentities($row_editme['NewsTypeID'], ENT_COMPAT, 'utf-8');
$selected_arr = explode(",",$selected);
?>
<option value="<?php echo $row_types['id']?>" <?php if (in_array($row_types['id'],$selected_arr)) {echo "selected";} ?>>
<?php echo $row_types['name']?>
</option>
<?php } while ($row_types = mysql_fetch_assoc($types)); ?>
</select>
I think it is not supposed to be selected.
Can you try checked?
<input type="checkbox" <?php /*if condition is met here, then*/ echo "checked='checked'" ?> />
It will pre-select all the values the user selected.
I have select option box and also add many field i want to update through submit button.
Here is code:
<td><select name="select_provider" id="select_provider">
<option value="selected">All</option>
<?php $sql_list_provider=mysql_query("Select opp_provider from ambition_opp_provider");
while($cmd=mysql_fetch_array($sql_list_provider)){
?>
<option><?php echo $cmd['opp_provider'];}?></option>
</select></td>
<td><input type="submit" name="update_provider" id="update_provider" value="UPDATE PROVIDER" style="background-color:#000000; color:#fff"/></td>
Thanks for advance.
In your code </option> is outside the loop that causes to generate invalid html. Simply remove } from and add <?php } ?> after </option>
And add value attribute to option tag. value="<?php echo $cmd['opp_provider'];?>" and in server-side, you'll be able to access it like: $_POST['select_provider'] (assume you use post method).
I want to make that if user is select certain option(provided from database) only one value then the other option tag appear to select else for all rest of other options user have text box to enter the value. Something like the below-
<tr>
<td><span class="alert">* </span>Country:</td>
<td><select name="country_id" class="medforminput" id="country_id" onchange="getState('select_state.php?country_id='+this.value)">
<option value="Select Country">Select Country</option>
<?php
while($row4=mysql_fetch_array($coun1))
{ ?>
<option value="<?php echo $row4['country_id']; ?>">
<?php echo $row4['country_name']; ?></option>
<?php }?>
<option value="others">Others</option>
</select></td>
</tr>
<tr>
<td><span class="alert">* </span>State</td>
<?php
if($row4['country_name']=="INDIA")
{?>
<td id="statediv"><select name="state_id" class="medforminput" id="state_id" onchange="getCity('select_city.php?state_id='+this.value)">
<option value="">Choose State</option>
</select></td>
<?php else {
<td id="statediv"><input type="text" name="state_id" class="medforminput" id="state_id"/>
</td>
<?php }?>
</tr>
I am getting nothing what wrong in the concept?
I want to display select tag where user choose the "INDIA" else text box to enter the state.
You need to write an onChange event handler in javascript that tests the option selected, then either displays the dropdown or textbox in a dynamic div.
I'm going to use jQuery because it's a LOT less code.
Example:
<select name="country_id" class="medforminput" id="country_id" >
<!-- options go here -->
</select>
<script language="javascript">
$("#country_id").change(function() {
if ($(this).val() == 'India') { // put the country id here instead of string comparison
$("#stateInput").html(getStates());
} else {
$("#stateInput").html("<input type='text' id='stateId' name='stateId' />");
}
});
</script>
Please excuse the pseudocode - I didn't want to make it excessively long.