javascript - ajax is working without event.preventDefault(); - but breaks if added event.preventDefault(); -


i've built ajax request insert new row mysql , updated database array. works without event.preventdefault(); (and redirects php file) - breaks when added event.preventdefault(); prevent redirect.

any ideas?

$("#upload-form").submit(function() {     //event.preventdefault();     window.alert(paintscatlg.length);      document.queryselector("#upload-form #id").value = paintscatlg.length;     document.queryselector("#upload-form #src").value = "paintings//" + document.queryselector("#upload-form #exhibition_en").value + "//" + filename;     window.alert(document.queryselector("#upload-form #id").value);     window.alert(document.queryselector("#upload-form #src").value);      var uploadform = new formdata();     uploadform.append("id", $('#id').val());     uploadform.append("src", $('#src').val());     uploadform.append("title_en", $('#title_en').val());     uploadform.append("title_he", $('#title_he').val());     uploadform.append("exhibition_en", $('#exhibition_en').val());     uploadform.append("exhibition_he", $('#exhibition_he').val());     uploadform.append("subjects_en", $('#subjects_en').val());     uploadform.append("subjects_he", $('#subjects_he').val());     uploadform.append("keywords_en", $('#keywords_en').val());     uploadform.append("keywords_he", $('#keywords_he').val());     uploadform.append("height", $('#height').val());     uploadform.append("width", $('#width').val());     uploadform.append("sold", $('#sold').val());     var settings = {         // "async": true,         // "crossdomain": true,         "url": "upload.php",         "method": "post",         "datatype": 'json',         "processdata": false,         "contenttype": false,         "mimetype": "multipart/form-data",         "data": uploadform     }      $.ajax(settings).success(function(data) {         //   alert("hello! alert box!!");         alert('good');         alert(data);         paintscatlg = data;     }); }); 
$servername = "localhost"; $username = "root"; $password = ""; $dbname = "chana_goldberg";  // create connection $conn = new mysqli($servername, $username, $password, $dbname); $conn->set_charset("utf8");  // check connection if ($conn->connect_error) {     die("connection failed: " . $conn->connect_error); }   // if(isset($_post['submit'])) { //   unset($_post['submit']); $id = $_post['id'];     $src = $_post['src']; $title_en = $_post['title_en']; $title_he = $_post['title_he']; $exhibition_en = $_post['exhibition_en']; $exhibition_he = $_post['exhibition_he']; $subjects_en = $_post['subjects_en']; $subjects_he = $_post['subjects_he']; $keywords_en = $_post['keywords_en']; $keywords_he = $_post['keywords_he']; $height = $_post['height']; $width = $_post['width']; $sold = $_post['sold']; $enc_exhibition = mb_convert_encoding($exhibition_en, "ascii"); $target_dir = "paintings/$enc_exhibition/"; // $src = "paintings/$enc_exhibition/"; if ( ! is_dir($target_dir)) {     mkdir($target_dir); }  $target_file = $target_dir . basename($_files["filetoupload"]["name"]); $uploadok = 1; $imagefiletype = pathinfo($target_file,pathinfo_extension); // check if image file actual image or fake image if(isset($_post["submit"])) {     $check = getimagesize($_files["filetoupload"]["tmp_name"]);     if($check !== false) {         // echo "file image - " . $check["mime"] . ".";         $uploadok = 1;     } else {         echo "file not image.";         $uploadok = 0;     } } // check if file exists if (file_exists($target_file)) {     echo "sorry, file exists.";     $uploadok = 0; } // check file size if ($_files["filetoupload"]["size"] > 500000) {     echo "sorry, file large.";     $uploadok = 0; } // allow file formats if($imagefiletype != "jpg" && $imagefiletype != "png" && $imagefiletype != "jpeg"    && $imagefiletype != "gif" ) {     echo "sorry, jpg, jpeg, png & gif files allowed.";     $uploadok = 0; } // check if $uploadok set 0 error if ($uploadok == 0) {     echo "sorry, file not uploaded.";     // if ok, try upload file } else {     if (move_uploaded_file($_files["filetoupload"]["tmp_name"], $target_file)) {         $exhibition_en = mysql_real_escape_string($exhibition_en);          $stmt = $conn->prepare("insert paintings_catalog (src, title_en, title_he, exhibition_en, exhibition_he, subjects_en, subjects_he, keywords_en, keywords_he, height, width, sold) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");         $stmt->bind_param("ssssssssiiss", $src, $title_en, $title_he, $exhibition_en, $exhibition_he, $subjects_en, $subjects_he, $keywords_en, $keywords_he, $height, $width, $sold);          $stmt->execute();          $query = "select * paintings_catalog order id";         $result = $conn->query($query);          // numeric array         $paintscatlg[] = array(1 =>'');         while ($row = $result->fetch_array(mysqli_num)){             $paintscatlg[] = $row;           };          echo json_encode($paintscatlg);     } } 

you need use event parameter provided submit handler function, not global event object. try this:

$("#upload-form").submit(function(e) { // note 'e' here     e.preventdefault();     // code... }); 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -