-1

My problem is I can't update my database but client side is work fine.I suspect my problem maybe the php file cannot get id from ajax. I was not clear about how to send the id and FormData object to another page.

I have tried to use append on FormData object to send id but it wont work.

Could anyone give me hint or explain about how to send the id and FormData object to php file?

jquery

   $(document).on('click','.update_btn',function(){
   url=/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.? 
    +=&%@!\-\/]))?/;
   var update_btn=$(this).attr('id');
    var edit_company_name=$('#edit_company_name').val();
    var edit_website=$('#edit_website').val();
    var extension=$('#edit_company_image').val().split('.').pop();
    if($('#edit_company_name').val()==''){
        alert('名字没填写!');
    }
    else if($('#edit_company_image').val()==''){
        alert('图片没放!')
    }else if($.inArray(extension,['jpg','png','gif','jpeg'])==-1){
        alert('图片格式错误!');
    }else if(url.test(edit_website)){
        alert('请写网站格式!');
    }
    else{
      swal.fire({
        title: "你是否确定更改公司的资料?",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是, 我确定要更改此资料!"

    }).then((result)=>{
      var formdata=new FormData(this);
      formdata.append('edit_company_name',edit_company_name);
      formdata.append('edit_website',edit_website);
      if(result.value){
        $.ajax({
          url:"edit-company.php",
          method:"POST",
          data: formdata,
          cache:false,
          contentType:false,
          processData:false,
          success:function(){
           swal.fire({
            text:'恭喜你,你成功更改公司的资料!',
                type:'success',
                showCancelButton: false,
                showConfirmButton: false
           })
           setTimeout(function(){location.href="company.php"},10000);  
          }

        });
        }//end of result value
     });


    }
})

edit-company.php


include('dbconnect.php');
if(isset($_POST['update_btn'])){
        $edit_company_name=trim(mysqli_real_escape_string($conn,$_POST['edit_company_name']));
        $edit_website=trim(mysqli_real_escape_string($conn,$_POST['edit_website']));
        $edit_file=$_FILES['edit_company_image'];
        $edit_file_tmp=$_FILES['edit_company_image']['tmp_name'];
        $target_path="image/company/";
        $upload_path=$target_path.basename($edit_file);
        $sql="SELECT * FROM company WHERE company_name='".$_POST['update_btn']."'";
        $result=mysqli_query($conn,$sql) OR die("Error ".mysqli_error($conn));
        while($row=mysqli_fetch_array($result)){
           $file=$row['company_image'];
        }
        unlink($file);
        $query="UPDATE company SET company_name='".$edit_company_name."',company_image='".$upload_path."',website='".$edit_website."' WHERE company_name='".$_POST['update_btn']."'";
        mysqli_query($conn,$query);
        move_uploaded_file($edit_file_tmp,$upload_path);



    }
kbk
  • 11
  • 4

1 Answers1

0

use ajax and check your response and then you can use sweetalert accoding to your response

$('#buttonId').click(function() {
    var val1 = $('#fieldId').val();
    $.ajax({
        type: 'POST',
        url: 'your_php_file.php',
        data: { fieldId: val1},
        success: function(response) {
            $('#result').html(response);
        }
    });
});
PHP Geek
  • 3,949
  • 1
  • 16
  • 32