I have a form validation issue. Below is the logic that happens on submit (part of it at least.) In the for loop, we check an array of possible events that a site visitor can register for. If the user hasn't checked any events (these are checkboxes because a user can register for multiple events), we should enter the second if statement below, but for some reason we're not. I know that none of the post variables are set if nothing is checked and, by setting a session variable equal to the variable $ECEventCnt, I'm able to varify that if nothing is posted, that variable is equal to 0. However, we seem to never get into the second if statement. Any thoughts?
unset($_SESSION["ECEvents"]);
$ECEventsArray = array();
$ECEventCnt = 0;
$_SESSION['debug'] = 'EC';
for ($i=0; $i<count($Val_WhichEventTypes); $i++) {
$key = $Val_WhichEventTypes[$i]["eventKey"];
//echo 'key' . $key;
if (isset($_POST["WhichEvent-" . $key]) && $_POST["WhichEvent-" . $key] == $key) {
$_SESSION['debug'] .= ' we made it to the place.' . $_POST["WhichEvent-" . $key];
$ECEventsArray[$key] = $key ;
if (strlen($ECEventsArray[$key])) $ECEventCnt += 1; // Only advance counter if EC Event is checked (key value is set)
}
}
$_SESSION['ecventcount'] = $ECEventCnt;
if ($ECEventCnt = 0) {
set_step_INvalid(5);
$_SESSION['debug'] .= ' we made it to the 2nd place.';
$cnt += 1;
$ValidationError .= ((strlen($ValidationError)==0) ? "" : ", ") . "<br />Please just select at least one Event Type/Time";
}
$_SESSION["ECEvents"] = $ECEventsArray;
//valid_step52();
}
if ($ECEventCnt = 0) {
should be
if ($ECEventCnt == 0) {
You are assigning to the variable $ECEventCnt, but what you mean to do is compare using it.
Related
I want to display a specific value from key value list..
here is my code:
if (isset($_POST) && count($_POST)>0 )
{
foreach($_POST as $paramName => $paramValue) {
echo "<br/>" . $paramName . " = " . $paramValue;
}
}
ouput
ORDERID = ORDS3700373
TXNAMOUNT = 200.00
CURRENCY = INR
TXNID = 32221284
BANKTXNID = 475815
STATUS = TXN_SUCCESS
RESPCODE = 01
RESPMSG = Txn Successful.
TXNDATE = 2017-01-10 18:13:25.0
GATEWAYNAME = WALLET
BANKNAME =
PAYMENTMODE = PPI
CHECKSUMHASH =
here I want to display only ORDERID and TXNID.. How do I get that value?
You can easily access post values by it's field name instead of looping through all post elements. Simply access that elements directly as below:
if(isset($_POST['ORDERID'])) {
echo 'ORDERID = '.$_POST['ORDERID'];
}
if(isset($_POST['TXNID'])) {
echo 'TXNID= '.$_POST['TXNID'];
}
Moving comments to an answer.
You do not need to loop post it is just a global array. You can access the values at any of the keys like any associative array because that is what it is. Likewise these value can be used like any other
if(isset($_POST['ORDERID'])){
$orderid = $_POST['ORDERID'];
}
if(isset($_POST['TXNID'])){
$txnid = $_POST['TXNID'];
}
// Should use htmlspecialchars() or htmlentities() here
// but didn't want to confuse OP. It is for security.
echo "ORDERID is: " . $orderid . " and TXNID is: " . $txnid;
A note for security never trust user input and sanitize all $_POST variables before echoing or persisting. There are far better article out on the internet than I can summarize here.
You can use if condition in the loop like this
if (isset($_POST) && count($_POST)>0 )
{
foreach($_POST as $paramName => $paramValue) {
if($paramName == 'ORDERID' || $paramName == 'TXNID')
echo "<br/>" . $paramName . " = " . $paramValue;
}
}
add an if like
if($paramName == "ORDERID" || $paramName == "TXNID") {
after foreach, remeber to close it after echo statement line
Don't overcomplicate a trivial task with a loop.
Just drop the loop and echo the two values directly:
// Assuming the two values are expected to come in pair:
if(isset($_POST['ORDERID']) && isset($_POST['TXNID'])) {
echo "<br/>ORDERID = " . $_POST['ORDERID'];
echo "<br/>TXNID = " . $_POST['TXNID'];
}
If you insist on having a loop, then you can go through the property names which you need
foreach(array('ORDERID', 'TXNID') as $paramName) {
if(isset($_POST[$paramName])) {
echo "<br/>" . $paramName . " = " . $_POST[$paramName];
}
}
i've been hours trying to assign value in the array but every iteration the array is set to 0 and i can't understand why , here's the code ...
$AurQ = array();
$prod = '';
while ($NOTIFY = $notification->fetch_assoc()) {
print_r($AurQ);
if ($NOTIFY['notification_type_ID'] == 1) {
if (in_array($NOTIFY['qID'], $AurQ, true)) {
echo "exist";
continue;
} else {
$AurQ[] = $NOTIFY['qID']; // Adding the value to the array
$prod .= '<li><a href="http://localhost/website/link/' . $NOTIFY['qID'] . '"> <span class="AskIcon"></span><span class="noti_announce">';
$prod .= '<span class="unB">' . $NOTIFY['first_name'] . ' ' . $NOTIFY['last_name'] . '</span> ' . $NOTIFY['notify_name_en'] . ' "' . $NOTIFY['q_title'] . '"</span>';
$prod .= '<span class="noti_time"><span class="icon"></span>' . time_elapsed_string($NOTIFY['time']) . '</span></a></li>';
} // end of if doesn't exist in Array List
} //end of if
} // end of loop
The problem appears to be that you're pushing a value to the array only after checking if it already exists in that array, which it doesn't given that you're initializing it as an empty array just above it.
So, there's a logic error here, and depending on what you're trying to do, it can be fixed by moving the $AurQ[] = $NOTIFY['qID'] ; line into the else statement, or by changing your if statement from if (in_array($NOTIFY['qID'], $AurQ, true)) { to if (!in_array($NOTIFY['qID'], $AurQ, true)) {.
As you didn't mention an issue with things displaying when you didn't want them to, I'm going to assume you're looking for the former solution as opposed to the latter. So, your code will wind up looking something like this:
$AurQ = array();
$prod ='';
while($NOTIFY = $notification->fetch_assoc()) {
if ($NOTIFY['notification_type_ID'] == 1) {
if (in_array($NOTIFY['qID'], $AurQ, true)){ //If it's ALREADY in the array
echo "exist";
continue;
} else { //Otherwise we need to add it, and display it
$AurQ[] = $NOTIFY['qID'] ; // Adding the value to the array
$prod .= '<li><a href="http://localhost/website/link/'.$NOTIFY['qID'].'"> <span class="AskIcon"></span><span class="noti_announce">';
$prod .= '<span class="unB">'.$NOTIFY['first_name'].' '.$NOTIFY['last_name'].'</span> '.$NOTIFY['notify_name_en'].' "'.$NOTIFY['q_title'].'"</span>';
$prod .= '<span class="noti_time"><span class="icon"></span>'.time_elapsed_string($NOTIFY['time']).'</span></a></li>';
}// end of if doesn't exist in Array List
}//end of if
} // end of loop
Given your code you just need to move $AurQ[] = $NOTIFY['qID']; to else block.
Currently, $AurQ[] is empty and if block will never run because in_array($NOTIFY['qID'], $AurQ, true) will always return false.
I have a Mailchimp radio button setup for Yes/No. I'm having trouble testing it's value from PHP with something like this...
function draw_results_detail ($hitNum) {
$result = '';
if ($hitNum >= count($_SESSION['hitList'])) return $result;
if ($hitNum < 0) return $result;
$aHit = $_SESSION['hitList'][(int) $hitNum];
$result .= sprintf(
"%s %s<h2>%s %s</h2>",
$aHit->membership,
$aHit->affiliation,
htmlspecialchars(capitalize_scrub($aHit->fname)),
htmlspecialchars(capitalize_scrub($aHit->lname))
);
if (FALSE !== strpos(strtolower($aHit->unlisted),'yes')) {
$result .= "<p>Email address and telephone number are unlisted.</p>";
} else {
$result .= sprintf(
"<p>Email: <a href=\"mailto:%s\">%s<a/>" .
"<br/>Phone: %s</p>",
htmlspecialchars(strtolower($aHit->email)),
htmlspecialchars(strtolower($aHit->email)),
htmlspecialchars(strtolower($aHit->phone))
);
}
$temp = $aHit->modified;
if ($temp != '') {
$result .= "<span style=\"font-size:10px;\">(last modified: $temp UTC)</span>";
}
$temp = count($_SESSION['hitList']);
$hitNum += 1;
$result .= "<span style=\"font-size:10px;\"> [$hitNum of $temp]</span>";
return $result;
}
The central IF statement fails to trigger. If I "var_dump" $aHit, the "unlisted" key is String(0) even though Mailchimp shows the corresponding record's button is selected to Yes.
How can I test the button setting from Mailchimp?
Make sure the text of the Mailchimp radio button items EXACTLY matches the text of the corresponding SQL data element.
In this case, a webhook was updating the SQL record whenever Mailchimp data was modified. The text of the item wasn't being set correctly.
Rookie mistake, and not even in the snippet shown. Sorry for the confusion.
am having a problem with a bit of code.
I am trying to generate a unique name to insert into a database.
i have created the following function which checks to see if the name already exists:
function checkExists($database_reelfilm, $reelfilm, $mySlug, $locVal){
$mmid_rs_slugCheck = "-1";
if (isset($mySlug)) {
$mmid_rs_slugCheck = $mySlug;
}
$mmid2_rs_slugCheck = "-1";
if (isset($locVal)) {
$mmid2_rs_slugCheck = $locVal;
}
mysql_select_db($database_reelfilm, $reelfilm);
$query_rs_slugCheck = sprintf("SELECT * FROM locations_loc WHERE locations_loc.slug_loc = %s AND locations_loc.id_loc != %s", GetSQLValueString($mmid_rs_slugCheck, "text"),GetSQLValueString($mmid2_rs_slugCheck, "int"));
$rs_slugCheck = mysql_query($query_rs_slugCheck, $reelfilm) or die(mysql_error());
$row_rs_slugCheck = mysql_fetch_assoc($rs_slugCheck);
$totalRows_rs_slugCheck = mysql_num_rows($rs_slugCheck);
if($totalRows_rs_SlugCheck > 0){
return true;
}else{
return false;
}
};
i then create a loop to checking if the variable name exists, if it does i want it to add the value of the counter to the variable name then recheck to see if that exists until i have a unique name which i can then save to my db.
$updateVal = slugify($row_rs_locations['name_loc']);
$newSlug = slugify($row_rs_locations['name_loc']);
$locVal = $row_rs_locations['id_loc'];
//echo(slugify($row_rs_locations['name_loc']));
$checkCount = 1;
$isDupe = '<BR>';
while(checkExists($database_reelfilm, $reelfilm, $newSlug, $locVal)){
$isDupe = 'Duplicate Added ' . $checkCount . ' to slug...<BR>';
$newSlug = $newVal . $checkCount;
$checkCount ++;
}
if($updateVal != $newVal){
$updateVal = $newSlug;
}
I am obviously doing something wrong, I need the while loop on the next iteration to use the newSlug value set in the loop, from my various attempts i am not at all sure if this is possible.
Whats the best way to accomplish this?
$newVal is not ever given a value, but it is used twice (second block of code). I think you need something like:
$newSlug = slugify($row_rs_locations['name_loc']);
$newVal = $newSlug;
i need the while loop on the next iteration to use the newSlug value
set in the loop
In the while loop you do
$newSlug = $newVal . $checkCount;
But $newVal does not exist. Replace that line with the following:
$newSlug .= $checkCount;
I am trying to create logic to see what checkbox is selected (of 5 possible checkboxes) and if it is selected assign it a value of 0. if it is not selected I was to assign it a value of 1. The proceeding code snippet highlight this but throws a parse error in my else statement and I cannot fighure out why.
//Check to see what checkbox is marked for correct answer
//Correct answer variables
$chkBox1 = 'unchecked';
$chkBox2 = 'unchecked';
$chkBox3 = 'unchecked';
$chkBox4 = 'unchecked';
$chkBox5 = 'unchecked';
if (isset($_POST['chkBox1'])) {
if ($chkBox1 == 'chkBox1Selected') {
$chkBox1 = '0';
}
else{
$chkBox1 = '1';
}
}//End of chkBox1Selected logic
You don't understand how checkboxes work. If a checkbox is deselected before posting, it will not be set on post.
Therefore, the only condition that will ever be present in your code is that every value will show as 1, since they cannot be overridden.
Take this snippet and try it out. It dynamically loops for the amount of variables you need and assigns the values based upon the submitted value.
$_POST['chkBox4'] = 'test';
for( $i = 1; $i <= 5; $i++ )
{
$name = 'chkBox' . $i;
$$name = !isset( $_POST[$name] ) ? 0 : $_POST[$name];
}
print $chkBox2 . ' // '. $chkBox4;
http://codepad.org/51RotnCf
Ok I got it to work from a syntax standpoint, however now no matter what is selected it is still assigning a value of 1 to all the checkboxes and not changing the selected checkbox to a value of 0. Here is the new code that is correct from a syntax standpoint but defaults to 1 no matter what:
//Check to see what checkbox is marked for correct answer
//Correct answer variables
$chkBox1 = '1';
$chkBox2 = '1';
$chkBox3 = '1';
$chkBox4 = '1';
$chkBox5 = '1';
if (isset($_POST['chkBox1'])) {
if ($chkBox1 == 'chkBox1Selected') {
$chkBox1 = '0';
}
}//End of chkBox1Selected logic
if (isset($_POST['chkBox2'])) {
if ($chkBox2 == 'chkBox2Selected') {
$chkBox2 = '0';
}
}//End of chkBox2Selected logic
if (isset($_POST['chkBox3'])) {
if ($chkBox3 == 'chkBox3Selected') {
$chkBox3 = '0';
}
}//End of chkBox3Selected logic
if (isset($_POST['chkBox4'])) {
if ($chkBox4 == 'chkBox4Selected') {
$chkBox4 = '0';
}
}//End of chkBox4Selected logic
if (isset($_POST['chkBox5'])) {
if ($chkBox5 == 'chkBox5Selected') {
$chkBox5 = '0';
}
}//End of chkBox5Selected logic