0

Hello and thanks for the help in advance. I'm new to JavaScript and ajax. I'm trying to get a php code to display in the same page based on a variable that has been predefined elsewhere in the page. I found the code below using an object to display the php in the same page. my problem is when i try to add the variable it doesn't get passed with the php request. Any help or ideas would be appreciated.

   <form id="locform" name="locform" method="get">
    <input type="text" name="box1" id="box1">
</form>

<script type="text/javascript">    
   var y = localStorage.getItem("location");
   document.locform.box1.value = y;
   document.getElementById("example").innerHTML = '<object type="text/html" data="loc.php?loc=" + y></object>';    
</script>
<div id="example"></div>
4
  • you haven't closed your string properly Commented Dec 28, 2015 at 8:48
  • Actually your script couldn't identify your div, move the script after div, or load script using window.onload. And you are passing y as string not as a variable! Commented Dec 28, 2015 at 8:50
  • Using an <object /> to do an AJAX call is very creative but not your best bet. I would recommend you to look into AJAX calls. W3Schools has a great tutorial. Commented Dec 28, 2015 at 8:55
  • You wrongly concatenation of 'y'. Please correct it with data="loc.php?loc='+y+'" Commented Dec 28, 2015 at 10:11

1 Answer 1

2

You're putting y as a string not as variable, Try this:

document.getElementById("example").innerHTML = '<object type="text/html" data="loc.php?loc=' + y + '"></object>';
0

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.