在我的网站上,通过form
我在数据库中发送/注册相同的信息SELECT
/查询并返回!返回保存在数据库中的最后一个表,即用户刚刚在表单上输入的表(以及来自数据库的更多信息)。
如何在中显示来自数据库的这些值modal bootstrap
页面不刷新是必要的。为此,我插入了AJAX
具体如下:
$(document).ready(function(){
$(\'#enviar\').click(function(){
$.ajax({
//CAAL AJAX IN WORDPRESS
url: \'wp-admin/admin-ajax.php\',
type: \'POST\',
//INSERT, QUERY AND DISPLAYS TO USER
data: \'action=prancha\',
error: function(){
alert(\'ERRO!!!\');
},
//IF OK, DISPLAYS TO USER IN DIV "RESULT"
success: function(data){
$(\'#result\').html(data);
}
});
});
});
我的职能。PHP:
function prancha(){
header(\'Content-Type: text/html; charset=utf-8\');
include "../../../wp-config.php";
/* DECLARING THE VARIABLES */
$nome = "";
$email = "";
$estilo = "";
$experiencia = "";
$altura = "";
$peso = "";
// VALIDATION
if(!empty($_POST)){
$nome = $_POST[\'nome\'];
$email = $_POST[\'email\'];
$estilo = $_POST[\'estilo\'];
$experiencia = $_POST[\'experiencia\'];
$altura = $_POST[\'altura\'];
$peso = $_POST[\'peso\'];
cadastra_user($nome, $email, $estilo, $experiencia, $altura, $peso);
}
//INSERT IN DATABASE NAME, EMAIL, ESTILE, EXPERIENCE, HEIGHT AND WEIGHT
function cadastra_user($nome, $email, $estilo, $experiencia, $altura, $peso){
global $wpdb;
$table = \'user\';
$data = array(
\'nome\' => $nome,
\'email\' => $email,
\'estilo\' => $estilo,
\'exp\' => $experiencia,
\'altura\' => $altura,
\'peso\' => $peso,
);
$updated = $wpdb->insert( $table, $data );
if ( ! $updated ) {
$wpdb->print_error();
}
}
//CONECT WITH DATABASE TO DO THE SELECT
include "db.php";
function BuscaAlgo($conexao){
// QUERY + INNER JOIN IN DATABASE
$query = "SELECT USU.usuario,
USU.nome,
USU.exp,
USU.altura,
USU.peso,
PRAN.exp_ref,
PRAN.altura_ref,
PRAN.peso_ref,
PRAN.tipo_prancha,
PRAN.tamanho_prancha,
PRAN.meio_prancha,
PRAN.litragem_prancha
FROM DADOS_USUARIO AS USU
INNER JOIN PRANCHA AS PRAN
on USU.exp = PRAN.exp_ref
WHERE USU.altura = PRAN.altura_ref
AND USU.peso = PRAN.peso_ref
ORDER BY USU.usuario DESC LIMIT 1";
$resultado = mysqli_query($conexao,$query);
$retorno = array();
while($experiencia = mysqli_fetch_assoc($resultado)){
$retorno[] = $experiencia;
}
return $resultado;
}
//DISPLAYS THE QUERY TO USER
$resultado = array();
$resultado = BuscaAlgo($conexao);
foreach($resultado as $valor){
echo $valor["usuario"]; print(". . . .");
echo $valor["nome"]; print(". . . .");
echo $valor["exp"]; print(". . . .");
echo $valor["altura"]; print(". . . .");
echo $valor["peso"]; print(". . . .");
print("///////");
echo $valor["tipo_prancha"]; print(". . . .");
echo $valor["tamanho_prancha"]; print(". . . .");
echo $valor["meio_prancha"]; print(". . . .");
echo $valor["litragem_prancha"];
}
die(); //END THE EXECUTION
}
//ADD THE AJAX HOOKS IN WORDPRESS
add_action(\'wp_ajax_prancha\', \'prancha\');
add_action(\'wp_ajax_nopriv_prancha\', \'prancha\');
代码正在注释,基本上我是这样做的:
AJAX:
在“URL”字段中,调用本机Wordpress“admin ajax”。php`
在“数据”字段中,调用向用户进行注册、查询和显示的函数在“成功”字段中,打印“数据”的值功能:我在数据库中生成注册码,用
echo
.
这个AJAX
正在返回错误消息。这是错误的AJAX
它本身现场error: function(){ alert(\'ERRO!!!\');},
我如何解决这个问题?我做错了什么?
注1:当我插入my函数中的代码时, the registration code, the query and the
“echo”以直接方式显示,在我的footer.php
, 它起作用了。因此,我们可以理解,错误甚至不在插入、查询或显示的代码中<注2:我想在modal boostrap
. 起初,我只是在屏幕上显示,以检查一切是否正常。之后,我将研究如何将这些数据放入modal
, 虽然不是这篇文章的主要主题,但也欢迎提出如何做到这一点的建议