-2

I need to put a javascript variable in js file to php variable in php file to do action in sql db later. I use local server Apache to do this. My javascript variable is "uid"

In my js file I've insert this :

function post()
{
  $.ajax({
    type: "POST",
    url: "proc.php",
    data: uid, 

  });   
}

In my php file named "proc.php" the code is :

<?php

$myvar = $_POST['uid'];
echo 'myvar';

?>

Is it possible to display by echo "myvar" in alert box to know if my code works ?

  • 4
    you will need php or node.js for this, you can't update your db with client side javascript – baao Dec 19 '14 at 23:43
  • Read this maybe help: http://stackoverflow.com/questions/857670/how-to-connect-to-sql-server-database-from-javascript – QMaster Dec 19 '14 at 23:45
  • @baao yes you can although its not recommended since it exposes your connection string to the public – meda Dec 19 '14 at 23:45
  • @meda I have to admit I never thought about the possibility because of security concerns. But it seems that you are right. – baao Dec 19 '14 at 23:47
  • I've try to send it with Ajax $.ajax({ type: "POST", url: "proc.php", data: "duration", success: function(msg){ alert( "Data Saved: " + msg ); } }); – Nicolas Simon Dec 22 '14 at 09:04

1 Answers1

0

My problem was data are'nt in Json format :

This code works fine :

In pour js file :

     var data = {
        fn: "filename", // json format
        test : "...",
        ....
                };

     $.post("proc.php", data);

And in your php file "proc.php", you can get the variable with :

$nom = $_POST['fn'];
$id = $_POST['test'];
$var = ...

After you can display data with an echo, and insert it in a div