简体   繁体   中英

php $_POST return empty array

i thoroughly tried many method on this forum for this topic but i can't solve my problem.

so here's the problem. i can't return my $_POST value from the form, but javascript's document.getElementById().value can catch the form value.

here's my form code:

<form action="#" method="post" enctype="text/plain">
    <input id="input_date" name="input_date" class="datepicker" placeholder="Choose date" type="text"/>
    <input id="submit" name="submit" type="button" value="Submit here" onclick="Submit_date()"/>
</form>

this is my php code in the same file:

$tanggal = date('yyyy-mm-dd', strtotime($_POST['input_date']));

this is my javascript code also in the same file:

var tanggalan = document.getElementById("input_date").value;
confirm("Date1: " + tanggalan + 
"\n Date2: " + <?php echo json_encode($_POST['input_date'])?> + 
"\n Date3: " + <?php echo json_encode($tanggal)?>);

for example, the form is writed "2 March, 2016" , the Date1 returns exactly what i want, but Date2 returns "null" and Date3 returns "70707070-0101-0101".

what am i doing wrong?

"\n Date2: " + <?php echo json_encode($_POST['tanggal'])?>

Date2 is null simply because your calling $_POST variable that does not exist.

$tanggal is a variable not a $_POST variable as you assigned here

$tanggal = date('yyyy-mm-dd', strtotime($_POST['input_date']));

Make it $tanggal = $_POST['tanggal'] = date('yyyy-mm-dd', strtotime($_POST['input_date'])); to make it work.

Remove enctype="text/plain" and add action to the form which helps you setting $_POST when you submit the form.

Also, use the following in the PHP code to avoid returning "70707070-0101-0101"

$tanggal = date('Ym-d', strtotime($_POST['input_date']));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM