2017-07-31 66 views
0

我试图找出如何使可用时,它是通过HTML表单提交,并作为cookie存储提交后立即编辑一些数据...显示表单提交的数据值与PHP验证无刷新

在顶部,表单验证:

<?php 
//check if form was sent 
if(isset($_POST['send'])){ 
    //some basic validation 
    if(strlen($_POST['myname']) > 2){ 
    //set the cookie if passed 
    setcookie('myname', $_POST['myname']); 
    } 
} 
//set error if didn't pass validation 
else { 
    $error[] = 'Your name is required!'; 
} 
?> 

因此,如果用户帖子的数据会抓住它,并将其存储在cookie中。 和如果不是,将给出一个错误:

<?php 
    //check for errors 
    if(isset($error)){ 
    foreach($error as $error){ 
    echo '<p style="background: red;">'.$error.'</p>'; 
    } 
    } 
?> 

HTML表单用PHP:

<form method="post"> 
    <input name="myname" type="text" value="<?php if(isset($_COOKIE["myname"])){ 
    echo $_COOKIE["myname"];} ?>" placeholder="Your Name"> 
    <input name="send" type="submit" value="send"> 
</form> 

这用于收集数据并发送它通过PHP验证为了设定一个曲奇饼。如果cookie已经设置,我希望它显示内联PHP代码的值。

会发生什么情况是当表单被提交时,cookie被存储,但用户只有在刷新页面后才能看到它,我可以添加标题(“Refresh:0”);但它不能解决问题,因为错误按摩在刷新时不会显示。

我需要使用它的几种形式,所以目前我正在寻找一个快速和简单的PHP解决方案,但它也可以是一个合适的JQuery/AJAX片段。

+1

要达到此目的,无需刷新/重新加载页面,您将需要使用ajax – RamRaider

+0

您是否已经尝试使用AJAX?jQuery'.validate()'也非常有用。 – Difster

+0

没有使用AJAX,不知道当PHP在同一个文件上时该怎么做。我也使用Jquery验证器,不确定如何将它放在一起工作。 – Hakerovsky

回答

0

你只能通过jQuery做到这一点,AJAX

获得cookie值 -

jQuery的 VAR ckVal = $ .cookie( “cookiename”)

的JavaScript -

function readCookie(name) { 
      var nameEQ = name + "="; 
      var ca = document.cookie.split(';'); 
      for (var i = 0; i < ca.length; i++) { 
       var c = ca[i]; 
       while (c.charAt(0) == ' ') c = c.substring(1, c.length); 
       if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); 
      } 
      return null; 
     } 
0

1st:正如你在comment中提到的form and php这两个代码都在same page这么简单提交form这样。不需要ajax

第二:如有错误意味着你简单展示了error message并把submited valuevalue attribute这样

value="<?php if(isset($_POST['myname'])){ echo $_POST['myname']; } ?>" 

PHP:

<?php 

if(isset($_POST['send'])){ 

    if(strlen($_POST['myname']) > 2){ 

    // here you can do whatever you want 
    // header('Location:next_page.php'); 

    }else{ 

    $error[] = 'Your name is required! and it should be minimum three character '; 
    } 


} 

    if(isset($error)){ 
    foreach($error as $error){ 
    echo '<p style="background: red;">'.$error.'</p>'; 
    } 
    } 
?> 

The HTML form with PHP: 

<form method="post"> 
    <input name="myname" type="text" value="<?php if(isset($_POST['myname'])){ 
    echo $_POST['myname']; } ?>" placeholder="Your Name"> 
    <input name="send" type="submit" value="send"> 
</form> 
0

在web2的,我们能做到这一点通过使用AJAX。简单地打在形式透过Ajax和

$('#submit_btn').on('click', function (e) { 
    e.preventDefault(); 
    var $this = $('#form_id'); 
    $.ajax({ 
     url: 'setcookie.php', 
     data: $this.serialize(), 
     dataType: 'html', 
     type: 'POST', 
     beforeSend: function() { 
      // show loader 
     }, 
     success: function (html) { 
      // Show success message 
      // Set value of input elements by reading cookie in JS 
     }, 
     error: function() { 
      // Show ValidationError 
     } 
    }); }); 

在cookie.php放服务器端验证的形式input.You可以让同一个页面,如果你还想要把代码相同的页面上的AJAX。