-7

I have this ajax code, pretty simple :

$.ajax({
         type: "POST",
         url: "save.php",
         data: {
         imgBase64: dataURL,
         counter : time
         }
       });

And in the other side, I have my php file :

<?php
define('UPLOAD_DIR', 'upload/');
$img = $_POST['imgBase64'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$counter = $_POST['counter'];
$formatedcounter = sprintf('%03d', $counter);
$data = base64_decode($img);
$filename = UPLOAD_DIR . 'GWD' .  $formatedcounter . '.png';
$success = file_put_contents($filename, $data);
print $success ? $file : 'Unable to save the file.';
?>

I would like to combine both. Is it possible to write the php code instead of "save.php" for data in ajax? I've already tried but I get errors in phpstorm.

Thanks a lot! Regards.

9
  • 3
    No it's not possible..thats not how ajax works... url:"you must provide an url here" Commented Jul 28, 2017 at 7:17
  • 1
    Can't see where $file is defined in the php file Commented Jul 28, 2017 at 7:18
  • @BilalJohn ok. Thanks a lot. So second question : is it possible to right the php code after the ajax and point the url to this piece of code, which is not in a separate file? Commented Jul 28, 2017 at 7:22
  • @PhilippeGREBAN yeah its possible. To send ajax request to the same page you can keep url parameter empty Commented Jul 28, 2017 at 7:32
  • this example might help.. stackoverflow.com/questions/7561569/… Commented Jul 28, 2017 at 7:33

1 Answer 1

0

Thanks a lot guys.

So it can only point to a file. It's not (for example) possible to write the php code after and to point to it.

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

3 Comments

you can write both on same page.. see above comments... may be read this too makitweb.com/how-to-handle-ajax-request-on-the-same-page-php
Thanks a lot, I'm gonna read this. Thanks for helping a beginner <3

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.