【问题标题】:jquery window location not work inside php echo?jquery窗口位置在php echo中不起作用?
【发布时间】:2015-02-20 05:36:37
【问题描述】:

jquery 窗口位置在 php echo 语句中不起作用,我的代码有什么问题吗?

这是我的代码:

<?php
include "connection/db_connection.php";
$error=''; 
if(isset($_POST['submit']))

{ 
$sno=$_POST['sno'];
$name=$_POST['name'];
$country=$_POST['s2'];
$username=$_POST['username'];
$email=$_POST['email'];
$password=$_POST['pwfield'];
$address=$_POST['address'];
$city=$_POST['city'];
$state=$_POST['state']; 

$sql2="select email from login_warehouse WHERE email='".$email."' AND sno!='".$sno."'";
$result2=mysql_query($sql2);
if(mysql_fetch_array($result2)>= 1) 
{

    $error= "Entered E-Mail ID Already in Exists!<br/>";
}
else
{
    $sql=mysql_query("update `login_warehouse` set name='".$name."', username='".$username."', password='".$password."', email='".$email."',address='".$address."',city='".$city."',state='".$state."', country='".$country."' where sno='".$sno."'");
    if($sql)
    {
        echo '<script>
        $(window).load(function () {
        window.setTimeout(function () {
        window.location.href = "edit_userprofile.php";
        }, 900)
        }); 
        </script>';
       $error='Admin details updated successfully!';            
  }
else
{

$error="Customer Already Exists";
}       
}
}
?>      

我在 echo 语句中写错了什么?或者如果条件为真,是否有任何其他方式重新加载页面。

【问题讨论】:

  • 有什么原因不能通过 php 重新定位吗?
  • 检查 firebug 或 chrome 的控制台是否有任何错误?
  • 尝试先使用alert进行验证。
  • 为什么不使用 PHP header() 函数进行重定向?
  • 我试过了,问题不在于 jquery。它来自 php 执行(也检查 jquery 参考),您应该将脚本更改为 pdo 以获得更好的一切

标签: php jquery mysql


【解决方案1】:

您的Javascript 中没有错误。检查您的查询在哪里成功执行。

为什么不使用 PHP 的 header() 来重定向页面。

if($sql)
{
   header("location:edit_userprofile.php");

【讨论】:

    【解决方案2】:

    如果你真的想使用 jQuery/javascript,请确保在任何 jQuery 代码之前包含 jQuery 库。如果您发布的代码是起始代码 Before HTML ,请确保您首先包含 js 库。

    我会说如果你使用 jquery 重定向,你需要加载 DOM(这需要几秒钟),加载窗口,如果你使用 php header(),你将被重定向 Instantly

    【讨论】:

      【解决方案3】:

      由于您使用的是 jQuery 代码,因此您必须在 jQuery 代码执行之前包含 jQuery 库文件。

      【讨论】:

        【解决方案4】:

        您可以使用 PHP header() 重定向到您想要的页面,如下所示:

        if($sql)
        {
            header("location:edit_userprofile.php");
        }
        

        【讨论】:

        • 好的,但我想显示成功消息,10 秒后页面将重定向或重新加载同一页面。
        【解决方案5】:
        <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
        <?php
        include "connection/db_connection.php";
        $error=''; 
        if(isset($_POST['submit']))
        
        { 
        $sno=$_POST['sno'];
        $name=$_POST['name'];
        $country=$_POST['s2'];
        $username=$_POST['username'];
        $email=$_POST['email'];
        $password=$_POST['pwfield'];
        $address=$_POST['address'];
        $city=$_POST['city'];
        $state=$_POST['state']; 
        
        $sql2="select email from login_warehouse WHERE email='".$email."' AND     sno!='".$sno."'";
        $result2=mysql_query($sql2);
        if(mysql_fetch_array($result2)>= 1) 
        {
        
        $error= "Entered E-Mail ID Already in Exists!<br/>";
        }
        else
        {
        $sql=mysql_query("update `login_warehouse` set name='".$name."', username='".$username."', password='".$password."', email='".$email."',address='".$address."',city='".$city."',state='".$state."', country='".$country."' where sno='".$sno."'");
        if($sql)
        {
            echo '<script>
            $(window).load(function () {
            window.setTimeout(function () {
            window.location.href = "edit_userprofile.php";
            }, 900)
            }); 
            </script>';
           $error='Admin details updated successfully!';            
          }
        else
        {
        
        $error="Customer Already Exists";
        }       
        }
        }
        ?> 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-10-17
          • 2012-07-22
          • 2013-06-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-03
          • 1970-01-01
          相关资源
          最近更新 更多