我试图让我的提交按钮变成一种不同的颜色并在我的一些if语句中禁用它但由于某种原因,如果我在我的函数之外回显$submitButtonColor & submitButtonDisabled
它没有获得新的颜色。如果我在我的功能凭证中回应它们,那么它的工作正常。
我必须在功能之外使用它们,因为我必须稍后在HTML中使用它们。
所以我的问题是我无法在函数外使用我的变量,即使我使用的是$ GLOBALS
的index.php
<?php
$date = "";
$begin = "";
$tijdsduur = "";
$aantal = "";
$color = "#29abe2";
$discountAmount = "";
$voucherexpired = "none";
$voucherexists = "none";
$getVoucherList = "https://www.planyo.com/rest/?method=list_vouchers&api_key=YOURKEY&resource_id=110556";
$cleanVoucherList = preg_replace("/ /", "%20", $getVoucherList);
$voucherlist = file_get_contents("$cleanVoucherList");
$voucherList = json_decode($voucherlist, true);
$submitButtonColor = "#29abe2";
$submitButtonDisabled = "";
function voucher()
{
$testsubject = $_POST['voucher'];
$doesvoucherexists = false;
foreach($GLOBALS['voucherList']['data']['results'] as $testVoucher => $testVoucherArr) {
if ($testsubject == $testVoucherArr['code']) {
$doesvoucherexists = true;
if (date("Y-m-d") <= $testVoucherArr['rental_end_date']) {
$GLOBALS['discountAmount'] = $testVoucherArr['discount_value'];
$GLOBALS['submitButtonColor'] = "#29abe2";
$GLOBALS['submitButtonDisabled'] = "";
echo "<span style='color: #cc00cc'>De voucher " . $testsubject . " is gebruikt voor een korting van €" . $GLOBALS['discountAmount'] . "</span>";
}
else {
$GLOBALS['voucherexpired'] = "inline";
$GLOBALS['submitButtonColor'] = "#cc00cc";
$GLOBALS['submitButtonDisabled'] = "disabled";
echo "<span style='color: #cc00cc'>Deze voucher is verlopen</span>";
}
break;
}
}
if ($doesvoucherexists === false && $testsubject == ""){
$GLOBALS['submitButtonColor'] = "#29abe2";
$GLOBALS['submitButtonDisabled'] = "";
}elseif ($doesvoucherexists === false){
$GLOBALS['voucherexists'] = "inline";
$GLOBALS['submitButtonColor'] = "#cc00cc";
$GLOBALS['submitButtonDisabled'] = "disabled";
echo "<span style='color: #cc00cc'>Deze voucher is niet geldig</span>";
}
}
echo $GLOBALS['submitButtonColor'] . $GLOBALS['submitButtonDisabled'];
if (isset($_POST['date']) && isset($_POST['quantity'])) {
if (isset($_POST['date'])) {
voucher();
$date = $_POST['date'];
$begin = $_POST['begin'];
$tijdsduur = $_POST['eind'];
$aantal = $_POST['quantity'];
$eind = $begin + $tijdsduur;
$startTijd = "$date " . $begin;
$eindTijd = "$date " . $eind . ":00";
$canmakereservation = "https://www.planyo.com/rest/?method=can_make_reservation&api_key=YOURKEY&resource_id=110556&start_time=$startTijd&end_time=$eindTijd&quantity=$aantal";
$cleancanmakereservation = preg_replace("/ /", "%20", $canmakereservation);
$reservationavailable = file_get_contents("$cleancanmakereservation");
$reservationAvailable = json_decode($reservationavailable, true);
$getrentalprice = "https://www.planyo.com/rest/?method=get_rental_price&api_key=YOURKEY&resource_id=110556&quantity=1&start_time=$startTijd&end_time=$eindTijd&quantity=$aantal";
$cleanRentalPrice = preg_replace("/ /", "%20", $getrentalprice);
$rentalprice = file_get_contents("$cleanRentalPrice");
$rentalPrice = json_decode($rentalprice, true);
$price = $rentalPrice['data']['total'] - $GLOBALS['discountAmount'];
if ($reservationAvailable['data']['is_reservation_possible'] == true){
$color = "#29abe2";
echo "Uw huurprijs exclusief borg: €" . $price;
}else{
$color = "#cc00cc";
echo $reservationAvailable['data']['reason'];
$GLOBALS['submitButtonColor'] = "#cc00cc";
$GLOBALS['submitButtonDisabled'] = "disabled";
}
echo "$color";
}
exit;
}
?>
答案 0 :(得分:0)
如果我理解你想要实现的目标。 基本上,您希望更改函数内部的变量值并访问函数外部的结果。要做到这一点,你可以这样做:
创建一个函数:
function voucher($btnDisabled){
if(someConditions == true){
$btnDisabled = "disabled";
}else{
$btnDisabled = "enabled";
}
return $btnDisabled;
}
调用您的函数并传递$submitButtonDisabled
作为参数:
$submitButtonDisabled = '';
voucher($submitButtonDisabled);
//in here, $submitButtonDisabled become 'disabled' or 'enabled' based on the conditions;
$submitButtonDisabled;
希望你明白〜
答案 1 :(得分:0)
试试这个
$date = ""; $begin = ""; $tijdsduur = ""; $aantal = ""; $color = "#29abe2"; $discountAmount = ""; $voucherexpired = "none"; $voucherexists = "none"; $getVoucherList = "https://www.planyo.com/rest/?method=list_vouchers&api_key=YOURKEY&resource_id=110556"; $cleanVoucherList = preg_replace("/ /", "%20", $getVoucherList); $voucherlist = file_get_contents("$cleanVoucherList"); $voucherList = json_decode($voucherlist, true); $submitButtonColor = "#29abe2"; $submitButtonDisabled = ""; function voucher($submitButtonColor = "#29abe2", $submitButtonDisabled = "" ) { $testsubject = $_POST['voucher']; $doesvoucherexists = false; foreach($GLOBALS['voucherList']['data']['results'] as $testVoucher => $testVoucherArr) { if ($testsubject == $testVoucherArr['code']) { $doesvoucherexists = true; if (date("Y-m-d") De voucher " . $testsubject . " is gebruikt voor een korting van €" . $GLOBALS['discountAmount'] . ""; } else { $GLOBALS['voucherexpired'] = "inline"; submitButtonColor = "#cc00cc"; $submitButtonDisabled = "disabled"; echo "Deze voucher is verlopen"; } break; } } if ($doesvoucherexists === false && $testsubject == ""){ $submitButtonColor = "#29abe2"; $submitButtonDisabled = ""; }elseif ($doesvoucherexists === false){ $GLOBALS['voucherexists'] = "inline"; $submitButtonColor = "#cc00cc"; $submitButtonDisabled = "disabled"; echo "Deze voucher is niet geldig"; } return array('submitButtonDisabled' => $submitButtonDisabled, 'submitButtonColor' => $submitButtonColor); } //echo $GLOBALS['submitButtonColor'] . $GLOBALS['submitButtonDisabled']; if (isset($_POST['date']) && isset($_POST['quantity'])) { if (isset($_POST['date'])) { $tmp = voucher($submitButtonColor, $submitButtonDisabled); $submitButtonDisabled = $tmp['submitButtonDisabled'] $submitButtonColor = $tmp['submitButtonColor'] echo $submitButtonColor . $submitButtonDisabled; $date = $_POST['date']; $begin = $_POST['begin']; $tijdsduur = $_POST['eind']; $aantal = $_POST['quantity']; $eind = $begin + $tijdsduur; $startTijd = "$date " . $begin; $eindTijd = "$date " . $eind . ":00"; $canmakereservation = "https://www.planyo.com/rest/?method=can_make_reservation&api_key=YOURKEY&resource_id=110556&start_time=$startTijd&end_time=$eindTijd&quantity=$aantal"; $cleancanmakereservation = preg_replace("/ /", "%20", $canmakereservation); $reservationavailable = file_get_contents("$cleancanmakereservation"); $reservationAvailable = json_decode($reservationavailable, true); $getrentalprice = "https://www.planyo.com/rest/?method=get_rental_price&api_key=YOURKEY&resource_id=110556&quantity=1&start_time=$startTijd&end_time=$eindTijd&quantity=$aantal"; $cleanRentalPrice = preg_replace("/ /", "%20", $getrentalprice); $rentalprice = file_get_contents("$cleanRentalPrice"); $rentalPrice = json_decode($rentalprice, true); $price = $rentalPrice['data']['total'] - $GLOBALS['discountAmount']; if ($reservationAvailable['data']['is_reservation_possible'] == true){ $color = "#29abe2"; echo "Uw huurprijs exclusief borg: €" . $price; }else{ $color = "#cc00cc"; echo $reservationAvailable['data']['reason']; $submitButtonColor = "#cc00cc"; $submitButtonDisabled = "disabled"; } echo "$color"; } exit; } ?>