Hey Leute,
Ich habe ein Formular was mit Ajax an eine PHP Datei gesendet wird und dieses eine json Datei abspeichert.
Jetzt möchte ich noch ein File Upload integrieren und da stocke ich.
Jetzt habe ich gelesen das per Ajax der File Upload nicht machbar sein soll bzw. es sich nicht vermischen lassen würde.
Wie würdet Ihr vorgehen? - Gibt es die Möglichkeit das hochgeladene Bild als base64 zu codieren und zu senden oder soll ich eine weitere php per ajax aufrufen?
index.php:
upload.php:
Ich habe ein Formular was mit Ajax an eine PHP Datei gesendet wird und dieses eine json Datei abspeichert.
Jetzt möchte ich noch ein File Upload integrieren und da stocke ich.
Jetzt habe ich gelesen das per Ajax der File Upload nicht machbar sein soll bzw. es sich nicht vermischen lassen würde.
Wie würdet Ihr vorgehen? - Gibt es die Möglichkeit das hochgeladene Bild als base64 zu codieren und zu senden oder soll ich eine weitere php per ajax aufrufen?
index.php:
HTML-Code:
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> <div class="row"> <div class="col-md-6"> <form class="form-horizontal"> <fieldset> <!-- Form Name --> <legend>Kunde</legend> <!-- Select Basic --> <div class="form-group"> <label class="col-md-4 control-label" for="AnspracheFeld">Ansprache</label> <div class="col-md-4"> <select id="AnspracheFeld" name="AnspracheFeld" class="form-control"> <option value="1">Herr</option> <option value="2">Frau</option> </select> </div> </div> <!-- Text input--> <div class="form-group"> <label class="col-md-4 control-label" for="KundeFeld">Name</label> <div class="col-md-4"> <div class="input-group"> <input id="KundeFeld" name="KundeFeld" type="text" placeholder="Meier" value="Meier" class="form-control input-md" > <div class="input-group-addon"><i class="glyphicon glyphicon-user"></i></div> </div> </div> </div> <!-- Text input--> <div class="form-group"> <label class="col-md-4 control-label" for="EmailFeld">E-Mail</label> <div class="col-md-4"> <div class="input-group"> <input id="EmailFeld" name="EmailFeld" type="text" placeholder="test@test.de" value="test@test.de" class="form-control input-md" > <div class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></div> </div> </div> </div> <!-- File Button --> <div class="form-group"> <label class="col-md-4 control-label" for="filebutton">Datei anhängen</label> <div class="col-md-4"> <input id="filebutton" name="filebutton" class="input-file" type="file"> </div> </div> <!-- Speichern/Absenden --> <div class="form-group"> <label class="col-md-4 control-label" for="SpeichernButton">Angebot</label> <div class="col-md-8"> <button id="SpeichernButton" name="SpeichernButton" class="btn btn-success"><span class="glyphicon glyphicon-floppy-disk"></span> Speichern</button> </div> </div> </fieldset> </form> </div> </div> </body> </html> <script> $("#SpeichernButton").click(function(e) { e.preventDefault(); var $Absender = "User", $AbsenderEmail = "test@test.de", $AnspracheFeld = ($("#AnspracheFeld option:selected").text()), $KundeFeld = ($("#KundeFeld").val()), $KundeEmail = ($("#EmailFeld").val()); //-----------------------------------Informationen werden in einer JSON Datei gespeichert----------------------------- $.ajax({ type: "POST", url: "upload.php", dataType: 'json', data: { "Angebot": { "Absender": $Absender, "AbsenderEmail": $AbsenderEmail, "Kunde": $KundeFeld, "KundeAnsprache": $AnspracheFeld } }, }) location.reload(); }); </script>
upload.php:
PHP-Code:
<?php
$neu = ($_POST);
$file = file_get_contents('angebot.json');
$data = json_decode($file);
unset($file);
$data[] = $neu;
file_put_contents('angebot.json',json_encode($data));
unset($data);
?>
Kommentar