How to use PHP command inside javascript

Asked

Viewed 69 times

0

I would like to know how to use PHP(include) commands within javascript, as in the example below:

    jQuery(function($)
    {
        var $formteste = $('#fazer_upload_form')
        var $formcadastro = $('#form_cadastro');
        var $formlogin = $('#form_login');

        $formteste.submit(function()
        {
            <?
                include 'testar.php';
            ?>
            $("#painel_fazer_upload").dialog('close');
            return false;
        });
    });

When I use this way the modal doesn’t even open.

Testing.php would have that:

    include("connection.php");

$arquivo_up = $_FILES['arquivo']['name'];
$arquivo_descricao = $_POST['arquivo_descricao'];

if(file_exists("Uploads/$arquivo_up"))
{
    $a = 1;
    while(file_exists("Uploads/[$a]$arquivo_up"))
    {
        $a++;   
    }

    $arquivo_up = "[".$a."]$arquivo_up";    
}

$extensao = pathinfo($arquivo_up, PATHINFO_EXTENSION);

if($extensao == "torrent")
{
    if(move_uploaded_file($_FILES['arquivo']['tmp_name'], "Uploads/".$arquivo_up))
    {
        $arquivo_up = pathinfo($arquivo_up, PATHINFO_FILENAME);

        $sqlupload = mysqli_query($coneccao, "INSERT INTO arquivos_downloads(nome, descricao) VALUES ('$arquivo_up', '$arquivo_descricao')");

        header("location: index.php");
    }
    else
    {
        echo "  <meta http-equiv='refresh' content='0; url=index.php'>
                <script type='text/javascript'>alert('Erro no upload do arquivo!')</script>";       
    }   
}
else
{
    echo "  <meta http-equiv='refresh' content='0; url=index.php'>
            <script type='text/javascript'>alert('Formato Invalido!')</script>";
}

Note: I would like to know briefly how to do just that and not have to study something about it, thank you.

  • 1

    What is the goal, could you explain? that there needs ajax.

  • 1

    What’s inside of yours teste.php? You’ve seen how your HTML after the page renders? What exactly do you want to do by placing php within the javascript?

  • In a login system I would send to the test.php page, check the database, etc and then go back to index.php, instead of doing all this I would just like to call the test page,php, run everything right there and close the modal.

  • 1

    @Lucascarezia in this case you need to use AJAX. You know what that is?

  • I know what it is, but I only know his basics;C

  • 1

    @Lucascarezia reading suggestion: http://answall.com/q/43736/129, http://answall.com/q/6626/129

  • 1

    Thanks, I’ll give a study, but there is some "part" specific AJAX that I must study to do what I want ??

  • Really what you seek is ajax

  • But there’s some specific part of him I must study to perform what I want ??

  • Put in the question the contents of this php file to understand what you want to do.

  • Yes, what should I do ??

  • Lucas, you came to see the question link at the top of the page the solution of your question?

  • Yeah, I ran some tests and I couldn’t do it the same way, so I voted to reopen.

Show 8 more comments
No answers

Browser other questions tagged

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