如何将此错误消息发送回PHP中的原始页面?

如何将此错误消息发送回PHP中的原始页面?,php,twitter-bootstrap,Php,Twitter Bootstrap,index.php: 表单位于引导模式中 <form class="form-horizontal" action="core/login.php" method="post"> ... </form> 如何将echo发送回与index.php上的登录表单相同的引导模式 然后在index.php中,您可以返回错误参数并切换它: index.php <? if(isset($_GET['error'])) { if($_GET['error']=='

index.php:

表单位于引导模式中

<form class="form-horizontal" action="core/login.php" method="post">
   ...
</form>
如何将echo发送回与index.php上的登录表单相同的引导模式

然后在index.php中,您可以返回错误参数并切换它:

index.php

<?
  if(isset($_GET['error']))
{
   if($_GET['error']=='loginauth')
  {
    echo "Login failed";
  }
   if($_GET['error']=='error2')
  {
    echo "error 2 description";
  }

}

?>
在模型中使用ajax。jQuery.ajax


Plunker示例使用jquery.ajax或.Post发布数据

首先,您必须使用jquery从表单中获取数据:

var form_values = { 
                    value_name1 : $('#selector1').val(),
                    value_name2 : $('#selector2').val()
                  };
然后使用将数据发送到处理php脚本,php脚本应返回json格式的验证消息,并使用json_encode:

$.post('/url/path', form_values, callback(data));
回调函数将保存您的代码,即post调用完成后发生的情况(失败并出现错误或成功):

function callback(data)
{
   //Decode received message with JSON.parse if it is available or $.parseJSON if not
   data = JSON && JSON.parse(data) || $.parseJSON(data);

   //Check message variable received from php
   if (data.message == "Login Failed")
   {
       //Do whatever you want here, I`ve used alert to be simpler
       alert('Failed to login');
   } else {
       alert('Succesfully logged in');
   }
}

显示错误的简单方法是..调用index.php中login.php的所有代码…例如:-使用ifisset$_POST{**这是您的代码**}您可以将错误附加到url index.php?error=xyz,然后在索引页上打印错误值(如果它不为null)。感谢您的回答!你能举一个例子说明如何在我的情况下使用它吗?请用一个例子来改进你的答案好的,我会做一个总结jsfiddle@Didar_Uranov对不起,我还是不明白。我知道你在做什么,但我不明白这一切在我现有的代码中应该放在哪里。
$.post('/url/path', form_values, callback(data));
function callback(data)
{
   //Decode received message with JSON.parse if it is available or $.parseJSON if not
   data = JSON && JSON.parse(data) || $.parseJSON(data);

   //Check message variable received from php
   if (data.message == "Login Failed")
   {
       //Do whatever you want here, I`ve used alert to be simpler
       alert('Failed to login');
   } else {
       alert('Succesfully logged in');
   }
}