-2

im making something and this code isnt working

<script src='jq.js'></script>
<script>
function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}

nick = getURLParameter('kagNick')
</script>
<?php
$url = "https://api.kag2d.com/player/"+nick+"/status";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
?>

<script>
status = $result.split("\n")
if(status[4].indexOf("true,") != -1)
    document.write("gold player")
</script>

What is wrong with the code? What I´m trying to do: -mazeygg.com/penis?kagNick=Mazey return ´gold player´ in the first js/jq part: make the /penis.html*?kagNick=Nickname* (nickname = mazey, for example) in the second part, php: connect to the website in the third part, js again: split the website and print something (Check http://api.kag2d.com/player/mazey/status)

I chose this because I did not find a proper way to do the php part in JS (I code js, not php), if you know how: it´s really appreciated! If you do not: Help me fixing this code so I can move on.

Thanks, in advance

8
  • 1
    @lorenzo.marcon twice
    – Onimusha
    Commented Jul 17, 2013 at 23:18
  • 3
    Also are those penis urls just an unfortunate coincidence?
    – Paul
    Commented Jul 17, 2013 at 23:18
  • 2
    no I just always use penis for testing stuff, but whatever. How do I fix that, @Paulpro
    – Mazey
    Commented Jul 17, 2013 at 23:21
  • 1
    Oh, okay. I thought maybe it was an abbreviation gone wrong or something haha.
    – Paul
    Commented Jul 17, 2013 at 23:22
  • 3
    Personally I use foobar, or some equivalent, but each to their own!
    – Nick R
    Commented Jul 17, 2013 at 23:42

3 Answers 3

3

There are many things wrong with your code and the main problem is you are mixing PHP and JS code. For example:

$url = "https://api.kag2d.com/player/"+nick+"/status";

nick is a Javascript variable and you can't just mix it in PHP code.

The same problem for:

status = $result.split("/n")

where $result is PHP variable.

4
  • How do I make a bridge, or whatever? There must be a way to do it some how
    – Mazey
    Commented Jul 17, 2013 at 23:24
  • PHP = Server side language. Javascript = Client side. They don't mix. They don't "bridge". They don't know or care about eachother. @Mazey, you need to read a book or three.
    – Stephen
    Commented Jul 17, 2013 at 23:27
  • Yes you can do ajax and that's the only solution for your problem I think. Basically after you get that nick, do a GET request to that API page (use jsonp or code a bridge with PHP and curl) and then process the result in the response. Commented Jul 17, 2013 at 23:29
  • I dislike books, anyway. I´ll go for using ajax, I guess. Thanks
    – Mazey
    Commented Jul 17, 2013 at 23:30
0

"Bridge" from PHP to JS:

<script type="application/javascript" language="javascript">
var someVariableThatYouWantToGetFromPHP="<?=$PHPvariable?>";
</script>

When PHP has executed, there will be such an output HTML:

<script type="text/javascript" language="javascript">
var someVariableThatYouWantToGetFromPHP="wheeha! JS has got the value of PHP variable";
//and here later you are now able to use it
</script>

"Bridge" from JS to PHP is a little bit more complex. You can't just get some JS variable and put it in some PHP script at some time. You have to make an HTTP request to the server with PHP.
That is exactly what forms do. If you have a form in your HTML code:

<form name="form1" action="phpscript.php" method="POST">
<input name="passingJSvarToPHP" type="hidden" value="" />
</form>

, then you may later submit the form from your JS code and this way pass any variable you want to your PHP script. JS code:

document.form1.passingJSvarToPHP.value=yourJSvariableThatYouWantToPutInYourPHPscript;
document.form1.submit();

And there is your PHP code on the server side, that will catch the variable:

<?php
$yeahGotTheJSvariable=$_POST['passingJSvarToPHP'];
//and here you may use your variable passed from JS code
?>


You have to understand, that when a form submit()s, the phpscript.php is then loaded instead of the page with the form.


You have to understand the synchronous request-response model of HTTP before writing such web applications, and before this, wait a bit with javascript. That will be level 1.


P. S. Level 2: AJAX.
P. P. S. Level 3: WebSockets.

0

I see two things wrong with the file:
1. For you to include php in a file the file extension must have a .php extension
2. Variables are not shared for php and javascript. What you could do is use PHP's $_GET[] array. For example:

<?php
$nick = $_GET['nick']
$url = "https://api.kag2d.com/player/".$nick."/status";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
$formattedresult = explode("/n",$result)
 if(strrpos($formattedresult,"true,") != -1)
 {
  echo "Gold player"
 }
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.