1

I have a simple setup to allow a user to change his content on a page.

It calls up a query and populates the area with the results. My goal is to have the user enter in some new information, insert that info into the table, and requery the results.

Here is my Javascript function that is called:

function addLink(){
 var ajaxRequest;

 if(window.XMLHttpRequest){
  ajaxRequest = new XMLHttpRequest();
 } 
 else{
  ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
 }

 ajaxRequest.onreadystatechange = function(){
  if(ajaxRequest.readyState == 4 && ajaxRequest.status==200){
   var ajaxDisplay = document.getElementById('links');
   ajaxDisplay.innerHTML = "<?php displayTitle('Links'); ?><?php displayContent('Links', $isLoggedIn); ?>"
  }
  var imgURL = document.getElementById('links_img').value;
  var linkURL = document.getElementById('links_link').value;
  var queryString = "?imgURL=" + imgURL + "&linkURL=" + linkURL;
  ajaxRequest.open("GET", "addLink.php" + queryString, true);
  ajaxRequest.send();  
 }

}

Those PHP functions merely spit out the HTML, the displayContent() specifically for the actual table data.

Here is my HTML for adding some info to the database:

<center><br /><br />
Image URL: <input type='text' id='links_img' />
Link URL: <input type='text' id='links_link' />
<input type='button' onclick='addLink()' value='add' /></center>

Here is my PHP for adding the information:

<?php

 mysql_connect([sensitive information]) or die(mysql_error());
 mysql_select_db([sensitive information]) or die(mysql_error());

 $result = mysql_query("INSERT INTO linksMod (Image URL, Link URL) VALUES ("$_GET['imgURL']","$_GET['linkURL']") or die(mysql_error());

        mysql_close();
?>

Thee page does nothing when I click the 'Add' button.

Thanks for your help!

2 Answers 2

2

These lines:

  var imgURL = document.getElementById('links_img').value;
  var linkURL = document.getElementById('links_link').value;
  var queryString = "?imgURL=" + imgURL + "&linkURL=" + linkURL;
  ajaxRequest.open("GET", "addLink.php" + queryString, true);
  ajaxRequest.send();

need to be outside the "readystatechange" handler function. The way it's written now, they're inside the handler function and, since it isn't called, they'll never happen.

Sign up to request clarification or add additional context in comments.

1 Comment

I can't believe I missed that. Now I have to get the PHP to actually work haha. Thanks a lot!
-1
ajaxDisplay.innerHTML = "<?php displayTitle('Links'); ?><?php displayContent('Links', $isLoggedIn); ?>"

I don't think it's possible to embed php in javascript .

2 Comments

If the JavaScript code is in a <script> tag on the page, then that's fine. It's also possible that a stand-alone JavaScript file could be generated with php, but I doubt that's what's going on here.
The javascript is within a <script> tag. I was thinking this might not work as well, but I can play with it - that's another issue. Pointy's comment below solved my problem.

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.