A. js/jquery如何這樣調用php
js/jquery調用php一般是用AjAx方式來調用的。
首先確認需要調用的php文件,可以是絕對路徑,也可以是相對路徑
這里以Jquery為例子解釋上面的問題
如下圖就是jquery的實現代碼,下圖的url就是需要請求的php地址;
B. jquery php ajax接收資料庫
在請求你可以成功後 把tbody清空(或給tbody定義一個id 然後操作id也可以) 在放入數據。
其實你寫的太麻煩了,你直接post 請求你的chuli.php 返回json數據。 在js for循環 tr 標簽 把json數據放到 td 里 就ok .
C. php中 jquery如何獲取js動態添加的文本框數組值,並插入mysql資料庫
建2個頁面
1、test.php頁面,代碼如下:
<title></title>
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
x=100000;
y=1;
$("#dosubmit").click(function(){
var rand=parseInt(Math.random()* (x - y ));
var rand1=String.fromCharCode(Math.floor( Math.random() * 26) + "a".charCodeAt(0));
var col="<input name=info["+rand1+rand+"] type='text' class='text' value='"+rand+"' />";
$("input[id=submit]").before(col);
})
$("#submit").click(function(){
$("form input[class=text]").each(function(){
var val=$(this).val();
})
})
})
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<div class="con">
<form name="form1" action="testsave.php" method="post">
<input name="info[name]" class="text" value="10" type="text">
<input name="" class="text1" value="顯示" id="submit" type="submit">
</form>
<input name="" class="text1" value="增加" id="dosubmit" type="submit"><br>
</div>
2、保存提交過來的值頁面testsave.php頁面。代碼如下:
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<?php
$value="";
$test='test';
$conn=mysql_connect('localhost','root','0000','test');
mysql_select_db($test,$conn);
foreach($_POST['info'] as $key){
$value=$value.','.$key;
}
$sql="insert into base (name) values ('$value')";
if(mysql_query($sql)){
echo "添加成功<br />";
}
?>
<input type="button" name="button" id="button" value="顯示/隱藏內容">
<input type="button" name="domit" id="domit" value="返回">
<div class="content" style="display:none">
<?php
$sql1="select id,name from base";
$query=mysql_query($sql1);
while($result=mysql_fetch_array($query)){
echo $result['id']."========>".$result['name']."<br />";
}
?>
</div>
<?php
mysql_close($conn);
?>
<script type="text/javascript">
$(function(){
$("#button").bind("click",function(){
var show=$("div.content");
if(show.is(":visible")){
show.hide();
}
else{
show.show();
}
})
$("#domit").click(function(){
window.history.go(-1);
})
})
</script>
D. jquery+php+mysql 如何提交後不轉頁局部刷新
這個不難,給你做一個演示文件,你稍等!
<?php
session_start();//打開session
error_reporting(E_ALL^E_NOTICE);//忽略錯誤
$Oldification=$_SESSION['classification'];
$m=$_POST['mm'];
!empty($m)or$m=0;
if($m==1){//如果$m的值會等於1,說明表單已經提交(再這里其實是js的Ajax提交的post數據,和表單以post方式提交是一樣的道理,也是一樣的寫法
$classification=$_POST['classification'];//取得發送來的分類名稱
//下面的代碼我用session來進行保存,你自己根據你自己的情況,是保存到資料庫也好,其他的也好,隨便你了
sizeof($Oldification)or$Oldification=array();
array_push($Oldification,$classification);
$_SESSION['classification']=$Oldification;
echo'<optionvalue="">--請選擇--</option>';
foreach($Oldificationas$op){
echo'<optionvalue="',$op,'">',$op,'</option>';
}
echo'<optionvalue="increase">增加分類</option>';
die;
}
?>
<!doctypehtml>
<html>
<head>
<metacharset="utf-8">
<title>分類演示文件</title>
<styletype="text/css">
<!--
*{font-family:'新宋體';font-size:13px;}
div.fen{width:300px;margin:100pxauto;}
-->
</style>
<scripttype="text/javascript"src="jquery-1.7.1.js"></script>
<scripttype="text/javascript">
<!--
varkeyCodes=false;//初始化一個變數,設置為false,當是這個值的時候,捕獲鍵盤事件,反之則不捕獲
$(document).ready(function(e){
varsel=$('select.fenlei');//取得下拉框對象
sel.change(function(){//給下拉框綁定change事件,也就是下拉框選擇不同的值的時候觸發的js事件
if($(this).val()=='increase'){//如果選中的值等於increase
$(this).find('option:first').prop('selected','selected');;//重置下拉框
keyCodes=true;
varhtml='<divid="fugai"></div><divid="input">分類名稱:<inputtype="text"id="Classification"><buttontype="button"id="submit">提交</button><buttontype="button"id="Cancel">取消</button></div></div>';//設置變數,變數值為一個輸入框和一個提交按鈕
$('body').append(html);//將輸入框和提交按鈕追加到body標簽最後面
$('#fugai').hide().css({'position':'absolute','z-index':'100','left':'0px','top':'0px','width':'100%','height':'100%','background':'#000'}).fadeTo('slow',0.2);//給追加的第一個div標簽設置成遮罩層
$('#input').hide().css({'position':'absolute','z-index':'1000','background':'#fff','border':'solid3px#d7d7d7','padding':'20px'});//將追加的第二個div標簽設置成浮動層
varwidth=$('#input').width(),height=$('#input').height(),left=($(window).width()-width)/2,top=($(window).height()-height)/3;//計算浮動層的上距離和左距離
$('#input').css({'top':top+'px','left':left+'px'}).fadeTo('slow',1);//設置浮動層居中
$('#Cancel').click(function(){//給取消按鈕綁定點擊事件
$('#input').fadeTo('slow',0,function(){$(this).remove();})
$('#fugai').fadeTo('slow',0,function(){$(this).remove();})
keyCodes=false;
//將浮動層和遮罩層淡入並刪除
});
$('#submit').click(function(){//給提交按鈕綁定點擊事件
varObj=$('#Classification');//獲取輸入框對象
varVal=Obj.val().replace(/s/gi,'');//取得輸入框的值,並過濾空格
if(Val==''){//如果沒有輸入值
alert('請輸入分類名稱');//彈出警告框
Obj.focus();//使輸入框獲得焦點
}else{
$.post(document.URL.replace(/#/gi,''),{mm:1,classification:Val},function(Success){//Ajax方式以post提交數據至當前頁面進行數據處理,並返回數據處理結果Success
$('select.fenlei').html(Success);//將數據處理結果以html代碼的方式重新更新下拉框
$('#Cancel').click();
});
}
});
$(document).keyup(function(event){//綁定鍵盤按下後彈起事件
if(keyCodes&&event.keyCode==13){//鍵盤捕獲開關打開,並且按下的是回車鍵
$('#submit').click();//觸發提交按鈕的點擊事件,這樣就達到了回車提交的目的
}
});
}
});
});
//js代碼部分完成
//-->
</script>
</head>
<body>
<divclass="fen">請選擇分類
<selectname="fenlei"class="fenlei">
<optionvalue="">--請選擇--</option>
<?php
//這里你可以根據你自己的情況設置下拉框選項,比如讀取資料庫數據。但上下兩個不能少,否則無法觸發js事件
foreach($Oldificationas$op){
echo'<optionvalue="',$op,'">',$op,'</option>';
}
?>
<optionvalue="increase">增加分類</option>
</select>
</div>
</body>
</html>
E. 使用ajax+jquery+php訪問mysql資料庫,並且達到不跳轉頁面的效果。。。
完整代碼如下:
html
<script>
window.onload=function(){
try{
varxmlHttp=newXMLHttpRequest();
}catch(e){
//無法生成對象那麼就是IE瀏覽器或不支持AJAX
try{
varxmlHttp=newActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
varxmlHttp=newActiveXObject("Microsoft.XMLHTTP");
}catch(e){
alert('你必須使用支持AJAX的瀏覽器');
}
}
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4&&xmlHttp.status==200){
//請求完成!
varresult=xmlHttp.responseText;
varre=eval('('+result+')');
varhtmlStr='<tr><td>ID</td><td>NAME</td></tr>';
for(vari=0;i<re.length;i++){
htmlStr+='<tr><td>'+re[i].id+'</td><td>'+re[i].name+'</td></tr>'
}
document.getElementById('result').innerHTML=htmlStr;
}
};
xmlHttp.open('get','../phpLab/index.php',true);
xmlHttp.send(null);
};
</script>
<body>
<divid="showResult">
<tableid="result">
</table>
<div>
</body>
php測試 代碼
<?php
//資料庫只有兩個欄位id,name
$handle=mysql_connect('localhost','root','');
mysql_select_db('test',$handle);
$sql="select*fromtestTbl";
$result=mysql_query($sql);
$arr=array();
while($r=mysql_fetch_assoc($result)){
$arr[]=$r;
}
echojson_encode($arr);
?>
結果:
F. jquery+php 注冊並檢測完資料庫後form提交數據
我都看不明白你在說什麼,看了好多遍
jquery 檢測的時候總是會被jquery的回調函數牽絆住,我先等注冊完後,提交的時候用form表單提交,而不用到jquery的回調函數:
G. 如何使用jquery php mysql來實現在線測試題,包括動態讀取題目,答題完畢後台評分
<?php
include_once("connect.php");//連接資料庫
$sql = "select * from quiz order by id asc";
$query = mysql_query($sql); //查詢數據
while($row=mysql_fetch_array($query)){
$answers = explode('###',$row['answer']); //將答案選項分開
$arr[] = array(
'question' => $row['id'].'、'.$row['question'], //題目
'answers' => $answers //答案選項
);
}
$json = json_encode($arr); //轉換json格式
?>
H. jQuery 怎麼獲取 php查找資料庫的值
比如資料庫查出的值為$data,
可以使用Json的方式獲取數據,即在PHP端這樣:
<?php
$data = array('a'=>'1');
echo json_encode($data);
?>
在HTML頁面
<script type="text/javascript">
$.ajax({
url:"PHP頁面地址",
dataType:"json",
success:function(data){//data就是PHP頁面輸出的東西了
alert(data.a);
}
});
</script>