I need a validation before submitting this form:
<form action="<?php echo $editFormAction; ?>" method="POST" id="form1" name="contract">
<table class="myp-table">
<tr>
<td>Value Test</td>
</tr>
<?php do { ?>
<tr>
<td><select name="playerContract[]">
<option value="0" <?php if (!(strcmp(0, $row_datacontract['playerContract']))) {echo "selected=\"selected\"";} ?>>0</option>
<option value="1" <?php if (!(strcmp(1, $row_datacontract['playerContract']))) {echo "selected=\"selected\"";} ?>>1</option>
<option value="2" <?php if (!(strcmp(2, $row_datacontract['playerContract']))) {echo "selected=\"selected\"";} ?>>2</option>
<option value="3" <?php if (!(strcmp(3, $row_datacontract['playerContract']))) {echo "selected=\"selected\"";} ?>>3</option>
</select></td>
</tr>
<?php } while ($row_datacontract = mysql_fetch_assoc($datacontract)); ?>
<tr>
<td><input class="linkbuttonmp" name="contract" type="submit" value="Invio" /></td></tr>
</table>
</form>
The sum of the values selected by users mustn't exceed the default value which in my case is a value retrieved from my database.
I tried with jquery in this way:
var max = 3;
$("#form1 select").change(function () {
var selects = <?php echo $row_datacoach['coachContract']; ?>;
$("#form1 select").each(function () {
selects = selects + parseInt($(this).val());
});
if(selects >= max) {
$("#submit").attr("disabled","disabled");
} else {
$("#submit").removeAttr("disabled");
}
});
Unfortunately it doesn't work.
Can you help me?
UPDATE
Sorry, I made a mistake: the following is the script I actually tried:
<script type="text/javascript">
var max = <?php echo $row_datacoach['coachContract']; ?>;
$("#form1 select").change(function () {
var selects = 0;
$("#form1 select").each(function () {
selects = selects + parseInt($(this).val());
});
if(selects >= max) {
$("#submit").attr("disabled","disabled");
} else {
$("#submit").removeAttr("disabled");
}
});
</script>
The value fetched from database need to be assigned to variable max and the selects has to be initialized with value 0.
I think it should be
var max = <?php echo $row_datacoach['coachContract']; ?>;
$("#form1 select").change(function () {
var selects = 0;
$("#form1 select").each(function () {
selects = selects + parseInt($(this).val());
});
if(selects >= max) {
$("#submit").attr("disabled","disabled");
} else {
$("#submit").removeAttr("disabled");
}
});
Demo: Fiddle
add ID to form :
<form action="" method="POST" name="contract" id="form1">
then do :
var max = 3;
$("#form1 select").on('change', function () {
var selects = 0;
$("#form1 select").each(function () {
selects += parseInt(this.value, 10);
});
$("#submit").prop('disabled', selects >= max);
});
Related
In Laravel, I am trying to get the value of selected drop-down value and send it to another controller. And by using that drop-down value below values need to be changed by fetching the data from the database.
Below is the code I have tried.
<form action="{{{ url("/getDetailsBynumber/$_POST['number']") }}}" method="post">
<select name="number" id="number">
<option selected="selected">Select number id</option>
<?php
$numberArray = json_decode($number_id, true);
for ($i = 0 ; $i<=$number_count ; $i++) {
?>
<option value="<?php echo $numberArray[$i]["number_value"] ?>"><?php echo $numberArray[$i]["number_value"]; ?></option>
<?php
}
?>
</select>
<input type="submit" value="Submit">
You need to add name attribute to select tag. As a result when form submitted it will send selected value with it's name. For example:
<select name="product_id">
<option value="1">Cup</option> <!-- Suppose this option selected -->
<option value="2">Pen</option>
<option value="3">Book</option>
</select>
If you submit form, you can get selected value in your controller as follows:
public function methodName(Request $request)
{
// $request->product_id is name attribute of your select tag
print_r($request->product_id); // It will print out 1 which is value of Cup
}
<form action="{{ url("/getDetailsBynumber/") . $_POST['number'] }}" method="post">
<select name="number" id="number">
<option selected="selected">Select number id</option>
<?php
$numberArray = json_decode($number_id, true);
for ($i = 0 ; $i<=$number_count ; $i++) {
?>
<option value="<?php echo $numberArray[$i]["number_value"] ?>"><?php echo $numberArray[$i]["number_value"]; ?></option>
<?php
}
?>
</select>
<input type="submit" value="Submit">
This is my code to get state name after submit if validation fail incase then it works
on load of page you can get the country id like this
var countryid = $('#opt_country').val();
after that base on that you have to find state_id
like this
if (countryid != '')
{
$.ajax({
url: base_path + 'getstate',
type: "POST",
data: {in_country_id: countryid},
async: true,
cache: false,
success: function (statedata) {
var obj = jQuery.parseJSON(statedata);
console.log(obj);
if (obj.SUCC_FLAG == 1)
{
for (var i in obj)
{
console.log(obj);
var id = obj[i].in_state_id;
var name = obj[i].st_state_name;
if (id == stateid)
{
$("#opt_state").append("<option value='" + id + "' selected >" + name.toString() + "</option>");
} else {
$("#opt_state").append("<option value='" + id + "'>" + name.toString() + "</option>");
}
}
}
else
{
confirm('We are unable to load State');
}
},
error: function () {
alert("server error");
}
});
}
and after that you can get the state so here is my example to get state after submit the page
This my code. How this multiselect values with array to post modelseardh.php
<p>
<label>Country:</label>
<select multiple="multiple" name="country[]" class="qs_carlocation1 carlocations" onChange="jsFunction()" id="selectOpt">
<?php
$sql_se = mysql_query('select * from `country`');
while($se = mysql_fetch_assoc($sql_se)) {
?>
<option value = "<?php echo $se['id'];?>"><?php echo $se['country'];</option>
<?php
}
?>
</select>
</p> `
<p id="change_form">
<label>Mark:</label>
<select multiple="multiple"name="search_mark[]"class="qs_carlocation1 carlocations" >
<?php
$sql_search_mark = mysql_query('select * from `mark`');
while($smark = mysql_fetch_assoc($sql_search_mark)) {
?>
<option value="<?php echo $smark['id'];?>"><?php echo $smark['name'];?></option>
<?php
}
?>
<script>
function jsFunction(){
var select = document.getElementById("selectOpt").value;
$.post(
"modelsearch.php",
{
id: "" + select + ""
},
function(data)
{
$('#change_form').html(data);
}
);
}
</script>
</select>
</p>
<?PHP
//this modelsearch.php
include('../db.php');
$id = $_POST['id'];
?>
There's wrong with enclosing & misplaced tag in the following line that possibly make the rendering ouput fails:
<option value = "<?php echo $se['id'];?>"><?php echo $se['country'];"></option>
which is missing ?>"> right before the </option>, and you need to remove "> between ?> <?php
it should be :
<option value = "<?php echo $se['id'];?><?php echo $se['country'];?>"></option>
and you also don't ouput any values between <option> xxx </ouput>, so the value attribute is assigned in the <option> but nothing to be ouput. I meant that xxx to be output
and FYI , you dont need to multiple echo the values, just concatenate the values :
<option value = "<?php echo $se['id'] +""+ $se['country'];?>" ></option>
First of all, you don't need to keep the country name in Array change country[] to country only
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script language='javascript'>
function showselection()
var frm = document.testingform //this is the name of the form can be as your form name
var opt = frm.country //this is the multiselect option name
var numofoptions = opt.length
var selValue = new Array
var j = 0
for (i=0; i<numofoptions; i++)
{
if (opt[i].selected === true)
{
selValue[j] = opt[i].value
j++
}
}
var myvalues=document.getElementById('yourhiddenvaluename').value=selValue;
$.post( 'modelsearch.php',{ newva: myvalues }, function( data ) {
$('#change_form').html(data)
});
}
</script>
<select name='country' multiple onchange='showselection()'>
//Your values
</select>
<input type="text" id="yourhiddenvaluename" name="seletedvalued" />
In modelsearch.php:
print_r($_POST['newva']);
I am using this script get by somewhere from internet..
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function sureTransfer(from, to, all) {
if ( from.getElementsByTagName && to.appendChild ) {
while ( getCount(from, !all) > 0 ) {
transfer(from, to, all);
}
}
}
function getCount(target, isSelected) {
var options = target.getElementsByTagName("option");
if ( !isSelected ) {
return options.length;
}
var count = 0;
for ( i = 0; i < options.length; i++ ) {
if ( isSelected && options[i].selected ) {
count++;
}
}
return count;
}
function transfer(from, to, all) {
if ( from.getElementsByTagName && to.appendChild ) {
var options = from.getElementsByTagName("option");
for ( i = 0; i < options.length; i++ ) {
if ( all ) {
to.appendChild(options[i]);
} else {
if ( options[i].selected ) {
to.appendChild(options[i]);
}
}
}
}
}
window.onload = function() {
document.getElementById("src2TargetAll").onclick = function() {
sureTransfer(document.getElementById("source"), document.getElementById("target"), true);
};
document.getElementById("src2Target").onclick = function() {
sureTransfer(document.getElementById("source"), document.getElementById("target"), false);
};
document.getElementById("target2SrcAll").onclick = function() {
sureTransfer(document.getElementById("target"), document.getElementById("source"), true);
};
document.getElementById("target2Src").onclick = function() {
sureTransfer(document.getElementById("target"), document.getElementById("source"), false);
};
}
</script>
</head>
<body>
<table>
<tr>
<td>
<form name="formcheck" method="post" action="target.php">
<select id="source" name="source" multiple>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
<option value="5">five</option>
<option value="6">six</option>
<option value="7">seven</option>
<option value="8">eight</option>
<option value="9">nine</option>
<option value="10">ten</option>
</select>
</td>
<td>
<input type="button" id="src2TargetAll" name="src2TargetAll" value=">>"/><br/>
<input type="button" id="src2Target" name="src2Target" value=">"/><br/>
<input type="button" id="target2Src" name="target2Src" value="<"/><br/>
<input type="button" id="target2SrcAll" name="target2SrcAll" value="<<"/><br/>
</td>
<td>
<select id="target" name="target" multiple>
</select>
</td>
</tr>
</table>
</form>
</body>
</html>
This code creating a two select box which is selected items left to right all code works fine but in php file i am trying to get value by this php code $targets = $_POST['target']; but don't get all value so plz tell me what is the right way to get value of this in php?
Use this
<select id='source' name='source[]' multiple='multiple'>
Give the select element a name ending in [] so that PHP will present it as an array in $_POST/GET/REQUEST.
use this
<select id="source" name="source[]" multiple='multiple'>
it will post a array in server side
I am using a JS/Ajax function to echo Selected values from select boxes. I am populating these select boxes with values from MySQL DB. The function works with text input fields but now that I am using it with these select boxes I am getting no response.
Can I echo the selected value from a select box with the JS? or Is there a better way? EXAMPLE
JS
<script>
$(document).ready(function() {
var timer = null;
var dataString;
function submitForm(){
$.ajax({ type: "POST",
url: "index.php",
dataType: 'json',
success: function(result){
$('#special').html('<p>' + $('#resultval', result).html() + '</p>');}
});
return false;
}
$('#category_form').on('change', function() {
clearTimeout(timer);
timer = setTimeout(submitForm, 2000);
});
});
</script>
PHP/HTML
try {
$pdo = get_database_connection();
$sql = "SELECT *
FROM `categories`
WHERE `master_id` = 0";
$statement = $pdo->query($sql);
$list = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo 'There was a problem';
}
<form action="" method="post" id="category_form'" name="category_form'">
<select name="main" id="main" size="7" class="update">
<option value="">Select one</option>
<?php if (!empty($list)) { ?>
<?php foreach($list as $row) { ?>
<option value="<?php echo $row['id']; ?>">
<?php echo $row['name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
<select name="subc1" id="subc1" size="7" class="update"
disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc2" id="subc2" size="7" class="update"
disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc3" id="subc3" size="7" class="update"
disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
</form>
<div id="special"></div>
It looks like you're selecting the wrong element with jQuery.
Change:
$('#category_form').on('change', function() {
clearTimeout(timer);
timer = setTimeout(submitForm, 2000);
});
To:
$('#main').on('change', function() {
clearTimeout(timer);
timer = setTimeout(submitForm, 2000);
});
I am using a selectbox to display the list of "enterpirses" from the database. But the onchange event is not triggering. But when i manually once put the value of querystring then it starts working. I dont understand why it is happening.
I am new to javascript and php, kindly suggest me the solution to this.
<select id="enterprisebox" onchange="javascript:valueselect()" >
<option value="-">-</option>
<?php foreach($enterprise as $val) { ?>
<option value="<?php echo $val['customer_id'];?>"><?php echo $val['customer_name']?>
</option>
<? } ?>
</select>
and my javascript is--
function valueselect()
{
var i = document.getElementById('enterprisebox');
var p = i.options[i.selectedIndex].value;
//alert(p);
window.location.href = "channelinformation.html?selected="+p;
}
onchange="javascript:valueselect()" replace with onchange="valueselect(this.value);"
function valueselect(myval)
{
window.location.href = "channelinformation.html?selected="+myaval;
}
You Can Use Directly
onchange="window.location='channelinformation.html?selected='+myaval"
Make life easier and use jQuery:
$(document).ready(function () {
$('#enterprisebox').change(function () {
window.location.href = "channelinformation.html?selected="+ $(this).val();
});
});
Try this,
<select id="enterprisebox" onchange="javascript:valueselect(this)" >
<option value="-">-</option>
<?php foreach($enterprise as $val) { ?>
<option value="<?php echo $val['customer_id'];?>"><?php echo $val['customer_name']?>
</option>
<? } ?>
</select>
<script>
function valueselect(sel) {
var value = sel.options[sel.selectedIndex].value;
window.location.href = "channelinformation.html?selected="+value;
}
</script>