NodziGames
NodziGames

Reputation: 435

XMLhttprequest Not Calling PHP script

I'm pretty new to web development, and I'm trying to save a base64 encoded image on my sql database. The sql statement is working fine as I'm able to place user registration data into the database through PHP, but I'm running into trouble using XMLhttprequest to send the image to PHP so that I can use the $_GET method to receive the data. JQuery is strictly forbidden, so I'm stuck pretty bad. The image variable has the data in, and I did check to see that it's saved correctly. This is how I'm sending the XML request:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
    if (this.readyState == 4 && this.status == 200)
    {
        alert(this.responseText);
    }
}
xhttp.open("POST", "saveimage.php?img="+image, true);
xhttp.send();

The image variable contains the data. This code runs as well, since I placed alerts in between to make sure it does. Onto the saveimage.php file then:

<?php

session_start();

$email = $_SESSION['email'];
$image = $_GET['img'];
$conn = new PDO("mysql:host=localhost; dbname=camagru", "root", "admin");
$conn->setAttribute(PDO::ERRMODE_EXCEPTION);
$sql = $conn->prepare("INSERT INTO `images` (`image`, `email`) VALUES (:image, :email");
$sql->bindParam(':image', $image);
$sql->bindParam(':email', $email);
$sql->execute();

?>

Sadly from here, nothing happens. My database remains empty, no matter what. Am I missing something? It doesn't look like the PHP script is running at all.

Upvotes: 0

Views: 1606

Answers (2)

hairmot
hairmot

Reputation: 2975

I imagine the base64 encoded image string is too long for a get request - limited to something like 2083 characters on IE for example.

Have you tried POST?

You could check the web server logs to see if if does receive the whole string.

Heres Some Updated Code:

xhttp.setRequestHeader("Content-type" , "application/x-www-form-urlencoded");
xhttp.send("img=" + image);

See this answer to see how to POST the data

Upvotes: 1

Sal Orozco
Sal Orozco

Reputation: 45

You are sending the Request via Post but yet you are checking the $_GET request.

set the params like this

var params =  "img"+image;

and your call like this

xhttp.open("POST", "saveimage.php, true);
xhttp.send(param);

and on your server response script

<?php 
session_start();

if($_SERVER['REQUEST_METHOD'] == "POST" && (isset($_FILES['img']['name']))
{

$email = $_SESSION['email'];
$image = $_POST['img'];
$conn = new PDO("mysql:host=localhost; dbname=camagru", "root", "admin");
$conn->setAttribute(PDO::ERRMODE_EXCEPTION);
$sql = $conn->prepare("INSERT INTO `images` (`image`, `email`) VALUES (:image, :email");
$sql->bindParam(':image', $image);
$sql->bindParam(':email', $email);
$sql->execute();


}

?>

Are you trying to upload a file or just passing the name of a file threw ajax? If you are uploading a file remember you have to check if file exisist on the server before you record it to the database. If you are not uploading you don't need that (isset).

Upvotes: 1

Related Questions