0

I would like to use a value from PHP in JavaScript, but it's not working. I fetch data from a database and store it in a PHP variable and for conditional formatting. I need to also use the data in JavaScript for validation.

The value required by me is $date = date('Y-m-d'); If I pass the value by json Encode the alert function is not working...

$(document).ready(function()
{
alert("Hi");
var array = php print(json_encode($date)); 
});

Whereas if I just alert the function without...

var array = php print(json_encode($date)); ;

...it works fine.

<script type="text/javascript" src="assests/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
alert("Hi");
var array = '<?php print(json_encode($date)); ?>';
});
</script>
</head>
<body>
<form action="majorprocess.php">
<?php
$dbconn = mysql_connect('localhost', 'root','' );
if(!$dbconn)
{
die(mysql_error());
}
else
{
$date = date('Y-m-d');
Chad Nouis
  • 6,861
  • 1
  • 27
  • 28
user1894647
  • 613
  • 3
  • 11
  • 22
  • Try using `` then you can get it by using `document.getElementById`. Store your data in hidden field so that you can use them with js – Hearner Jul 22 '15 at 13:22
  • `$date` is not defined when you pass it to `json_encode` – Soana Jul 22 '15 at 13:39
  • Warning: You are using the `mysql_xxx()` functions for your database access. These functions are deprecated, and are being removed in the next version of the language. You are strongly advised to migrate your code to use the other methods available -- either the `mysqli_xxx()` functions, or the PDO library. See also: [Why shouldn't I use the mysql functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Simba Jul 22 '15 at 13:48

4 Answers4

1
<script type="text/javascript" src="assests/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
    alert("<?php echo json_encode(date('Y-m-d')); ?>");
});
</script>
tesst
  • 149
  • 1
  • 11
0

What is the resulting HTML? There could be a PHP error outputting instead of valid javascript.

Try putting the PHP section at the top maybe?

When asking a question it is always best to post any outputs you are getting so people can help.

Christian
  • 1,557
  • 11
  • 16
0

try this one

     </head>
      <body>
      <form action="majorprocess.php">
      <?php
       $dbconn = mysql_connect('localhost', 'root','' );
        if(!$dbconn)
        {
         die(mysql_error());
        }
         else
            {
             $date = date('Y-m-d');
            }
        ?>
        <script type="text/javascript" src="assests/js/jquery-1.10.2.js"></script>
        <script type="text/javascript">
         $(document).ready(function(){
          var array = '<?php print(json_encode($date)); ?>';
           alert(array);
           });
          </script>
Gulim Shah
  • 194
  • 7
0

You are using the PHP $date variable before setting its value. Make sure to move the php block before you try to output the date:

<?php
$dbconn = mysql_connect('localhost', 'root','' );
if(!$dbconn)
{
    die(mysql_error());
}
else
{
    $date = date('Y-m-d');
}
?>
<script type="text/javascript" src="assests/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
    alert("<?php echo json_encode($date) ?>");
});
</script>