codeigniter validating from textbox value(jquery) from array(php) - php

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??

Related

Search filters not functioning on first attempt

I have a search filter based on flags of different colors.
If I will search based on color it is showing result but when I will select white color flag it is showing entire data in my database table. But If I choose any other color apart from white color for the first time it will show result. And if I choose white color flag second time,then it gives the correct result. What might be the reason. I need help
//filter on flag_color
if($this->input->get('flg')){
$arr_flag = explode('-', $this->input->get('flg'));
$str_flag = implode(',',$arr_flag);
if(6 == $str_flag){
$str_flag = 0;
}
$str_condition .= 'AND t.sender_flag_color_id IN('.$str_flag.')';
}
array(
'0'=>array(
'color'=>'White',
'path'=>'whiteflag.png'),
'1'=> array(
'color'=>'Blue',
'path'=>'blueflag.png'),
'2'=>array(
'color'=>'Green',
'path'=>'greenflag.png'),
'3'=>array(
'color'=>'Yellow',
'path'=>'yellowflag.png'),
'4'=>array(
'color'=>'Red',
'path'=>'redflag.png') ,
'5'=>array(
'color'=>'Orange',
'path'=>'orangeflag.png')
);
view
<?php
$arr_params = $this->uri->uri_to_assoc();
$arr_flag = array();
if(isset($arr_params['flg']))
{ ?>
<input type="hidden" id="flag_in_url" name="flag_in_url" value="yes" />
<?php
}
?>
<!-- id="webmenu1" -->
<select class="flag_color" onchange="urgency_select('flag_color')" name="header_flag" id="webmenuflag1">
<?php
$arr_params = $this->uri->uri_to_assoc();
$arr_flag = array();
$int_flag_url = '';
if(isset($arr_params['flg']))
{
$int_flag_url = $arr_params['flg'];
}
if(isset($arr_flag_color))
{
foreach($arr_flag_color as $key=>$flag_color)
{
if($int_flag_url == $key)
{
$flag_selected = 'selected';
}
else
{
$flag_selected = '';
}
?>
<option <?php echo $flag_selected;?> value="<?php echo $key;?>" data-image="<?php echo base_url();?>images/<?php echo $flag_color['path'];?>"></option>
<?php
}
}
?>
js: urgency_select
var flag_color_id = '';
if(select_option == 'flag_color' || $( "#flag_in_url" ).val() == 'yes')
{
if($( ".flag_color" ).val() != undefined)
{
var flag_color_id = $( ".flag_color" ).val();
if(flag_color_id == 0)
flag_color_id = 6;
search_url += 'flg/'+flag_color_id+'/';
}
}
Its because conflict with the value of white color flag...
In the view I changed the code
if($int_flag_url == $key)
{
$flag_selected = 'selected';
}
to
if($int_flag_url == 0 || $int_flag_url == $key)
{
$flag_selected = 'selected';
}

Shopping cart code is not working. getting error for undefined variable

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.

Loop through an array of checkboxes and insert values

First time posting, hope all goes well :)
Been working on this for a while but basically I'm doing something like this: http://drupal.org/project/fancycheckboxes
I need to set a value of yes/no for each checkbox that I'm looping through and right now it works but is changing the value of all when I change one.
My foreach loop:
<?php $object = new stdClass(); $i = 0; ?>
<?php foreach( $filters as $key => $value ) {
?>
<tr>
<td><label for="show_filter">Show <?php echo $object->$key = $value['filter']; ?> Filter</label></td>
<td><input type="checkbox" class="show_filter" <?php //if ( $row['active_filter'] === 'yes' ) { ?>checked="checked"<?php //} ?> name="dp_show_filter" value="<?php echo $object->$key = $value['active_filter']; ?>"/><?php echo $object->$key = $value['active_filter']; ?></td>
<td><a class="button-primary" href="?page=profolio_theme&del=<?php echo $row['id'];?>">Delete</a></td>
</tr>
<?php
$i++;
}
and my jQuery:
jQuery(document).ready(function(e) {
var $ = jQuery.noConflict();
$i = 0;
customCheckbox = $('.dp_wrap input[type="checkbox"]');
showFilter = $('.dp_wrap input[name="dp_show_filter"]');
values = [];
return customCheckbox.each(function() {
// the element
var el = this;
// Hide checkbox
$(this).hide();
// Replace element
var rep = $('<span></span>').addClass('dp-checkbox').insertAfter(this);
// default state
if( $(showFilter).val() === 'yes'){
$(showFilter).prop('checked', true);
$(rep).removeClass('off').addClass('on');
} else {
$(showFilter).prop('checked', false);
$(rep).removeClass('on').addClass('off');
}
if($(this).is(':checked') ) {
$(rep).addClass('on');
} else {
$(rep).addClass('off');
}
// Click event
$(rep).click(function(e) {
e.preventDefault();
if( $(el).is(':checked') ) {
values.push($(showFilter).val('no'), ++$i);
$(el).prop('checked', false);
$(rep).removeClass('on').addClass('off');
} else {
values.push($(showFilter).val('yes'), ++$i);
$(el).prop('checked', true);
$(rep).removeClass('off').addClass('on');
}
});
});
});
Currenty my values are posting to a database as yes/no.
Any help is greatly appreciated and I hope my first post is an acceptable one.
It is changing all of the values as it looks as though you are giving the same name to every checkbox created on your page.
You can change your PHP code to something more like this:
<?php $object = new stdClass(); $i = 0; ?>
<?php foreach( $filters as $key => $value ) {
?>
<tr>
<td><label for="show_filter">Show <?php echo $object->$key = $value['filter']; ?> Filter</label></td>
<td><input type="checkbox" class="show_filter" <?php //if ( $row['active_filter'] === 'yes' ) { ?>checked="checked"<?php //} ?> name="dp_show_filter_<?php echo $row['id'] ?>" value="<?php echo $object->$key = $value['active_filter']; ?>"/><?php echo $object->$key = $value['active_filter']; ?></td>
<td><a class="button-primary" href="?page=profolio_theme&del=<?php echo $row['id'];?>">Delete</a></td>
</tr>
<?php
$i++;
}
Then change your default state to this and remove the if statement from underneath it:
// default state
if( $(this).val() === 'yes'){
$(this).prop('checked', true);
$(rep).removeClass('off').addClass('on');
} else {
$(this).prop('checked', false);
$(rep).removeClass('on').addClass('off');
}
I am pretty sure that will fix your problem as you described it. It is hard to tell without a more complete example of your code.
You iterate through your customCheckbox array with an each, but inside your loop, you access $(showFilter), which is a global array.
Did you mean $(showFilter[i]) ?
The each callback receives i (the index in the loop) as a parameter :
return customCheckbox.each(function(i) {
...
if( $(showFilter[i]).val() === 'yes'){ ...

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;
},

how to return javascript in a php class file ?

I want the following javascript code to be echoed in a PHP class file. How I can achieve that?
echo '<script language="JavaScript" type="text/javascript">'."\n";
echo 'var parDoc = window.parent.document;';
if ($result == 'OK') {
echo 'parDoc.getElementById("picture_error").innerHTML = "";';
}
else {
echo "parDoc.getElementById('picture_error').innerHTML = '".$result_msg."';";
}
if($filename != '') {
echo "parDoc.getElementById('picture_preview').innerHTML = '<img src=\'$preview_url$filename\' id=\'preview_picture_tag\' name=\'preview_picture_tag\' width=\'60\' />';";
}
echo "\n".'</script>';
exit(); // do not go futher
<?php
?>
<script language="JavaScript" type="text/javascript">
var parDoc = window.parent.document;
<?php
if ($result == 'OK') {
?>
parDoc.getElementById("picture_error").innerHTML = "";
<?php
}
else {
?>
parDoc.getElementById("picture_error").innerHTML = "<?php echo $result_msg;?>";
<?php
}
if($filename != '') {
?>
parDoc.getElementById("picture_preview").innerHTML = '<img
src=\'<?php echo $preview_url.$filename;?>\'
id=\'preview_picture_tag\'
name=\'preview_picture_tag\'
width=\'60\' />';
<?php
}
?>
</script>
exit(); // do not go futher
Surround it with the PHP code sequence relevant to your installation: <? ?> or <?php ?> depending on your configuration.

Categories