user1511579
user1511579

Reputation: 1537

Receive data on php file send by jquery function

I have the following function that is called when I click on a button to submit a form:

    function dadosFormularios(preenchimentoForm){
    //var path = document.location.pathname;
    //alert(path);
    alert(preenchimentoForm);
    //window.location.href = 'wp-content/themes/template/index.php'; 

    var qstringA = '';
    //dados dos campos
    var nome=document.getElementById("nome").value;
    qstringA = 'nome='+ nome;
    //alert(qstringA);

    if(preenchimentoForm==false){
        alert('Please correct the errors in the Form'); 
    }
    else{
        if(preenchimentoForm==true){
            window.location.href = 'index.php?'+qstringA;
        return false;
        }
    }
}

Since I'm using this way of processing the data, how can I alert my page index.php that the data sent by the function arrived on the index? I can't use a if (isset($_POST['button']..) since I send the information by the function and not through the button of the form, right?

Upvotes: 0

Views: 241

Answers (3)

DavChana
DavChana

Reputation: 1976

window.location.href = 'index.php?'+qstringA;
This line is just redirecting to index.php with a query string ?nome=nome_value.
For example. index.php?nome=nome_value

So, in your index.php You can simply get everything posted with $_GET.

Check it by doing a print_r($_GET); there.

In index.php, you can simply check

if(isset($_GET["nome"])){
    //Nome is set
    //Do something here
}

P.S. Although, without knowing the other circumstances or reasons behind usage of this function, it can be said that this function is just doing what a simple <form action=index.php> would have done.

P.P.S. Although you have mentioned jQuery in title and also tagged it, I am not sure this function is using any of the jQuery code. I think it is just a simple Javascript function.

Upvotes: 2

Cl&#233;ment Andraud
Cl&#233;ment Andraud

Reputation: 9269

Use ajax() like :

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

http://api.jquery.com/jQuery.ajax/

Upvotes: 1

SomeKittens
SomeKittens

Reputation: 39522

If you're using jQuery, check out .ajax(). Just remember, it's asynchronous, so the results may not be what you think they are. You don't need to reload the whole page (which is what your window.location.href = 'index.php?'+qstringA; would do) just to submit information.

If you want to alert or something when the ajax call completes, you can define a function to call on a successful ajax call.

Upvotes: 1

Related Questions