1

我在格式化日期选择器中的日期以将其插入 mysql 数据库时遇到问题。如您所见,日期选择器格式是yy-mm-dd,我需要简单的Ymd mysql 格式。我尝试了很多选项,例如将字符串“\”替换为“-”和 strtotime() 以及两者一起,但它不起作用。你能帮我吗?

我的代码是:

HTML:

<script>
  $(document).ready(function() {
    $("#datepicker").datepicker({
        dateFormat: 'yy-mm-dd'
    });
  });
  </script>

</head>

<body>
    <div id="wrap"> <!--wrap start-->
        <br />
        <h1>Zbiranje podatkov</h1>
         <form action="" method="post" id="mainform">
        <table id="table_data">

            <tr> 
                <td>Ime</td>
                <td><input name="fname" type="text" size="40"></td>
                <td><span class="fname_val validation"></span></td>
            </tr>
            <tr> 
                <td>Priimek</td>
                <td><input name="lname" type="text" size="40"></td>
                <td><span class="lname_val validation"></span></td>

            </tr>
            <tr> 
                <td>E-pošta</td>
                <td><input name="email" type="text" size="40"></td>
                <td><span class="email_val validation"></span></td>
            </tr>
            <tr> 
                <td>Datum</td>
                <td><input id="datepicker" size="40" name="datepicker" type="text" /></td>
                <td><span class="datepicker_val validation"></span></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input name="register" type="button" value="Potrdi"> <span class="loading"></span></td>
                <td>&nbsp;</td>
            </tr>
        </table>
        <div id="note">
        <b>Email Already Exist:</b> <span style="color:#B41F2B;">john_doe@istockphp.com</span>
        </div>
       </form>
     </div> <!--wrap end-->
  </body>
</html>

而 PHP 是:

<?php
require_once("database.php"); //
header('Content-Type: text/html; charset=utf-8');

/* catch the post data from ajax */
$fname = $_POST['fname']; 
$lname = $_POST['lname'];
$email = $_POST['email'];
$date = date('Y-m-d',  strtotime($_POST['datepicker']));

$query = mysql_query("SELECT `email` FROM `contacts` WHERE `email` = '$email'");
if(mysql_num_rows($query) == 1) { // if return 1, email exist.
    echo '1';
} else { // else not, insert to the table

    $query = mysql_query("INSERT INTO `contacts` (`name` ,`surname` , `email`, `birthday`, `insert_timestamp`)
                        VALUES ('$fname', '$lname', '$email', '$date', NOW())"); 

  }

?>

JS代码在这里:

jQuery(function($) {
    var val_holder;

    $("form input[name='register']").click(function() { // triggred click 

        /************** form validation **************/
        val_holder      = 0;
        var fname       = jQuery.trim($("form input[name='fname']").val()); // first name field
        var lname       = jQuery.trim($("form input[name='lname']").val()); // last name field
        var email       = jQuery.trim($("form input[name='email']").val()); // email field
                var datepicker      = jQuery.trim($("form input[name='datepicker']").val()); // datum field
        var email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; // reg ex email check  

        if(fname == "") {
            $("span.fname_val").html("Prosimo vpišite ime.");
        val_holder = 1;
        } 
        if(lname == "") {
            $("span.lname_val").html("Prosimo, vpišite priimek.");
        val_holder = 1;
        } 
        if(email == "") {
            $("span.email_val").html("Prosimo, vpišite emial.");
        val_holder = 1;
        }
                if(datepicker == "") {
            $("span.datepicker_val").html("Prosimo, izberite datum.");
        val_holder = 1;
        }
        if(email != "") {
            if(!email_regex.test(email)){ // if invalid email
                $("span.email_val").html("Email ni pravilen.");
                val_holder = 1;
            } 
        }
        if(val_holder == 1) {
            return false;
        }  
        val_holder = 0;
        /************** form validation end **************/

        /************** start: email exist function and etc. **************/
        $("span.loading").html("<img src='images/ajax_fb_loader.gif'>");
        $("span.validation").html("");

        var datastring = 'fname='+ fname +'&lname='+ lname +'&email='+ email; // get data in the form manual
        //var datastring = $('form#mainform').serialize(); // or use serialize

        $.ajax({
                    type: "POST", // type
                    url: "check_email.php", // request file the 'check_email.php'
                    data: datastring, // post the data
                    success: function(responseText) { // get the response
                        if(responseText == 1) { // if the response is 1
                            $("span.email_val").html("<img src='images/invalid.png'> E-poštni naslov že obstaja.");
                            $("span.loading").html("");
                        } else { // else blank response
                            if(responseText == "") { 
                                $("span.loading").html("<img src='images/correct.png'> Registracije je uspešna.");
                                $("span.validation").html("");
                                $("form input[type='text']").val(''); // optional: empty the field after registration
                            }
                        }
                    } // end success
        }); // ajax end
        /************** end: email exist function and etc. **************/
    }); // click end
}); // jquery end
4

2 回答 2

3

使用日期时间()

$date = DateTime::createFromFormat('y-m-d', $_POST['datepicker']);
echo $date->format('Y-m-d');
于 2013-08-28T21:25:16.843 回答
1

问题出在JS代码中......

var datastring = 'fname='+ fname +'&lname='+ lname +'&email='+ email; 

我忘了为日期声明 var ......

于 2013-12-17T18:11:36.800 回答