-3

Please help me to solve this problem.

**

public function uploadPhoto($file)
    {
        if(!empty($file)){
            $fileTempPath=$file['tmp_name'];
            $fileName=$file['name'];
            $fileType=$file['type'];
            $fileNameCmps=explode('.',$fileName);
            $fileExtension=strtolower(end($fileNameCmps));
            $newFileName=md5(time().$fileName) . '.' .
            $fileExtension;
            $allowedExtn=["png","jpg","jpeg"];
            if(in_array($fileExtension,$allowedExtn))
            {
                $uploadFileDir=getcwd().'/uploads';
                $destFilePath=$uploadFileDir . $newFileName;
                if(move_uploaded_file($fileTempPath,$destFilePath)){
                    return $newFileName;
                }
            }
    
        }
    }**

this is how I send data to parameters

** if($conf == $_SESSION['randomNum']) {

                    //create object of user class 
                    require_once 'C:\xampp\htdocs\MEDICALCENTERPROJECT\app\libraries\User.php';
                    $obj = new User();
                    $obj->connect();
                     
                    $img = "";
                    if($pic == "")
                    {
                        $img = "null";
                    }
                    else{
                        $img = $obj->uploadPhoto($pic);

                    }**

I tried many times to understand the error but I could not do. Here the error is shown in line 111 its mean $fileTempPath=$file['tmp_name']; in this line. Please help me to solve this.

this is how I sent data using ajax

**$(document).ready(function() {

$("#success").hide();


$("#con").click(function() {

    let confirm = $("#ccode").val();
    let isTrue = "false";
    let username = $("#pusername").val();
    let email = $("#pemail").val();
    let mobile = $("#pmobile").val();
    let addrs = $("#paddress").val();
    let id = $("#pidno").val();
    let pswd = $("#ppassword").val();
    let c_pswd = $("#pcpassword").val();
    let pic = $("#puserphoto").val();


    let data = new FormData("#addformSign");
    $.ajax({
        url: "./Patials/sendEmailVerifyCode.php", //"./Patials/ajax.php",
        type: "POST",
        data: {
             conf: confirm,
            isTrue: isTrue,
            username: username,
            email: email,
            mobile: mobile,
            addrs: addrs,
            id: id,
            pswd: pswd,
             pic: pic,
         },

        success: function(response) {

            alert(response);

            if (response == confirm) {

                $("#emailCon").modal("show");
                $("#confirm").hide();
                $("#success").show();

            } else {

                $("#ccode").css({
                    "borderColor": "red",
                    "backgroundColor": "rgba(253, 127, 127, 0.5)"
                });
                $("#confirm").show();
                $("#errMsg").text("Invalid Code!");

            }



        },

        error: function(request, error) {
            console.log(arguments);
            console.log("Erro" + error);
            $("#confirm").show();
        },
    });


});

**

let confirm = $("#ccode").val(); this input field is not in the same form.

  • 2
    There are several things that looks weird with the code (like: $img = "null". Why do you make $img a string with the value "null" instead of making it null?). The problem is that the value you have in $pic is a string. Not the actual file content. Where does $pic come from? – Christoffer May 16 '22 at 21:49
  • Hi! Thank you for comment for this problem. I put $img = "null" because of when I get the data from data base I can use other image to profile who did not upload their photo that is why I do so. And $pic is come from ajax – Sasindu Bandara May 16 '22 at 22:08
  • $.ajax({ url: "./Patials/sendEmailVerifyCode.php", //"./Patials/ajax.php", type: "POST", data: { conf: confirm, isTrue: isTrue, username: username, email: email, mobile: mobile, addrs: addrs, id: id, pswd: pswd, pic: pic, }, this is the ajax code "pic" is come from here. Please help me !!!! – Sasindu Bandara May 16 '22 at 22:08

1 Answers1

1

I think you are sending string as $pic value. It has to be some sort of array. Make sure you are sending the file correctly with Ajax. Check this Debug by examining the real-time value of the variable using PHP's var_dump function.

The way to solve errors without internet (without asking anyone) is to search for the real-time value of the variable. You can do this in php with the var_dump/var_export/echo/print functions.

Make sure you provide enough information to the other party when asking questions. I don't know how you send data with Ajax. It's impossible for me to get the same error as you. You should ask the same in a way that I can provide or imagine in my own setting.

The error you get is TypeError, that is, you can fix this error by checking the data you send and the data you receive.


UPDATE:

What you need to be careful about is that all the <input> elements you want to send are in the <form>.

you can try like that.

$("#con").click(function() {
  let confirm = $("#ccode").val();

  let yourFormData = new FormData("#addformSign"); // all sended element must be inside this form

  $.ajax({
    url: "./Patials/sendEmailVerifyCode.php", //"./Patials/ajax.php",
    type: "POST",
    data: yourFormData,
    success: function(response) {
      alert(response);
      if (response == confirm) {
        $("#emailCon").modal("show");
        $("#confirm").hide();
        $("#success").show();
      } else {
        $("#ccode").css({
            "borderColor": "red",
            "backgroundColor": "rgba(253, 127, 127, 0.5)"
        });
        $("#confirm").show();
        $("#errMsg").text("Invalid Code!");
      }
    },
    error: function(request, error) {
      console.log(arguments);
      console.log("Erro" + error);
      $("#confirm").show();
    },
  });
});

If the above code does not solve the problem, make sure that you send the values ​​of the input fields you sent correctly with ajax.

For example, you need to use the following line as

let pic = $('#puserphoto').prop('files')[0];

instead of

let pic = $("#puserphoto").val();

also $_FILES is used to receive the file sent with the POST method.

$pic = $_FILES['pic']

Good Luck

Y.C.
  • 191
  • 5
  • Thank you. I have updated the question please check it and can you help me to solve it correctly – Sasindu Bandara May 17 '22 at 08:26
  • when I put $pic = $_FILES['pic'] like this with $pic = $_FILES['pic'] it always says "Undefined array key "pic" " but if I change the type of input as "text" instead "file" it does not say like that. Can you tell me what's wrong with me. – Sasindu Bandara May 17 '22 at 17:38