[英]Manipulation of an array sent by ajax to a php file, within a function of another file
我有一個用JS定義的數組,並通過AJAX將其發送到名為aux.php的PHP文件。 我需要的是,此數組可以在另一個名為Payments.php的PHP文件的類中的函數中可見和操縱。
我將所有代碼傳遞給他們,以便他們可以了解我在做什么:
HTML格式:
<form method="post" onsubmit="return validateForm()" id="formCash">
<input type="text" class="form-control" id="registroUsuario" name="registroUsuario" placeholder="Nombre Completo" maxlength="26" required>
<input type="text" class="form-control" id="registroDireccion" name="registroDireccion" placeholder="Dirección de envío con altura de calle" required>
<input type="text" class="form-control" id="registroCalendario" name="registroCalendario" placeholder="Día de envío" required>
<input type="email" class="form-control" id="registroEmail" name="registroEmail" placeholder="Correo Electrónico" maxlength="32" required>
<input type="text" class="form-control" id="registroTelefono" name="registroTelefono" placeholder="Teléfono de contacto" maxlength="16" onkeypress="return isNumber(event)" required>
<?php
$payments = new Payments();
$payments -> ctrCash();
?>
<input type="submit" class="btn btn-block btn-lg btn-default backColor btnPagar" id="btnPagar" value="CONFIRMAR PEDIDO">
</form>
包含數組並將其通過AJAX發送到aux.php的Javascript函數:
$("#btnPagar").click(function(){
var total = $(".valorTotalCompra").html();
var envio = $(".valorTotalEnvio").html();
var subtotal = $(".valorSubtotal").html();
var titulo = $(".valorTitulo");
var cantidad = $(".valorCantidad");
var valorItem = $(".valorItem");
var idProducto = $('.cuerpoCarrito button, .comprarAhora button');
var tituloArray = [];
var cantidadArray = [];
var valorItemArray = [];
var idProductoArray = [];
for(var i = 0; i < (titulo.length/2); i++){
tituloArray[i] = $(titulo[i]).html();
cantidadArray[i] = $(cantidad[i]).html();
valorItemArray[i] = $(valorItem[i]).html();
idProductoArray[i] = $(idProducto[i]).attr("idProducto");
}
var finalArray = [];
for(var i = 0; i < (tituloArray.length); i++){
finalArray[i] = {titulo : tituloArray[i], cantidad : cantidadArray[i], valorItem : valorItemArray[i], idProducto : idProductoArray[i]}
}
completeArray = JSON.stringify(finalArray);
$.ajax({
type: "POST",
url: "aux.php",
data: { completeArray : completeArray },
success: function(data) {
console.log(data);
}
});
})
文件aux.php,其中數組正確到達,並且var_dump給我正確的結果:
if(isset($_POST['completeArray'])){
$completeArray = json_decode($_POST['completeArray'], true);
var_dump("The array is", $completeArray);
Payments.php文件的類和功能,用於驗證服務器端表單的字段, 這是我需要能夠操縱數組的地方 :
class Payments {
public function ctrCash(){
if(isset($_POST["registroUsuario"])){
if(preg_match('/^[a-zA-ZñÑáéíóúÁÉÍÓÚ ]+$/', $_POST["registroUsuario"]) &&
preg_match('/^([0-2][0-9]|3[0-1])(\/|-)(0[1-9]|1[0-2])\2(\d{4})$/', $_POST["registroCalendario"]) &&
preg_match('/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/', $_POST["registroEmail"]) &&
preg_match('/^(?:\D*\d){2,4}\D*$/', $_POST["registroDireccion"]) &&
preg_match('/^[0-9]{7,12}$/', $_POST["registroTelefono"])) {
//include ('aux.php');
}
正如我在評論中提到的那樣,您的PHP輸出僅在腳本首次運行時(在頁面呈現之前)生成,因此您無法在那里獲得將來提交的數據。 因此,如果您不將數據提交到與呈現表單相同的頁面,則需要從JS(而不是從PHP)將結果呈現到頁面。
因此,用div替換HTML頁面上的PHP代碼段,如下所示: <div id='ajax_output'></div>
,然后將結果寫入Javascript的AJAX調用中的成功回調中:
success: function(data) {
document.getElementById('ajax_output').innerHTML = data;
}
因此,現在您的aux.php可以回顯ctrCash
的結果,而您的AJAX腳本將提取該結果並顯示出來。 因此,當您在aux.php(實際上是在獲取數據的腳本)中調用ctrCash
函數時,請將$completeArray
作為參數傳遞(並在函數簽名中將其定義為按引用,例如function ctrCash(&$completeArray)
或類似)。 該函數將具有對原始數組的完全訪問權限以進行修改,並且在調用之后,aux.php可以回顯結果,但是您希望顯示它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.