-2

Self Answer

I have a script that is supposed to use AJAX to retrieve a video from Youtube, but, it requires AJAX to grab a PHP file. This PHP file needs Javascript variable to work, however, I could not seem to find a way to do this on this website. I found a way do to it with Jquery on this Qoura question.

But if you don't want to go there for whatever reason(I made some adjustments to the PHP code because it was created very poorly), to make a Javascript variable a PHP variable, you would use this (I'm not incredibly skilled with jQuery at all so I won't attempt to explain it, and also, keep in mind that these are just basic examples, this easily can be abstracted in multiple ways)

Jquery:

var username=$('input').val();
$.ajax({
url:'link_to_php_file',
method:'get',
data:{name:username},
success:function(data)
{
alert("Success");
},
error:function(data)
{
alert("error");
}
});

and the PHP for this is

PHP:

<?php echo $_GET['username']; ?>

Making a PHP variable into a Javascript variable is much simpler, however, is not a replacement for the above.

PHP

<?php echo "<script>showname('" . $_GET['username'] . "')</script>"

JavaScript

<script>
    function showname(username) {
        alert(username);
    };
</script>
Beaux
  • 21
  • 1
  • 1
  • 8
  • It's just `$_GET['name']`, as that's what you've named it. – adeneo Sep 25 '16 at 00:05
  • There's a checkbox when posting that allows you to write a self answer and post it at the same time... – Andrew Li Sep 25 '16 at 00:08
  • 1
    _I could not seem to find a way to do this on this website_ - copy-paste your question title into Google. I assure you this topic has been thoroughly covered **many** times. – rjdown Sep 25 '16 at 00:28
  • @Chris I do not understand how this could in any sense be a duplicate of that question – Beaux Sep 25 '16 at 00:48
  • 1
    I'm voting to close this question as off-topic because it doesn't actually ask a question. If you are posting a "self answer" question as a public service you should still phrase it as a question, and then post the solution as an answer. Also, the first part seems to be saying "can't do it with Javascript but can with jQuery" - not sure if that's what you meant, but it is wrong: using jQuery *is* using Javascript; jQuery can't do anything that JS can't. – nnnnnn Sep 25 '16 at 01:51

2 Answers2

1

How to use PHP variable inside JavaScript code:

<script type="text/javascript>
    var stack = <?php echo $overflow; ?>;
</script>

I mean it is pretty simple. This code must be near PHP code that have $overflow variable itself.

If you want JavaScript variable inside PHP code then single way is using AJAX

<script type="text/javascript>
    $.ajax({
        type: "POST",
        url: "http://domain-name.com/ask.php",
        data: { name: "Stackoverflow" },
        success: function() { alert('Yes, I work'); },
        dataType: "json"
    });
</script>

More detail about JQuery & AJAX you will see in official documentation. Now In you ask.php you have:

$nameForSomethink = $_POST['name'];

After, you can do anything that you need.

I hope you understand my idea.

  • I suppose you hadn't noticed this was a self-answer question but thanks for your answer – Beaux Sep 25 '16 at 01:43
  • @Beaux: You should make it an actual self _answer_ next time, by posting question and answer separately. https://stackoverflow.com/help/self-answer – CBroe Sep 25 '16 at 01:46
0
$.ajax({
url:'link.php',
method:'get',
dataType: "json",
data:{name:youtubevideo},
success:function(data)
{
$('.video').attr("src", data[0]);
},
error:function(data)
{
alert("error");
}
});


   <?php
     $return = array();
     database or other stuff $_GET['name']
       foreach ($rows as $row){
          array_push($return, $row['url']);
       }
     echo json_encode($return);
   ?>

not sure something like this?

pepijn
  • 44
  • 1
  • 6