I want to post three element to backend use ajax. There are input type name is "suppid" checkbox array name is "lang[]" and select type name is "guide". All three are successfully post to the backend use ajax. But I wrote a function before to detect if the post is null, and it failed to detect checkbox,no matter what I try I fail to detect checkbox array and only when write this I can check both three successfully.
function check()
{
//$vali1=checknullnew($_POST); I can not use this function
//if($vali1['vali']=='Y') if I use this no matter checkbox
//are clicked or not it will be successfully uploaded.
if(isset($_POST['lang']) && isset($_POST['suppid']) &&isset($_POST['guide']))
//I can only use this to successful detect three post are null or not
{
$result['vali']='Y';
$result['message']='successfully uploaded';
$result['redirect_url']='test4.html';
return $result;
}
else
{
$result['vali']='N';
$result['message']='no';
return $result;
}
}
$response=check();
echo json_encode($response);
//My html form is like this
<form role="form" id="myform" >
<div class="form-group">
<label>
suppid
</label>
<input type="text" class="form-control" name="suppid" style="width: 120px;">
</div>
<div class="form-group">
<label >
lang
</label>
<label class="checkbox-inline"><input type="checkbox" name="lang[]" value="en">English</label>
<label class="checkbox-inline"><input type="checkbox" name="lang[]" value="zh-CN">Chinese</label>
<label class="checkbox-inline"><input type="checkbox" name="lang[]" value="zh-TW">Cantonese</label>
<label class="checkbox-inline"><input type="checkbox" name="lang[]" value="jp">Japanese</label>
<label class="checkbox-inline"><input type="checkbox" name="lang[]" value="kr">Korean</label>
</div>
<div class="form-group">
<label>
guide
</label>
<select class="form-control" name="guide" style="width: 120px;">
<option value="Y">yes</option>
<option value="N" selected>no</option>
</select>
</div>
<input name="submit" type="button" class="btn btn-default" id="button1" value="上传">
</form>
//My javascript is
<script>
$(document).ready(function() {
$('input[name=submit]').on('click',function(){
var suppid = $('input[name=suppid]').val().trim();
var lang = new Array;
$("input[name='lang[]']:checked").each(function() {
lang.push($(this).val().trim());
});
var guide = $('select[name=guide]').val().trim();
//alert('ok');
$.ajax({
type: "POST",
url: "test4.php",
data: {
suppid:suppid,
lang:lang,
guide:guide
},
dataType: "json", //send it along with your call
success: function( data )
{
if( data.vali == 'N' )
{
alert(data.message);
}
else {
alert(data.message);
window.location.href = data.redirect_url;
}
}
});
});
});
</script>
//My detect null function is like this
function checknullnew($check)
{
$valid=0;
foreach($check as $key => $value)
{
if($key=='lang')
{
if(!issset($value)){$valid=1;}
}
else
{
if(trim($value)=="")
{
$valid=1;
}
}
}
if($valid==0)
{
$result['vali']='Y';
return $result;
}
else{
$result['vali']='N';
$result['message']='Please input complete messages';
return $result;
}
}
//also I try this function too
//I use isArray() to detect the checkbox array and use isset()to detect if the //array is null or not.
Aucun commentaire:
Enregistrer un commentaire