user1294097
user1294097

Reputation: 153

Addinto MySQL with AJAX and PHP

I have a working code which allows users to insert url's into a database, and display the contents of the database.

The problem im running into when i use ajax to addinto database is that if an url has the character "&" it cuts the url right before that character for example:

1. http://www.arisroyo.com/wp-content/uploads/2012/07/php.png
2. http://www.arisroyo.com/wp-content/uploads/2012/07/test&123.png

When adding url 1 to the database i have no problems, but when adding url 2 to the database i get this added to the database:

http://www.arisroyo.com/wp-content/uploads/2012/07/test

It ignores anything after the "&" sign. The problem is that all of my urls will be using the "&" sign.

This is the code im using on AJAX to get the url from the user from the website:

var nocache = 0;
function insert() {
// Optional: Show a waiting message in the layer with ID login_response
document.getElementById('insert_response').innerHTML = "Just a second..."
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var site_url= encodeURI(document.getElementById('site_url').value);

// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', 'insert.php?site_url='+site_url+'&nocache = '+nocache);
http.onreadystatechange = insertReply;
http.send(null);
}
function insertReply() {
if(http.readyState == 4){
var response = http.responseText;
// else if login is ok show a message: "Site added+ site URL".
document.getElementById('insert_response').innerHTML = 'Site added:'+response;
}
}

I've been using the addinto mySQL without ajax, and never had a problem with the url and the "&" sign. I'd like to know what might be causing this problem.

Upvotes: 0

Views: 95

Answers (2)

user1294097
user1294097

Reputation: 153

I solved it by simply adding the word component to econdeURI like so:

var site_url= encodeURIComponent(document.getElementById('site_url').value);

I only post here when I'm completely stomped, so I'm sorry to have wasted your time! but thanks for taking the time to check out my question

Upvotes: 0

Ryan_K
Ryan_K

Reputation: 50

document.getElementById('insert_response').innerHTML = "Just a second..."

Missing a line termination there?

Might also try instantiating site_url like this: url site_url = @"thesite.com";

Upvotes: 1

Related Questions