0

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?

2
  • Are you trying to get the php script to process data and send a response back? Also, the PHP script will differentiate data sent in the query string $_GET, so you could use isset for that array instead, but if you're looking to get data back from the page, you need to look at using the jquery function .ajax() Commented Jul 17, 2012 at 12:48
  • i just want to receive the data on the .php file to process the data there Commented Jul 17, 2012 at 12:54

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it worked; I had the idea that the isset was the trigger to check when the data has arrived. So i just need to use $_GET normally and i can process the data there right?
Yeah, use isset($_GET['nome']) with if/else condition to check if requested data has arrived or not and then proceed further..
Ok, i guess i will try this way for now, since i'm more familiar with php than ajax. Thanks a lot everyone!
1

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.

1 Comment

It's not so much a "bad result" as unexpected results when the code tries to use data that hasn't returned back from the call. As long as you put the code that depends on the data in a function on success, then that shouldn't happen.
1

Use ajax() like :

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

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

1 Comment

@user1511579 No, not in the index.php, but in place of your Javascript code mentioned in your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.