接收jquery函数发送的php文件上的数据


Receive data on php file send by jquery function

当我点击按钮提交表单时,会调用以下函数:

    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;
        }
    }
}

由于我使用这种方式处理数据,如何提醒页面index.php函数发送的数据已到达索引?我不能使用if (isset($_POST['button']..),因为我通过功能而不是通过表单的按钮发送信息,对吧?

window.location.href = 'index.php?'+qstringA;
这一行只是用查询字符串?nome=nome_value将重定向到index.php
例如index.php?nome=nome_value

所以,在你的index.php中,你可以简单地用$_GET发布所有内容。

在那里做print_r($_GET);检查。

index.php中,您可以简单地检查

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

p.S.尽管在不知道使用该函数背后的其他情况或原因的情况下,可以说该函数只是在做一个简单的<form action=index.php>应该做的事情。

p.p.S.尽管您在标题中提到了jQuery并对其进行了标记,但我不确定该函数是否使用了任何jQuery代码。我认为这只是一个简单的Javascript函数。

如果您使用的是jQuery,请检查.ajax()。记住,它是异步的,所以结果可能不是你想象的那样。您不需要为了提交信息而重新加载整个页面(window.location.href = 'index.php?'+qstringA;会这样做)。

如果您想在ajax调用完成时alert或其他什么,您可以定义一个函数来调用成功的ajax调用。

使用ajax()类:

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

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