2012-04-11 59 views
0

我有這段代碼來向數據庫添加一條新記錄。 運行後,我需要使用jQuery將用戶重定向到成功頁面。通過jQuery重定向

<? session_start(); 
include "sec.php"; 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<link rel="stylesheet" type="text/css" href="../scripts/forms.css"> 
<head> 
<script type="text/javascript" src="js/jquery.min.js"></script> 
<script language="javascript" type="text/javascript" src="js/datetimepicker.js"></script> 
<script language="javascript"> 
    $(document).ready(function() { 
     $("[required=true]").after("<span></span>"); 
     $("#submit").click(function(){ 
      var error=false; 
      $("span").text(""); 
      $("[class~=Red]").removeClass("Red"); 
      if(trim($("#VisaDocNo").val())==""){ 
       $("#VisaDocNo").addClass("Red"); 
       error=true; 
      } 
      if(trim($("#VisaCountry").val())==""){ 
       $("#VisaCountry").addClass("Red"); 
       error=true; 
      } 
      if(trim($("#CustNo").val())==""){ 
       $("#CustNo").addClass("Red"); 
       error=true; 
      } 
      $("[class=Red]").eq(0).focus(); 
      return !error; 
     }); 
    }); 
    function trim(x) { 
     return x.replace(/^\s+|$\s+/,''); 
    } 
    function isEmail(str) 
    { 
     var regex = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; 
     return regex.test(str); 
    } 
</script> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>اضافة تأشيرة جديدة</title> 
</head> 
<body topmargin="0"> 
<?php 
include("../functions/connect.php"); 
if(!empty($action)){ 
$sql="select * from visa where VisaNo='$VisaNo'"; 
$q=mysql_query($sql); 
$n=mysql_num_rows($q); 
if($n==1){ 
echo "<script>window.alert('عفوا ,,, السجل مسجل ممسبقا.')</script>"; 
} 
else{ 
if($ActFollow!=1) 
    $ActFollow=0; 

$sql="insert into visa 
(VisaNo,VisaDate,CustNo,VisaDocNo,VisaCountry,VisaDoneDate,VisaType,SysUser,Notify,Notes,FileNo) 
values 
(0,CURDATE(),'$CustNo','$VisaDocNo','$VisaCountry','$VisaDoneDate','$VisaType','$SysUser','$Notify','$Notes','$FileNo')"; 
$q=mysql_query($sql); 
if($q){ 
echo "<script>window.alert('تمت اضافة سجل جديد.')</script>"; 
    $insert_page = mysql_insert_id(); 
    echo "<script type='text/javascript'>window.location='editvisa.php?change=$insert_page'</script>"; 
} 
else{ 
echo "<script>window.alert('خطأ في اضافة سجل جديد.')</script>"; 
} 
} 
} 

include "visamenu.php"; 
?> 
<div class="theform"> 
<table width="777" border="0" align="center" cellspacing="1" > 
<form action="" method="post" name="visa" dir="rtl"> 
     <tr > 
      <td colspan="2" class="xtt" height="30"><div align="center">اضافة سجل جديد</div></td> 
     </tr> 
     <tr> 
      <td width="553" align="right" class="xtd"> 
      <input id="preview" name="CustNo" required="true" dir="rtl" value="<?=$r[CustomersNo]?>" size="50" readonly="readonly"> 
<button type="button" name="choice" onClick="window.open('chs_customer.php','popuppage','width=900,toolbar=0,resizable=1,scrollbars=no,height=500,top=100,left=100');">عرض الزبائن</button> 
      </td> 
      <td align="right" class="xtx"><div align="right">اسم الزبون</div></td> 
     </tr> 
     <tr> 
      <td align="right" class="xtd"><input name="VisaDocNo" required="true" type="text" id="VisaDocNo" dir="rtl" lang="ar" size="50" maxlength="40" /></td> 
      <td align="right" class="xtx"><div align="right">رقم الفيزا</div></td> 
     </tr> 
     <tr> 
      <td align="right" class="xtd"><input name="VisaCountry" required="true" type="text" id="VisaCountry" dir="rtl" lang="ar" size="50" maxlength="40" /></td> 
      <td align="right" class="xtx"><div align="right">دولة الذهاب - التأشيرة</div></td> 
     </tr> 
       <tr> 
      <td align="right" class="xtd"><input name="VisaDoneDate" type="text" id="Date" dir="rtl" lang="ar" size="50" maxlength="40" /> 
      <a href="javascript:NewCal('Date','ddmmyyyy')"><img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a> 
      </td> 
      <td align="right" class="xtx"><div align="right">تاريخ دور التأشيرة</div></td> 
     </tr> 
     <tr> 
      <td align="right" class="xtd"><input name="VisaType" type="text" id="VisaType" dir="rtl" lang="ar" size="50" maxlength="40" /></td> 
      <td align="right" class="xtx"><div align="right">نوع الفيزا</div></td> 
     </tr> 
     <tr> 
      <td align="right" class="xtd"><input name="SysUser" type="text" readonly="readonly" id="SysUser" value="<? echo $_SESSION[aname]?>" size="50" maxlength="40" dir="rtl"/></td> 
      <td align="right" class="xtx"><div align="right">الموظف المسؤول </div></td> 
     </tr> 
     <tr> 
      <td align="right" class="xtd"><input name="Notify" type="checkbox" id="Notify" value="1"/></td> 
      <td align="right" class="xtx"><div align="right">تنبيه</div></td> 

     </tr> 
     <tr> 
      <td align="right" class="xtd"><input name="Notes" type="text" id="Notes" size="50" maxlength="40" dir="rtl"/> 
      </td> 
      <td align="right" class="xtx"><div align="right">ملاحظات </div></td> 
      </tr> 
      <tr> 
      <td align="right" class="xtd"><input name="FileNo" type="file" disabled="disabled" id="FileNo" size="41" maxlength="40" dir="rtl"/></td> 
      <td align="right" class="xtx"><div align="right">ملفات مرفقة</div></td> 
      </tr>  <tr> 
      <td align="right" class="xtd"><label> 
      <input type="submit" name="Submit" value="اضافة التأشيرة" onClick="return validate()"> 
      </label></td> 
      <td class="xtd"><input name="action" type="hidden" id="action" value="done" /></td> 
     </tr> 
     </form> 
</table> 
</div> 
</body> 
</html> 

添加新記錄之後,我們重定向到使用這種方法的另一頁:

 $insert_page = mysql_insert_id(); 
    echo "<script type='text/javascript'>window.location='editvisa.php?change=$insert_page'</script>"; 

我如何使用jQuery重定向?

+1

jquery不這樣做,使用普通的js。見[這個主題] [1]。 [1]:http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript – Alex 2012-04-11 03:52:52

+0

請告訴我,要麼sec.php或connect.php是否會導致數據庫轉義?你的代碼看起來對SQL注入非常開放。 – 2012-04-11 03:53:14

+0

無論你在你的代碼中使用jQuery是否足夠:-) – codef0rmer 2012-04-11 06:10:24

回答

0

這樣

<?php 
$insert_page = mysql_insert_id(); 
?> 
<script type='text/javascript'> 
window.location='editvisa.php?change=<?php echo $insert_page;?>' 
</script>; 
+0

謝謝,但我不會通過使用ajax而不是JavaScript重定向此鏈接:) – AbuKotsh 2012-04-17 03:57:25

+0

通過ajax重定向「此鏈接」?你在說什麼?我甚至沒有 - – SoonDead 2013-05-02 16:15:52

+0

謝謝..現在它的工作 – AbuKotsh 2018-02-12 07:53:27