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;
},
Related
I have a little problem here. I have a list, which is:
<form action="display.php" method="post">
<select name="holiday">
<option value="month">Kiekvieno mėnesio skaičiavimas</option>
<option value="day">Kiekvienos dienos skaičiavimas</option>
</select>
<br> <br>
<input type="submit" name="select" value="Pasirinkti" />
</form>
What I need to do is that when the user selects value ="month", php code would do one action, and when the user selects value ="day", php code would do another action?
My php code looks like this:
<?php
if($_POST['select']){
// Storing selected value in a variable
$selectedValue= $_POST['holiday'];
}
else if ($selectedValue == $_POST['month']) {
$todaysDate = new DateTime();
while ($employee = $select->fetch()){
$employmentDateValue = new DateTime($employee['employment_date']);
// Comparing employment date with today's date
$differenceBetweenDates = date_diff($employmentDateValue, $todaysDate);
$workMonths = $differenceBetweenDates->y * 12 + $differenceBetweenDates->m;
$holidayDays = $workMonths *4;
echo "<tr>";
echo "<td>".$employee['name']."</td>";
echo "<td>".$employee['surname']."</td>";
echo "<td>".$employee['employment_date']."</td>";
echo "<td>".$workMonths."</td>";
echo "<td>".$holidayDays."</td>";
echo "</tr>";
}
}
else {
echo "Lalalala";
}
?>
I've tried to do so with $_POST['select'], but it doesn't work. Thanks for any help guys
<?php
if($_POST['select']){
// Storing selected value in a variable
$selectedValue= $_POST['holiday'];
if ($selectedValue == 'month') {
}
else if ($selectedValue == 'day') {
}
else{
echo "Lalalala";
}
}
?>
You need to do $_POST['holiday'], so change:
if($_POST['select']){
// Storing selected value in a variable
$selectedValue= $_POST['holiday'];
}
to
if($_POST['holiday']){
// Storing selected value in a variable
$selectedValue = $_POST['holiday'];
}
You also need to change the line:
else if ($selectedValue == $_POST['month']) {
So it's not part of the original if statement:
if ($selectedValue == 'month') {
// your code
}
else {
echo "Lalalala";
}
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>
I found this tutorial on making a shopping cart app and It's not working. I get an error about undefined variable bookFound on line 22 where it says if(!$bookFound). I see maybe why it is not defined, I'm thinking maybe because it was defined in the if statement previously in the code and that is not returning true. Any ways I'm having problems fixing it so if you can make this code work that will be great. The user should be able to click the button and the div should be updated with calculated results.
<?php
session_start();
$booksInfo = $_SESSION['cart'];
if(count($booksInfo) > 0)
{
$bookFound = false;
for($i=0; $i< count($booksInfo); $i++)
{
if($booksInfo[$i]['bookId'] == $_POST['bookId'])
{
$booksInfo[$i]['quantity'] = $_POST['quantity'];
$bookFound = true;
break;
}
}
}
if(!$bookFound) //line 22 where error was found
{
$book = array('bookId' => $_POST['bookId'], 'quantity' => $_POST['quantity']);
array_push($booksInfo, $book);
}
$_SESSION['cart'] = $booksInfo;
$grossTotal = 0;
for($i=0; $i< count($booksInfo); $i++)
{
$aBook = $booksInfo[$i];
$bookName = getBookName($booksInfo[$i]['bookId']);
$bookPrice = getPriceForBook($booksInfo[$i]['bookId']);
$totalPrice = $bookPrice * $booksInfo[$i]['quantity'];
$grossTotal+= $totalPrice;
$str.= '<strong>Name - </strong>'.$bookName;
$str.= '<br/>';
$str.= ' <strong>Copies - </strong>'.$booksInfo[$i]['quantity'];
$str.= '<br/>';
$str.= '<strong>Price - </strong>$'.$bookPrice. ' * ' .$booksInfo[$i]['quantity'].' = $'.$totalPrice;
$str.= '<br/><br/>';
}
$str.= '<strong>Net Amount - </strong>$'.$grossTotal;
echo $str;
function getBookName($id)
{
$objXML = simplexml_load_file('books.xml');
foreach($objXML->book as $book)
{
if($book['id'] == $id)
{
return $book->name;
}
}
return false;
}
function getPriceForBook($id)
{
$objXML = simplexml_load_file('books.xml');
foreach($objXML->book as $book)
{
if($book['id'] == $id)
{
return $book->price;
}
}
return false;
}
?>
Index:
<body>
<div class="cart">
<strong>Your cart</strong>
<p id = "cart">Cart is empty</p>
</div>
<?php
$objXML = simplexml_load_file('books.xml');
foreach($objXML->book as $book)
{
echo '<div>';
echo 'Name - ' . $book->name, '<br />';
echo 'price - $'. $book->price, '<br/>';
?>
Quantity -
<select name="" id="">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
-
<input type="hidden" value = "<?php echo $book['id']; ?>">
<input type="button" value = "Select this book ">
<?php
echo '</div>';
}
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('input:button').click(function(){
$.post('calculate.php',
{
bookId : $(this).prev('input:hidden').val(),
quantity: $(this).prev().prev('select').val()
},
function(data)
{
$('#cart').html(data);
}
)
});
});
</script>
</body>
Try moving the $bookFound = false; statement above the if(count($booksInfo) > 0) statement. If the if(count($booksInfo) > 0){} block is not executed, $bookFound is not initialized for when you get to the if(!$bookFound){} block.
I have a dropdown control which and I would like it to default to a specific option based on the access of the logged in user. For example, the dropdown has 10 options but only administrators have access to view all 10 options. The majority of users only have access to 1 of the options though. The page contents are hidden/displayed based on whether or not the value of the dropdown is null.
Question: Using the example above, if an admin is logged in I need the dropdown to default to "Select an option". This way the page content is hidden. On the other hand, if a user with access to only 1 is logged in, I need it to default to that 1. This way they don't have to select anything and, by default the page content is displayed. How do I go about doing this?
Below is my current code which handles what the dropdown displays based on when a selection is made.
PHP/HTML
// Hide/Show main content div
<?php
if (isset($_GET['src'])) {
$src = $_GET['src'];
} else {
?>
<style>
#divmain { display: none; }
</style>
}
<?php } ?>
// Start of form, header, etc.
<select name="select1" id="select1">
<?php
$sql = getOptions();
$data = makeConnection($sql);
if ($src == null) { // If value is null, default to 'Select an option'
echo "<option selected value=\"\" disabled=\"disabled\">--Select an option--</option>";
while ($row = odbc_fetch_array($db)) {
echo "<option value=\"".$row['content']."\">".$row['content']."</option>";
}
} else { // If value not null keep the selected value selected
while ($row = odbc_fetch_array($db)) {
if ($row['content'] == $src) { $selected = " selected "; }
else { $selected = " "; }
echo "<option value=\"".$row['content']."\" ".$selected.">".$row['content']."</option>";
}
}
?>
</select>
JS
// Pass selected value on change
$('#select1').change(function() {
var sel = $(this).val();
location.href = "page1.php?src=" + sel;
}
SQL
// Hardcoding user for testing purposes, THIS WILL BE CHANGED
function getOptions() {
$results = "SELECT content, userid FROM table WHERE userid = 'username'";
return $results;
}
Any help is much appreciated and please let me know if I'm not clear about anything.
Got some help and have it figured out now. Here is the revised code:
PHP/HTML
// Hide/Show main content div
<?php
$src = null;
if (isset($_GET['src'])) {
$src = $_GET['src'];
} else {
?>
<style>
#divmain { display: none; }
</style>
}
<?php } ?>
// Start of form, header, etc.
<select name="select1" id="select1">
<?php
$sql = getOptions();
$data = makeConnection($sql);
if ($src == null) {
$i = 0;
$content = "";
while ($row = odbc_fetch_array($db)) {
$content .= "<option value=\"".$row['content']."\">".$row['content']."</option>";
$i++;
}
if ($i > 1) {
echo "<option selected value=\"\" disabled=\"disabled\">--Select an option--</option>";
}
echo $content;
if ($i > 1) { $oneopt = 1; }
else { $oneopt = 0; }
} else {
while ($row = odbc_fetch_array($db)) {
if ($row['content'] == $src) { $selected = " selected "; }
else { $selected = " "; }
echo "<option value=\"".$row['content']."\" ".$selected.">".$row['content']."</option>";
}
}
?>
</select>
JS
$('#select1').change(function() {
var sel = $(this).val();
location.href = "page1.php?src=" + sel;
}
<?php
global $optone;
if ($optone == 1) {
echo "$('#select1').trigger('change');";
}
?>
SQL -- Stays the same
#chenasraf really appreciate the help! Hope this can be of some help to someone in the future!
The preselected option is always the first to have a "selected" attribute. Your first disabled one has it first on all cases, so it's always chosen. Remove that and work from there.
On a side note, I think you can manage to make this options part work better. I've taken the liberty of rewriting it for you:
<select name="select1" id="select1">
<?php
$selected = '';
while ($row = obdc_fetch_array($db)) {
$options[] = '<option value="'.$row['content'].'">'.$row['content'].'</option>';
if ($row['content'] == $src)
$selected = count($options) - 1;
}
array_unshift($options, '<option disabled="disabled"', $selected == '' ? ' selected="selected"' : '','>--Select an option--</option>');
foreach ($options as $option) {
echo $option;
}
?>
</select>
I want to validate whether my 3 input's are the same as the value in the array.
<?php
$aylength = 0;
$resultacyear = array();
foreach($academicyear->result() as $ay)
{
$item = array(
'tahunakademik' => $ay->Tahun_Akademik,
'idsemester' => $ay->ID_Semester,
'idlevelyear' => $ay->ID_Level_Year
);
$resultacyear[]= $item;
$aylength++;
}
?>
<script type="text/javascript">
$('#btn_save').click(function() {
<?php $inc = 0; ?>
for(i=0;i<parseInt(<?php echo $aylength?>);i++)
{
if($('#txt_tahunAkademik').val()=="<?php echo $resultacyear[$inc]['tahunakademik'] ?>" && $('#cb_semester').val()=="<?php echo $resultacyear[$inc]['idsemester'] ?>" && $('#cmb_YearLevel').val()=="<?php echo $resultacyear[$inc]['idlevelyear'] ?>")
{
alert ("Data already exists!!");
return false;
break;
}
else
{
<?php $inc++; ?>
return false;
}
}
});
</script>
When i tried, it didn't validate or even checks the value.
Is something wrong with my validation??