Checking post data of a checkbox - php

I have a checkbox that I'm looking to update a contacts details with. At the moment it's not updating the contact in Send in Blue... I don't think that's the problem though.
There's 3 issues I'm not sure I'm doing correctly:
Checking if the data has been sent.
Checking the field data that already uses square brackets.
Checking to see if the checkbox is checked or not.
Here's my php:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
global $SendInBlue;
if(isset($_POST['data[newsletter_sub]']) == "on") {
$data_in['ONETIME_NEWSLETTER'] = true;
$data_in['PROMOS'] = true;
$data_in['SUB_NEWSLETTER'] = true;
} else {
$data_in['ONETIME_NEWSLETTER'] = false;
$data_in['PROMOS'] = false;
$data_in['SUB_NEWSLETTER'] = false;
}
// Update SiB contact
$data = array(
'attributes'=> $data_in
);
try {
$updateContact = new \SendinBlue\Client\Model\UpdateContact($data);
$result = $SendInBlue->updateContact($email, $updateContact);
return true;
} catch (Exception $e) {
}
}
?>
Here's some of the form with one checkbox:
<form method="POST" class="" id="communication_preferences" action="/event" name="communication_preferences">
<input type="checkbox" name="data[newsletter_sub]" id="newsletter_sub" <?php if($newsletter_sub == 'yes'){ echo 'checked';}?>>
<button class="button button--pink button--uppercase text--three js-profile-item-submit landmark ">
Save Changes
</button>
</form>
The page just reloads when you submit the form.

The problem with checkboxes is that they do not appear in the $_POST when not checked.
Your receiving script can simple do this:
$bMyCheckOn = isset($_POST["someCheckBoxName"]); // true/false

Please make sure that if (!empty($_POST['newsletter_sub'])) { matches your input.
Here, i rewrote your entire script:
<?php
if (!empty($_POST['submit'])) {
global $SendInBlue;
$data_in = [];
foreach(['ONETIME_NEWSLETTER', 'PROMOS', 'SUB_NEWSLETTER'] as $data) {
if (!empty($_POST['newsletter_sub'])) { //Checkbox set?
$data_in[$data] = true;
} else {
$data_in[$data] = false;
}
}
$data = ['attributes' => $data_in];
$attempt = new \SendinBlue\Client\Model\UpdateContact($data);
$result = $SendInBlue->updateContact($email, $attempt) ? true : false;
print_r($result);
} else {
echo 'Form not submitted.';
}
?>
<form method="POST" id="communication_preferences" action="">
<input type="checkbox" name="newsletter_sub" id="newsletter_sub" <?=($newsletter_sub == 'yes' ? 'checked' : '')?>>
<button type="submit" class="button button--pink button--uppercase text--three js-profile-item-submit landmark" name="submit" value="submit">
Save changes
</button>
</form>

Related

Form inserting empty data to database

Whenever I submit the form with empty input, it sends the empty input to my database. The form was working fine until after I used the htmlentities() for its functionality.
I used the gettype() function to return what's in the inserted variable and it returns "string", but when I checked the code from the browser, I could not see anything in the table.
This is the code snippet and the form processing code
<?php
$errors = [];
$missing = [];
if(isset($_POST['sendFirm']) ){
$expected = array('firmName','country','state','email','phoneNumber');
$required = array('firmName','country','state','phoneNumber');
<?php
foreach ($_POST as $key => $value) {
if(is_array($value)){
$value = $value;
}else{
$value = trim($value);
}
if(empty($value) && in_array($key, $required)){
$missing[] = $key;
$$key = '';
}elseif(in_array($key, $expected)){
$$key = "$value";
}
}
?>
}
?>
<?php
if($errors || $missing){
?>
<p>Please fix the following items</p>
<?php } ?>
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<div class="form-group">
<label>Compnay Name
<?php if($missing && in_array('firmName', $missing)) { ?>
<span class="text-danger">Please enter firm name</span>
<?php } ?>
</label>
<input class="form-control" type="text" name="firmName" id="firmName" placeholder="Company Name"
<?php
if($errors || $missing){
print 'value="' . htmlentities($firmName) . '"';
}
>
<button class="btn btn-primary" type="submit"
name="sendFirm">Submit</button>
</form>
?>
>
<?php
if(isset($_POST['sendFirm'])){
try {
$connectToFirms = new
PDO('mysql:host=localhost;dbname=firms','root','2332');
$connectToFirms->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
$prepInsertion = $connectToFirms->prepare('INSERT INTO contractors
VALUES (null,?,?,?,?,?)');
$prepInsertion->execute(array($firmName, $country, $state, $email,
$phoneNumber));
}catch (PDOException $e) {
print "An error occure: " . $e->getMessage();
}
}
?>
The form is expected to insert inputs into the database only if the input is not empty and is also in the $expected[];
Don't insert data
I would stop the whole data insertion, if the expected input is not given. I would also send the input data one by one to PHP, so you have a better overview over your code. Good overview = less errors ;)
Try it this way:
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$firmname = htmlentities($_POST["firmName"], ENT_QUOTES);
$country = htmlentities($_POST["country"], ENT_QUOTES);
$state = htmlentities($_POST["state"], ENT_QUOTES);
$pn = htmlentities($_POST["phoneNumber"], ENT_QUOTES);
// LET'S START THE VALIDATION
// if the required fields are not empty, insert data
if (!empty($firmname) && !empty($country) && !empty(state) && !empty($pn)) {
// insert data
} else { // else stop insertion and return error message
// return error message
}
} else {
// redirect...
}
I hope, i understood your question correctly and could help.

Send radio selection to function along with textbox

I've got some code working that takes the text from my input box and moves it across to a function. I'm now trying to change it so I add another form element, a radio button and I want to access the choice within my functions.php file.
This is my current code which works for the post name, but what if I want to also grab the colours boxes that was selected too?
main.php
<?php
if (isset($_POST['submit'])) {
$data = $_POST['name']; // the data from the form input.
}
?>
...
<form action="/" method="post">
<input type="text" name="name" placeholder="Acme Corp"/>
<input name="colour" type="radio" value="red">Red<br>
<input name="colour" type="radio" value="blue">Blue<br>
<input name="colour" type="radio" value="green">Green<br>
<input type="submit" name="submit" value="Submit">
</form>
<img src="pngfile.php?data=<?php print urlencode($data);?>"
alt="png php file">
I guess I confused because currently it is calling this:
pngfile.php
<?php
require_once 'functions.php';
$inputData = urldecode($_GET['data']);
process($inputData);
exit;
?>
Which calls functions.php
<?php
function process($inputdata)
{
...
EDIT: What I have tried:
main.php [Change]
$data = $_POST['name'] && $_POST['colour']
But I'm not really sure how to progress.
Never trust user input. Sanitize and validate your inputs before using them.
This can be arranged better, but the basics are still true.
PHP Manual: filter_input_array()
PHP Manual: filter_var_array()
Small Function Library
function sanitizeArray($filterRules)
{
return filter_input_array(INPUT_POST, $filterRules, true)
}
function validateArray($filteredData, $validationRules)
{
return filter_var_array($filteredData, $validationRules, true);
}
function checkFilterResults(array $testArray, array &$errors)
{
if (!in_array(false, $testArray, true) || !in_array(null, $testArray, true)) {
foreach($testArray as $key => $value)
{
$errors[$key] = '';
}
return true;
}
if ($testArray['name'] !== true) { //You can make a function and do various test.
$errors['name'] = 'That is not a valid name.';
}
if ($testArray['clour'] !== true) { //You can make a function and do many test.
$errors['colour'] = 'That is not a valid colour.';
}
return false;
}
function processUserInput(array &$filteredData, array $filterRulesArray, array $validationRulesArray, array &$cleanData, array &$errors)
{
$filteredInput = null;
$tempData = sanitizeArray($filterRulesArray);
if (!$checkFilterResults($tempData, $errors)){
throw new UnexpectedValueException("An input value was unable to be sanitized.");
//Consider forcing the page to redraw.
}
$filteredData = $tempData;
$validatedData = validateArray($filteredData, $validationRulesArray);
if (!$checkFilterResults($validatedData, $errors)){
return false;
}
$errors['form'] = '';
$cleanData = $validatedData;
return true;
}
function htmlEscapeArray(array &$filteredData)
{
foreach($filteredData as $key => &$value)
{
$value = htmlspecialchars($value, ENT_QUOTES | ENT_HTML5, 'UTF-8', false);
}
return;
}
Basic Main Line
try {
$filterRulesArray = []; //You define this.
$filteredData = []; //A temporary array.
$validationRulesArray = []; //You define this.
$validatedData = null; //Another temporary array.
$results = null; //Input processing results: true or false.
$cleanData = null; //Filtered and validated input array.
$errors = []; //Any errors that have accumulated.
if (isset($_POST, $_POST['submit'], $_POST['colour']) && !empty($_POST)) {
$results = processUserInput($filteredData, $filterRulesArray, $validationRulesArray, $cleanData, $errors);
} else {
$errors['form'] = "You must fill out the form."
}
if ($results === true) {
$name = $cleanData['name']; //You can do what you want.
$colour = $cleanData['colour']; //You can do what you want.
//header("Location: http://url.com/registration/thankYou/")
//exit;
}
//Prepare user input for re-display in browser
htmlEscapeArray($filteredData);
} catch (Exception $e) {
header("Location: http://url.com/samePage/"); //Force a page reload.
}
Let the form redraw if input processing fails.
Use the $errors array to display error messages.
Use the $filteredData array to make the form sticky.
<html>
<head>
<title>Your Webpage</title>
</head>
<body>
<h1>My Form</h1>
<form action="/" method="post">
<!-- Make spots for error messages -->
<input type="text" name="name" placeholder="Acme Corp" value="PUT PHP HERE"/>
<!-- No time to display sticky radios! :-) -->
<input name="colour" type="radio" checked="checked" value="red">Red<br>
<input name="colour" type="radio" value="blue">Blue<br>
<input name="colour" type="radio" value="green">Green<br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Tip:
It may be better to submit numbers for radios, as opposed to longer string values like (red, green, blue). Numbers are easier to sanitize and validate. Naturally, then you must translate the input number into its corresponding string. You would do that after validation has finished, but before using the values. Good luck!
you can access this using array like this.
$data[] = $_POST['name'];
$data[] =$_POST['colour'];
Or combine both variable
$data = $_POST['name'].'&'.$_POST['colour'];
Use Array in php for this process as follows:
if (isset($_POST['submit'])) {
$array_val = array(
"name"=> $_POST['name'],
"color"=> $_POST['color']
);
}

Passing information using post method without session variables

I will admit immediately that this is homework. I am only here as a last resort after I cannot find a suitable answer elsewhere. My assignment is having me pass information between posts without using a session variable or cookies in php. Essentially as the user continues to guess a hidden variable carries over all the past guesses up to that point. I am trying to build a string variable that holds them all and then assign it to the post variable but I cannot get anything to read off of the guessCounter variable i either get an undefined index error at the line of code that should be adding to my string variable or im just not getting anything passed over at all. here is my code any help would be greatly appreciated as I have been at this for awhile now.
<?php
if(isset($_POST['playerGuess'])) {
echo "<pre>"; print_r($_POST) ; echo "</pre>";
}
?>
<?php
$wordChoices = array("grape", "apple", "orange", "banana", "plum", "grapefruit");
$textToPlayer = "<font color = 'red'>It's time to play the guessing game!(1)</font>";
$theRightAnswer= array_rand($wordChoices, 1);
$passItOn = " ";
$_POST['guessCounter']=$passItOn;
$guessTestTracker = $_POST['guessCounter'];
$_POST['theAnswer'] = $theRightAnswer;
if(isset($_POST['playerGuess'])) {
$passItOn = $_POST['playerGuess'];
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$guessTestTracker = $_GET['guessCounter'];
$theRightAnswer = $_GET['theAnswer'];
}
else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if(isset($_POST['playerGuess'])) {
if(empty($_POST['playerGuess'])) {
$textToPlayer = "<font color = 'red'>Come on, enter something(2)</font>";
}
else if(in_array($_POST['playerGuess'],$wordChoices)==false) {
$textToPlayer = "<font color = 'red'>Hey, that's not even a valid guess. Try again (5)</font>";
$passItOn = $_POST['guessCounter'].$passItOn;
}
if(in_array($_POST['playerGuess'],$wordChoices)&&$_POST['playerGuess']!=$wordChoices[$theRightAnswer]) {
$textToPlayer = "<font color = 'red'>Sorry ".$_POST['playerGuess']." is wrong. Try again(4)</font>";
$passItOn = $_POST['guessCounter'].$passItOn;
}
if($_POST['playerGuess']==$wordChoices[$theRightAnswer]) {
$textToPlayer = "<font color = 'red'>You guessed ".$_POST['playerGuess']." and that's CORRECT!!!(3)</font>";
$passItOn = $_POST['guessCounter'].$passItOn;
}
}
}
}
$_POST['guessCounter'] = $passItOn;
$theRightAnswer=$_POST['theAnswer'];
for($i=0;$i<count($wordChoices);$i++){
if($i==$theRightAnswer) {
echo "<font color = 'green'>$wordChoices[$i]</font>";
}
else {
echo $wordChoices[$i];
}
if($i != count($wordChoices) - 1) {
echo " | ";
}
}
?>
<h1>Word Guess</h1>
Refresh this page
<h3>Guess the word I'm thinking</h3>
<form action ="<?php echo $_SERVER['PHP_SELF']; ?>" method = "post">
<input type = "text" name = "playerGuess" size = 20>
<input type = "hidden" name = "guessCounter" value = "<?php echo $guessTestTracker; ?>">
<input type = "hidden" name = "theAnswer" value = "<?php echo $theRightAnswer; ?>">
<input type = "submit" value="GUESS" name = "submitButton">
</form>
<?php
echo $textToPlayer;
echo $theRightAnswer;
echo $guessTestTracker;
?>
This is a minimal functional example of what you need to do. There are still a couple of minor bugs (like duplicate entries in the history), but I've left these as an exercise for you. Treat this as a starting point and build up what you need from it.
I've added comments to explain what's happening, so hopefully it is clear to you.
$answer = null;
$history = [];
$choices = ['apple', 'grape', 'banana'];
$message = '';
// check if a guess has been made.
if (!empty($_POST) && !empty($_POST['guess'])) {
// check if previous guesses have been made.
if (!empty($_POST['history'])) {
$history = explode(',', $_POST['history']);
}
// check guess.
if (!empty($_POST['answer']) && !empty($_POST['guess'])) {
// check guess and answer are both valid.
if (in_array($_POST['guess'], $choices) && isset($choices[$_POST['answer']])) {
if ($_POST['guess'] == $choices[$_POST['answer']]) {
// correct; clear history.
$history = [];
$message = 'correct!';
} else {
// incorrect; add to history and set previous answer to current.
$history[] = $_POST['guess'];
$answer = $_POST['answer'];
$message = 'incorrect!';
}
} else {
// invalid choice or answer value.
}
}
}
if (empty($answer)) {
// no answer set yet (new page load or correct guess); create new answer.
$answer = rand(0, count($choices) - 1);
}
?>
<p>Guess the word I'm thinking:</p>
<p><?php echo implode(' | ', $choices) ?></p>
<form method="POST">
<input type="hidden" name="answer" value="<?php echo $answer; ?>">
<input type="hidden" name="history" value="<?php echo implode(',', $history); ?>">
<input type="text" name="guess">
<input type="submit" name="submit" value="Guess">
</form>
<p><?php echo $message; ?></p>

make sure atleast one checkbox is selected php

I have a little mailing form with a few checkboxes. At least one of the boxes need to be selected before mailing should start.
My HTML:
<input type='checkbox' id='part1' name='box1' value='box1' checked>
<label for="part1">Voor Leden agenda</label>
<br/>
<input type='checkbox' id='part2' name='box2' value='box2' checked>
<label for="part2">Voor Leiding agenda</label>
<br/>
<input type='checkbox' id='part3' name='box3' value='box3' checked>
<label for="part3">Verhuur agenda</label>
<br/>
<button type='submit' name='send'/>send</button>
My PHP:
if (isset($_POST['box1'])) {
$box1 = 'yes';
} else {
$box1 = 'No';
}
if (isset($_POST['box2'])) {
$box2 = 'yes';
} else {
$box2 = 'No';
}
if (isset($_POST['box3'])) {
$box3 = 'yes';
} else {
$box3 = 'No';
}
i would like to have a script that gives a message like below if no checkbox is selected:
if()
{
echo "<p class='redfont'>no checkboxes are selected</p>";
echo "<p><a href='javascript:history.back();'>Click to go back</a></p>";
}
edit: how can I give this message with php, only if all boxes are unchecked
if(!isset($_POST['box1']) && !isset($_POST['box2']) && !isset($_POST['box3']))
{
// none is set
}
You could even apply De Morgan's law and write this equivalent expression
if(isset($_POST['box1']) || isset($_POST['box2']) || isset($_POST['box3']))
{
// at least one of them is set
}
You could even send those 3 parameters to 1 isset call but then that would check if all of them are set, which is not your requirement.
Try this:
if(isset($_POST["box1"]) || isset($_POST["box2"]) || isset($_POST["box3"])) {
if(isset($_POST['box1'])) {
$box1 = 'yes';
} else {
$box1 = 'No';
}
if(isset($_POST['box2'])) {
$box2 = 'yes';
} else {
$box2 = 'No';
}
if(isset($_POST['box3'])) {
$box3 = 'yes';
} else {
$box3 = 'No';
}
} else {
echo "<p class='redfont'>no checkboxes are selected</p>";
echo "<p><a href='javascript:history.back();'>Click to go back</a></p>";
}
This one is more readable I think:
$boxes = ['box1', 'box2', 'box3'];
$checked = [];
foreach($boxes as $box){
if(isset($_POST[$box])){
$checked[] = $box;
}
}
if(count($checked) == 0){
// no boxes checked
echo "<p class='redfont'>no checkboxes are selected</p>";
echo "<p><a href='javascript:history.back();'>Click to go back</a></p>";
}else{
// at least one box is checked, you can do another foreach statement
with the $checked variable to do stuff with the checked ones
}
To do this for ANY number of checkboxes (Webistes are bound to expand), I assume all of you checkboxes are of the form box**i** :
if( strpos( implode(',' , array_keys($test)) , 'box' ) !== FALSE )
I use this function in JQuery:
jQuery.validation = function(){
var verif = 0;
$(':checkbox[id=list_exp]').each(function() {
if(this.checked == true){
verif++
}
});
if(verif == 0){
alert("no checkboxes are selected");
return false;
}
}

Magento how to add a required field to the shipping method?

I have added a dropdown box in the template file of my shipping method. Now I want to make it a required field. I have tried so many ways. But didn't worked. Any help will be appreciated.
Below is the template file.
<?php
$_code=$this->getMethodCode();
$carrier = $this->getMethodInstance();
$pickupData = $this->getQuote()->getPickupData();
$_rate = $this->getRate();
if(!isset($pickupData['store']))
{
$pickupData['store'] = -1;
}
if(!isset($pickupData['name']))
{
$pickupData['name'] = '';
}
?>
<ul class="form-list" id="shipping_form_<?php echo $_rate->getCode() ?>" style="display:none;">
<li>
<label for="shipping_pickup[store]" class="required"><em>*</em><?php echo $this->__('Choose Store Location:') ?></label>
<span class="input-box" style="float:left ;">
<select class="required-entry" name="shipping_pickup[store]" id="shipping_pickup[store]">
<option value='0'><?php echo $this->__('Select Store..');?></option>
<?php
$collection = $this->getAllLocations();
foreach($collection as $coll)
{
$data = $coll->getData();
?>
<option value='<?php echo $data['location_id']; ?>'><?php echo $this->__($data['location_name']);?></option>
<?php
}
?>
</select>
</span>
</li>
</ul>
This code is always going to work because it is always going be set in the $_POST array which seems to turn into an array called $pickupData
You will need to alter your code to make sure that $pickupData['store'] is not a zero
//make sure this variable is available in the array to avoid errors
//check to make sure variable is not a zero still
if(isset($pickupData['store']) && $pickupData['store'] == 0){
$pickupData['store'] = -1;
}
In opcheckout.js edit the line 663 to 763.
validate: function() {
var methods = document.getElementsByName('shipping_method');
if (methods.length==0) {
alert(Translator.translate('Your order cannot be completed at this time as there is no shipping methods available for it. Please make necessary changes in your shipping address.').stripTags());
return false;
}
if(!this.validator.validate()) {
return false;
}
for (var i=0; i<methods.length; i++) {
if (methods[i].checked) {
var methodName = methods[i].value;
if(methodName == 'pickup_pickup')
{
var locationOpt = document.getElementById('shipping_pickup[store]');
var selectedOpt = locationOpt.options[locationOpt.selectedIndex].text;
if(selectedOpt == 'Select Store..')
{
alert(Translator.translate('Please specify a location.').stripTags());
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
}
alert(Translator.translate('Please specify shipping method.').stripTags());
return false;
},

Categories