3

Okay, so I have an AJAX function that sends data to the .php file, which processes it (simple as that):

$(document).on('click','#submit',function () {
    var title = $('#title_submit').val();
    var content = $('#content_submit').val();
    var image = $('#image_url').val();
    $.ajax({
        type: "POST",
        url: "../parts/add.php",
        data: {
            title: title,
            content: content,
            image: image
        },
        success: function (data) {
            redirect(); //a function that redirects to the main page
        }
    });
});

The content passes fine, and the function processes it. So, if there is no image ("image_url" field is empty) it simply ignores it and just adds the title and the content... But if there is an image it checks it's type (switch ($info) { case 'image/jpg':...) if none of them match:

default:
$new_name = "";
break;
}

and here's the hard part (for me):

if ($new_name == "") {
...//stop from executing and say "That ain't no image you be sendin'"; 
} else { //continue with adding an image to the folder and than creating a thumbnail of it...

I tried die() but if I entered something like "11111111111111111111111" into the Image URL input it entered "11111111111111111111111" into the database and outputed it instead of the image.

3
  • it should be if ($new_name == "") or if ($new_name === "") Commented May 6, 2013 at 11:51
  • use if(empty($new_name)) you assign variable instead of checking it Commented May 6, 2013 at 11:52
  • now that that's fixed, how do I stop the whole thing from executing and give out an error on the same page (using AJAX)? Commented May 6, 2013 at 11:59

1 Answer 1

1

You are checking wrong way, its like assigning empty string to $name use == operator to check against empty string

if ($new_name == "") {
Sign up to request clarification or add additional context in comments.

3 Comments

damn, such a stupid mistake :) but anyway, I corrected, but it still outputs text if I enter an unexisting url or simply text
Wait are you trying to upload image using this?
It won't work like this with ajax. You need to looks for some third party plugins that support image upload using ajax

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.