-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.php
90 lines (83 loc) · 2.47 KB
/
ajax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?
header('Content-Type: text/html; charset=utf-8');
include('variables.php');
function bindParameters(&$statement, &$params) {
$args = array();
$args[] = implode('', array_values($params));
foreach ($params as $paramName => $paramType) {
$args[] = &$params[$paramName];
$params[$paramName] = null;
}
call_user_func_array(array(&$statement, 'bind_param'), $args);
}
mysqli_report(MYSQLI_REPORT_ALL);
function sql_insert ($data, $typeDef, $table) {
$mysqli = mysqli_connect("$host", "$user", "$pass", "programadorabr");
$keys = array_keys($data);
$vals = array_values($data);
$key = '';
$val = '';
$type = '';
foreach($keys AS $key_value) {
if($key == '') {
$key = 'cra_cadastro_'.$key_value;
$val = '?';
} else {
$key .= ', '.'cra_cadastro_'.$key_value;
$val .= ',?';
}
}
// echo $key.'<br />'.$val;
$stmt = $mysqli->prepare("INSERT INTO $table ($key) values ($val)");
if ($stmt !== false) {
for($i = 0; $i < count($typeDef); $i++) {
/* Set params value for binding */
$params[$keys[$i]] = $typeDef[$i];
}
/* Bind our params */
bindParameters($stmt, $params);
for($i = 0; $i < count($typeDef); $i++) {
/* Set params for query */
$params[$keys[$i]] = $vals[$i];
}
/* Execute the prepared Statement */
if(!$stmt->execute() ) { printf("Execute Statement Error: %s\n", $stmt->error); exit; }
/* Close the statement */
$stmt->close();
return true;
}
else {
/* Error */
die ("Mysql Error: " . $mysqli->error);
return false;
}
};
$json = json_decode(file_get_contents("php://input"));
$datas = array();
foreach($json as $key =>$value) {
$datas[$key] = $value;
}
$n = count($datas);
$s = array();
//print_r($datas);
for($z = 0; $z < $n; $z++) {
array_push($s,'s');
}
if(sql_insert($datas, $s, "cra_cadastro")) {
$to = "[email protected]";
$subject = "[CRA] Novo cadastro: $_POST[dados_id], $_POST[dados_nome]";
$txt = "$datas";
$headers = "From: $_POST[dados_email]" . "\r\n";
if(mail($to,$subject,$txt,$headers)){
return true;
};
}
else {
$to = "[email protected]";
$subject = "[CRA] ERRO: $_POST[dados_id], $_POST[dados_nome]";
$txt = "$datas";
$headers = "From: $_POST[dados_email]" . "\r\n";
mail($to,$subject,$txt,$headers);
return false;
}
?>