-2

i have variables in php I want to sent to a function in javascript my php is like this

if (isset($_POST['acceptbut'])) {
**other code***
$date1 = $vals['date1'];
$date2 = $vals['date2'];
$nme = $vals['nme'];
 how would I call the addMyEvent and send those variables, cant seem to find  
 a way anywhere to send them to the javascript function
 }

I got these values from database using a foreach loop

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
Cormac Hallinan
  • 187
  • 1
  • 12
  • you need add more code to your question. – Adrian Cid Almaguer Feb 20 '15 at 19:05
  • possible duplicate of [How to call a JavaScript function from PHP?](http://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php) – Peter Noble Feb 20 '15 at 19:05
  • Based on what you've provided, you can't do what you're asking. You can make calls from javascript to php using ajax, but I don't know how you'd call javascript from php, other then dynamically generating your javascript and inserting those values in the rendered javascript. It sounds problematic and leads me to think that you might not be coming up with the best solution to your problem. – Halfstop Feb 20 '15 at 19:09

2 Answers2

2

What you can do is using ajax for your $_POST or $_GET request.

PHP myserver.php:

if (isset($_POST['acceptbut'])) {
  // i do something here...
  // you could either echo back text or JSON
  echo "Blah Blah Blah"; 
}

Javascript:

$.post("myserver.php", function(data){
    // I do something here with the data I received
    window.alert(data); // it should says Blah Blah Blah
});
Lance
  • 865
  • 8
  • 19
0

PHP is a server-side and javascript is cliente-side, you can do that.

But you can form your javascript function with php. Something like this

    echo '<script type="text/javascript">' .
   'function(' . $var1 . ');' .
   '</script>'
;
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
  • 1
    And you could dump PHP variable values in that outputted javascript, but it's totally hackish. – Halfstop Feb 20 '15 at 19:10
  • Yeah that works, but I would be cautious about doing stuff like that. It seems ripe for crazy bugs. But it would work and I'm certain I've done something like this before. – Halfstop Feb 20 '15 at 19:13