如何在PHP中调整保证金会话Flash?

时间:2019-10-17 21:57:50

标签: javascript php jquery html css

我正在尝试在数据存储成功时刷新成功消息。我的数据库代码和其他所有东西都工作正常,但是当我给出$ _SESSION成功消息时,它总是处于导航状态。

1)我想知道php中是否有任何方法可以设置$ _SESSION ['success_flash']消息的边距 2)是否有类似/ n / n类型的方式,以便我的Flash消息将在下面显示两行 3)

我的真实代码是:

$_SESSION['success_flash'] = '\n\n<span style="color:#FFFFFF;text-align:center;">Data Saved Successfully!</span>';

我尝试了/ n / n,但是没有用:

$_SESSION['success_flash'] = '\n\n<span style="color:#FFFFFF;text-align:center;">Data Saved Successfully!</span>';

我尝试过style =“ margin-top:200px;”但不起作用:

$_SESSION['success_flash'] = '<span style="margin-top:200px;color:#FFFFFF;text-align:center;">Data Saved Successfully!</span>';

请检查下面的图像,导航栏和文字消息“数据保存成功!”下仅显示绿色背景。因为它隐藏在导航下而没有显示。

enter image description here

在Flash消息和代码结束后,我还要使用以下代码。

echo "<script type='text/javascript'> document.location = 'index.php'; </script>";

任何想法或建议都会有所帮助。 谢谢。

我正在获取数据并将数据成功传递到数据库。我只想在php调用后重新加载页面,并向用户显示成功Flash。

我的Ajax调用:在我的php页面顶部。

<script>
function createD () {

var data = {
     'name'              : jQuery('#name').val(),
     'phone'             : jQuery('#phone').val(),
   };  

 //Ajax call Start Here
 jQuery.ajax({
   url : '/mycodpage/includes/codfile/product.php',
   method : 'POST',
   data : data,
   success : function(data){

     if (data != 'passed') {
       // This will show error
       jQuery('#modal_errors_1').html(data);
     }

     if (data == 'passed') {
       //clear the errors if any
       jQuery('#modal_errors_1').html("");
       location.reload();
     }
   },
   error : function (){alert("Something went wrong.");},


 });     //Ajax call End Here

  }

} // Function End

</script>

3 个答案:

答案 0 :(得分:0)

PHP一旦加载,它就不会在页面上更改,它不是动态的。这样做的方法很奇怪,但是如果在将数据保存到数据库中之后立即设置$ _SESSION ['success_flash']并重新加载页面,则可以完成此操作。

我认为最好的方法是在此处使用ajax,而ajax响应可以将菜单下的容器更改为“成功”。

但是我不知道您如何发送数据,是通过POST形式发送还是以其他方式发送?

答案 1 :(得分:0)

添加display:inline-block;或显示:跨度的样式。

只要dom元素的行为可以取决于外部dom对象(并且您未指定它们):如果仍然不起作用,请尝试使用padding代替margin,如果仍然不起作用,则将两个span标签放在另一个内部两者都带有inline-block。

使用$ _SESSION进行的所有操作都有些奇怪,但是使用XD ...但是根据您的描述,我认为它已经显示了跨度,但是没有空白...但是您不应以这种方式使用$ _SESSION。 / p>

答案 2 :(得分:0)

好的,现在我们到了某个地方。但是现在location.reload是不必要的,因为如果您将收到“通过”的答案,则可以设置通讯而无需重新加载(并且无需在会话变量中使用任何代码,就可以摆脱$ _SESSION ['success_flash']),如下所示。

请注意,我不知道绿色DIV的CSS类或ID是什么(如果有的话),因此对于此特定示例,我将假定它具有类“ .msg-response”。

<script>
function createD () {

var data = {
     'name'              : jQuery('#name').val(),
     'phone'             : jQuery('#phone').val(),
   };  

 //Ajax call Start Here
 jQuery.ajax({
   url : '/mycodpage/includes/codfile/product.php',
   method : 'POST',
   data : data,
   success : function(data){

     if (data != 'passed') {
       // This will show error
       jQuery('#modal_errors_1').html(data);
     }

     if (data == 'passed') {
       //clear the errors if any
       jQuery('#modal_errors_1').html("");

       // this new line will change the content of your message div
       $('div.msg-response').html('<span style="margin-top:200px;color:#FFFFFF;text-align:center;">Data Saved Successfully!</span>');

     }
   },
   error : function (){alert("Something went wrong.");},


 });     //Ajax call End Here

  }

} // Function End

</script>

这里也存在安全隐患:“如果(数据!='通过')”-当发生意外情况时,您将回显PHP错误(路径,文件名,变量,类,代码行等),除非您会在您的服务器上关闭display_errors(显示错误仅应在开发模式下启用,这是一个好习惯)。

为避免这种情况,您可以在此处使用JSON响应,处理“错误”状态,“成功”状态,如果JSON响应不同于“成功”或“错误”,则不执行任何操作。