Sparksido
Sparksido

Reputation: 625

Can't pass a javascript variable to a php tag

I want to display a javascript variable in the tag in the HTML page but it doesn't appear when I click the button. Can anyone tell me what I'm doing wrong?

HTML

<button name = "presser">go </button>
<p id = "display"></p>
<script type ="text/javascript" src ="jquery.js"></script>
<script type ="text/javascript" src = "processor_file.js"></script>

Javascript

$(":button").click(function(){
    $.get("middleman.php", {theword: "happen"},function(data,status){
        $.getElementById("display").innerHTML = data;
    });
});

PHP code from middleman.php

<?php
    if(isset($_GET["theword"])){
        $token = $_GET["theword"];
        echo substr($token, 1, 3);
    }
?>

The word is successfully passed to the javascript page from the php page but when I try to display it through the <p> tag, nothing happens.

Upvotes: 0

Views: 64

Answers (2)

Barmar
Barmar

Reputation: 781141

$.getElementById should be document.getElementById. Or use the jQuery function

$("#display").html(data);

Upvotes: 1

taxicala
taxicala

Reputation: 21769

You are mixing vanilla js and jquery, you should do as follows:

$(":button").click(function(){
    $.get("middleman.php", {theword: "happen"},function(data,status){
        $("#display").html(data);
    });
});

Upvotes: 6

Related Questions