How to send two $.ajax requests at the same time to the php file using a function as a parameter in AJAX date just by making a request?

Asked

Viewed 15 times

-1

I am having trouble sending the data via AJAX at the same click event. PHP does not identify the title, only the photos that were sent. In SQL, the title of the product is not registered, only the photos are registered. How can I send both data only making a request?

JS cellular file.php:

    function get_image_data() {
    var form = new FormData()
        for (let index = 0; index < fotos.length; index++) {
            form.append("file[" + index + "]", fotos[index]['file']);
        }
        return form;
    }

    $("#btn_celulares").click(function (e) {
    var titulo = $("#inputTitulo").val();
    e.preventDefault();
    $.ajax({
        url: "c_celular.php",
        type: "POST",
        processData: false,
        contentType: false,
        cache: false,
        data: get_image_data(),
        success: function (d) {
            console.log(d)
        }
    });
    $.post("c_celular.php",{
        titulo : titulo,
    },function(dados){

    });
})

PHP file c_celulares.php:

$fileName=$_FILES['file']['name'];
    $fileTmpName=$_FILES['file']['tmp_name'];

    $titulo = $_POST['titulo'];

    $conexao = new mysqli("localhost","root","","loja");
    $sql = "insert into produto (nome) values('$titulo')";
    $resultado = $conexao->query($sql);

    $ID_produto = $conexao->insert_id;

    foreach ($fileName as $key => $val) 
    {
        $nome_arquivo = $fileName[$key];
        $tmp_name     = $fileTmpName[$key];

        $caminho = "./fotos/".$nome_arquivo;

        move_uploaded_file($tmp_name, $caminho);
        
        $sql2="insert into foto_produto
               (caminho, id_produto)
               values('$caminho','$ID_produto')";

        $resultado = $conexao->query($sql2);
    
    }

Console response: Undefined index: title

  • The statement var titulo = $("#inputTitulo").val(); is done in the scope of an anonymous function passed as parameter in $("#btn_celulares").click(.... The attempt to use the variable titulo is being attempted as a method parameter $.post("c_celular.php",{... in a scope above the scope of the declaration

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.