0

I have this SweetAlert (JavaScript) Code and I want to pass that Variable to my database. The Variable is result.value[0] and I want to send that to my Database. But it does not work and I don't know why....

This is my entire JavaScript Code.

<script>


    $(document).ready(function () {
        $('#new-btn').click(function () {

            swal.mixin({
                input: 'text',
                confirmButtonText: 'Next &rarr;',
                showCancelButton: true,
                progressSteps: ['1', '2', '3']
            }).queue([
                {
                    input: 'file',
                    inputAttributes: {
                        name:"image",
                        class:"image",

                    },
                    title: 'Profilbild hochladen',
                    text: 'Empfohlen wird 1X1'
                },
                {
                    input: 'file',
                    title: 'Hintergrundbild hochladen',
                    text: 'Empfohlen wird 16X9'
                },
                {

                    title: 'Über mich',
                    text: ''
                },

            ]).then((result) => {
                if (result.value) {

                    var kuerzeltest = "mk304";


                    $.ajax({ type: "POST",  url: "../../register/profil_update.php",
                        data: {"post":result.value[2], "kuerzel": kuerzeltest  },



                    });  $.ajax({ type: "POST",  url: "../../register/profil_update.php",
                        data: {"bild":result.value[0],"bild2":result.value[1], "kuerzel": kuerzeltest },
                        contentType: false,
                        processData: false,

                    });
                    swal(
                        "Super!",
                        "Dein Profil wurde erfolgreich aktualisiert ",
                        "success"

                    )
                }
            })
        });
    })


</script>

and this is my backend code

<?php
include_once '../../userdata.php';
//Posts in Datenbank schreiben


$kuerzel = $_POST["kuerzel"];
$bild = $_FILES['bild'];
$bild2 = $_FILES['bild2'];
$post = $_POST["post"];

$pdo = new PDO ($dsn, $dbuser, $dbpass, array('charset'=>'utf8'));
$sql = "INSERT INTO user_bilder (kuerzel, bild, bild2, post) VALUES (?, ?, ?, ?)";

$statement = $pdo->prepare($sql);
$statement->execute(array("$kuerzel", "$bild", "$bild2", "$post"));

$row = $statement->fetchObject();

header("Location: ../webpage/home.php");

?>

would be greatfull for any tips!

  • Should `$bild = $_FILES['bild'];` perhaps be `$bild = $_POST['bild'];`? – E. Sundin Nov 10 '18 at 20:44
  • 2
    *"it doesn't work"* is a virtually meaningless problem statement, particularly when ajax is involved and you show us both server side and client side code. Note that doing a server side redirect in an ajax request will not cause the page to change to that new location – charlietfl Nov 10 '18 at 20:44
  • Do some research in how to set up ajax error handling and how to inspect the actual requests in browser dev tools. If you are going to use ajax you have to learn how to isolate if problem is server related or client code related – charlietfl Nov 10 '18 at 20:46
  • You need to breakdown your problem in three steps: AJAX call, PHP handling, adding image to database. To start with, your AJAX call is not correct. You are now using the corredct variable from swal, but you can see in the browser console that your HTTP POST doesn't contain the correct headers. You can have a look (for example) to https://stackoverflow.com/questions/2320069/jquery-ajax-file-upload. As an advice, IMHO I don't think that raising new questions for the same issue on stackoverflow is the right strategy to get support ;-) – G. Verni Nov 11 '18 at 08:39

0 Answers0