sunny sunny - 5 months ago 18
jQuery Question

How to remove Jquery PHP echo error?

I have a code of web form which have two fields Name with Date and Title. I use jquery in my code to display current date with calendar and when i change date in Date field it automatically change in Title field. I have some Hard code in Title field. Now my problem is that when i change date hard code is not display and my php code which is another file could not get value of title and could not store any value in data base mysql.

Here is my code.

d2.php

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(document).ready(function() {
$("#mydate").datepicker({
dateFormat: "dd-M-y",
onSelect: function(dateText, inst) {
$("#dt_title").html(dateText);
}
}).datepicker("setDate", new Date());
});
</script>
</head>
<body>

<form action="pd1.php" method="POST" enctype="multipart/form-data">

<table width="547" class="tblbdr" >
<tr>
<td height="23" colspan="6" class="head"><p> Morning Breifing</p></td>
</tr>
<tr> <td height="10"></td></tr>
<tr><td class="celltext"><b>Date:</b> </td> <td><input name="mydate" type="text" id="mydate" style="width:200px"> </td></tr>
<tr>

<tr><td class="celltext"><b>Title: </b><br> </td>

<td class="celltext" style="width:200px" ><span id="dt_title"> <input name="title" type="text" value=" MB | Falcons |
<?php echo date("d-M-y");?>" style="width:200px"/> </span></b> </td> </tr>
</tr>
<td class="celltext"><b>Upload File:</b></td>
<td colspan="4" bordercolorlight="#006666">
<input type="file" name="myfile" id="myfile" width="100%" size=80/>
</td></tr>
<td><input type="submit" value="Save"/> </td> <td> </td>
<td width="151">

</td>
<tr>
<td height="12">
</td>
<td width="290">

</td> </tr>
</table>
</form>
</body>
</html>


Here is my php code

Output without change in date

output1

Output after change in date

output 2

Answer

By $("#dt_title").html(dateText); you delete input field inside your span, and only write plain date text. You should set value of your title input instead of spans html. Fixed code:

onSelect: function(dateText, inst) {
            $("#dt_title input[type='text']").val(dateText);
        }

To keep title in input & only change date, you need to save title in additional attribute of input:

<input name="title" type="text" value=" MB | Falcons | <?php echo date("d-M-y");?>" data-title="MB | Falcons | " style="width:200px"/>

And then in jQuery change input value by adding title from attribute:

$("#dt_title input[type='text']").val($("#dt_title input[type='text']").attr('data-title')+dateText);