codeigniter input fields with same name - codeigniter

sorry if its a stupid question but i need a bit of a help.
I made a signup form, and i would like to people select 2 phone numbers.
If i select hungarian a hungarian phone type input slides down, if he selects ukraine than an ukraine phone input type slides down.
here is the html
<input type='text' name='telefon' id='magyar' class='input_title' value='<?php echo set_value('telefon'); ?>' />
<input type='text' name='telefon' id='ukran' class='input_title' value='<?php echo set_value('telefon'); ?>' />
<div class='hiba'><?php echo form_error('telefon'); ?></div>
magyar = hingarian
ukran = ukraine
telefon = phone
telo_tipus = phoe type
my problem is the validation, if i select hungarian and fill it out it says i need to add a phone number but if i select ukraine its ok
here is the validation
$this->form_validation->set_rules('telefon', 'Telefon', 'required|callback_hasznalt_telefon');
could please someone point out what im missing with the validation?
i tried with na=telefon[] but in that case the validation wont work
the callback only validates if the phone is taken thats ok but here it is
function hasznalt_telefon()
{
$telefon = trim($this->input->post('telefon'));
$query = $this->db->query(' SELECT telefon FROM felhasznalok WHERE telefon = "'.$telefon.'" ');
if($query->num_rows() > 0)
{
$this->form_validation->set_message('hasznalt_telefon', 'Ez a telefonszám már használatban van ');
return false;
}
else
{
return true;
}
}

Your codeigniter code is fine, but your html/javascript is what needs to change.
Instead of having two input fields with the same name (which the php script will only read the last one, btw), you should make a select field that changes what input type slides down from the 'telefon' field.
I'm not sure what javascript you are using, but in jquery you can have the the input field event bound on selection from the select field.
If you need more specific guidance for this, let me know and I'll edit my answer.
<select id="telefon_type">
<option>Telefon Type</option>
<option value="magyar">Magyar</option>
<option value="ukran">Ukran</option>
</select>
<input type="text" name="telefon" id="telefon" disabled />
$("#telefon_type").bind("change", function() {
$("#telefon").removeAttr("disabled");
if ($(this).val() == "magyar") {
$("#telefon").bind("focus",function() {
// onfocus event for #telefon for magyar
});
} else if ($(this).val() == "ukran") {
$("#telefon").bind("focus",function() {
// onfocus event for #telefon for ukran
});
} else {
$("#telefon").attr("disabled","disabled");
$("#telefon").val('');
}
});
Please note: I have not tested this code. The general idea behind it is that the filter you are running for the telefon field changes based on your selection of the select field.

Your question has beeen already answered. Phil Sturgeon is working on that issue, so you can try to use the development branch of CI from github.

If you want only ONE telephon number (depends on nationality) I think it would be simplier when you use only ONE input field and write its value to database (or do anything what you want). Or is there any reason to use two input fields?

Related

Laravel 8 & Yajra Datatables complicated search

I have a datatable that fetches orders and is working and displaying properly. I show the orders to the users that initiated them. Then they can only search on their owns orders. Now I need to display a message to a user, if an order was found but it was initiated by another user, instead of displaying an empty result in the datatable. This will happen after typing in the search box and not when loading the datatable in the beggining. The problem is that the query already filters the results by user id so I cannot change it during manual search.
I can display code if needed but the function is quite big and I don't really need code but the logic/way of doing that.
Do you have any suggestions on how I could accomplish this?
Well, maybe not the best way to do it but that's how I solved it:
In the controller, I check for the search field, and run a query on the relationship but only on the orders that have different seller than the logged in user:
$otherSeller = "";
if(!empty($request->search['value']))
{
$ordersOtherSeller = Order::where('status_id', "!=", 3)->where('seller_id', '!=', $loggedUser->id)->whereHas('user', function ($q) use ($request){
$q->searchFullName($request->search['value']);
})->first();
if($ordersOtherSeller != NULL && $ordersOtherSeller->count() > 0)
$otherSeller = $ordersOtherSeller->user->full_name . ' ' . $ordersOtherSeller->seller->full_name;
}
And I set a custom variable with the table's json:
...
->with('otherSeller', $otherSeller)
->make(true);
Then on the datatable jquery drawCallBack, I check for a populated string that guarantees that the result is not returned by a query from the current user:
fnDrawCallback: function( oSettings ) {
var api = this.api();
if(api.ajax.json().otherSeller != "")
{
$('#alert-info span').text(api.ajax.json().otherSeller);
$('#alert-info').show();
}
else
$('#alert-info').hide();
},
And last is the toggling of the materialert element with updated text:
<div id="alert-info" class="materialert info" style="display: none;">
<i class="material-icons">info</i> <span></span>
<button type="button" class="close-alert">×</button>
</div>

Keeping select values across pages

I have a couple of routes
Route::get('/route_one', 'IndexController#index');
Route::get('/route_two', 'IndexController#index');
They call the same controller function because these pages need the same array of data. This function is as follows
public function index()
{
$user = Auth::user();
if( $user ) {
$fileData = $this->fillArrayWithFileNodes(new DirectoryIterator( public_path(). '/images'));
$currentPath= Route::getFacadeRoot()->current()->uri();
if(!empty($fileData)) {
return view('index', compact('fileData', 'currentPath'));
}
} else {
return view('auth.login');
}
}
Now the index view is pretty straight forward, but it does has this part
#if($currentPath == 'route_one')
#include('layouts.searchbarRouteOne')
#endif
#if($currentPath == 'route_two')
#include('layouts.searchbarRouteTwo')
#endif
So depending on what route is called, a different sidebar is displayed. Now the sidebars essentially contain a load of select inputs like the following
<div class="col-lg-3">
<div class="form-group">
<label>Year</label>
<select id="year" class="form-control">
<option value=""></option>
#foreach($fileData["route_one"] as $year => $products)
<option value="{{ $year }}">{{ $year }}</option>
#endforeach
</select>
</div>
</div>
Both sidebars have different selects. When select options are selected, an ajax call is made to display an image. This all works fine.
This is my problem. I have a link to get to route_one or route_two. As the page refreshes when the link is clicked, the selects are at their default state. What I would like to do somehow is keep the last state of the select inputs. I am not storing this data within a database which may be an issue?
Furthermore, route_two relies on the select options from route_one. So when route_two is selected, I need to pass it route_ones options.
What would be the best way to achieve what I am after?
Thanks
Think what you are trying to accomplish here: remember the old input values.
You could send the form when clicking the link and flash the data in your controller or use JavaScript saving input values to the browser's storage.
Simple example using plain JavaScript
// Get all select-elements
var inputs = document.querySelectorAll('select');
// Loop through them
for (var i = 0; i < inputs.length; i++) {
// Set the old input value
inputs[i].value = localStorage.getItem(inputs[i].name);
// Start listening changes
inputs[i].addEventListener('change', store);
}
// The function to call when the value has changed
function store(event) {
// Set the new value to the browser's storage
localStorage.setItem(event.target.name, event.target.value);
}
In that example your form elements are required to have unique name attributes. Of course it can be switched out using e.g. id attribute.

Forms fields filtering issue in Magento 1.8.1.0

In account form fields or user registration page form fields for First name and Last name, when i place the following JS code :
<script>alert("Here")</script>
It is saved and is run on page load. This is very strange, because i checked the template files and values are escaped as below:
<?php echo $this->escapeHtml($this->getObject()->getFirstname()) ?>
I have also confirmed if i am in correct template file by changing the label of field.
I have read the following questions and tried to used them but did not worked for me.
https://magento.stackexchange.com/questions/569/how-to-escape-output-data
https://magento.stackexchange.com/questions/8179/is-there-a-generic-way-i-can-apply-input-filtering-to-magento-form-processing
Regarding to the observer method, it works, but when i try to login to magento admin, i cant, but when iremove the observer, i can login again.
Check the two attached screenshots.
Kindly help me with this issue.
Thank you
I have created an observer. For login forms at admin and front end (and may be some other forms), the form fields for user name and password are in array format like below:
<input type="text" id="username" name="login[username]" value="" class="required-entry input-text">
<input type="password" id="login" name="login[password]" class="required-entry input-text" value="">
TO fix this issue, i have modified the code in observer as below:
public function sanitizeParams($observer)
{
if(!Mage::app()->getStore()->isAdmin())
{
$post = Mage::app()->getRequest()->getParams();
foreach($post as $pkey => $pvalue)
{
if(is_array($post[$pkey]))
{
foreach($post[$pkey] as $key => $value)
{
$post[$pkey][$key] = filter_var($value, FILTER_SANITIZE_SPECIAL_CHARS);
}
}
else
{
$post[$pkey] = filter_var($pvalue, FILTER_SANITIZE_SPECIAL_CHARS);
}
}
$_POST = $post;
}
}
And it fixed my problem, and is working fine now.
Thanks.

Load in div based on choice in selectbox

I have an box in Page1 with some different alternatives and below this a div where I want to load in content (different divs) from an external page (Page2) based on choosen alternative.
Page1
<select id="choose_alternative">
<option> Books </option>
<option> Paper </option>
</select>
Page2
<div id="Book_div">Content</div>
<div id="Paper_div">Content</div>
I found THIS POST and tried figure out how to use the code and ended up with this:
$("#choose_alternative").change(function(){
$("#show_alternative").load( $(Page2.html #Book_div).val() );
$("#show_alternative").load( $(Page2.html #Paper_div).val() );
});
Does anybody know what I have done wrong?
Thanks.
If I understand your question right, what you want to do is according to the selection load the div. check the code below.
$("#choose_alternative").change(function(){
var choose_alternative = $("#choose_alternative option:selected").text();
if(choose_alternative == 'Books'){
$("#show_alternative").load('Page2.html #Book_div');
}
else if(choose_alternative == 'Paper'){
$("#show_alternative").load('Page2.html #Paper_div');
}
});
else you can just load the content right away
$("#choose_alternative").change(function(){
$("#show_alternative").load("Page2.html #Book_div");
$("#show_alternative").load("Page2.html #Paper_div");
});
Read more
Here is the relevant part of the documentation:
http://api.jquery.com/load/#loading-page-fragments
It says to do it like this:
$("#choose_alternative").change(function(){
$("#show_alternative").load("Page2.html #Book_div");
$("#show_alternative").load("Page2.html #Paper_div");
});
Ok I dont get it to work so something is wrong. Its strange becuse there is other similar scripts on the same page that works great.
$(document).ready(function (){
$('#choose_alternative').change(function(){
$show_alternative = $('#show_alternative');
var selectedElement = $('#choose_alternative :selected');
switch($(selectedElement).val())
{
case " Books ":
$show_alternative.load('Page2.html #Book_div');
break;
case " Paper ":
$show_alternative.load('Page2.html #Paper_div');
break;
default:
$show_alternative.html('No option selected.');
break;
}
}
}
Never forget the $(document).ready( function(){...}) part; it is what renders your code so it can be triggered.

change multiple hidden input values using a drop-down box and jquery

Hey, I'm having some trouble with this problem, and I don't even know where to start.
I'm using foxycart for an ecommerce website I'm building for my girlfriend, so sending values to the "cart" is limited to the input names foxycart is looking for.
IE; name, price, product_sku.
I have a tiny CMS backend that allows you to add different sizes, sku's for those sizes and a different price for that size.
So, being that I'm using foxycart, I need hidden inputs to send the values to the cart.
<input type="hidden" name="name" value="Test" />
<input type="hidden" id="price" name="price" value="19.99" />
<input type="hidden" id="product_sku" name="product_sku" value="sku3445" />
<input type="hidden" id="product_id" name="product_id" value="123" />
This works good. sends the name, price and sku to the cart.
I've made a drop down box that lists the different sizes/prices related to that product. I've set it up so that selecting a different size changes the price:
<select id="single" name="options" />
<option name="option_price" value="19.99">Default - $19.99</option>
<option name="option_price" value="18.99">Test Size: 18.99</option>
</select>
function displayVals() {
var singleValues = $("#single").val();
("#item_price").html(singleValues);
$("#price").val(singleValues);
}
$("select").change(displayVals);
displayVals();
This works too, send the price selected to a div and the hidden price input(so you can see the new purchase price) and to the cart(so the cart is showing the price of the product you want to purchase)
And now for the question:
How do I set this up so that selecting a different size/price will change the hidden inputs so that the product_sku, and size name are updated along with the price?
I'm thinking I have to use some Jquery.ajax() call, but have no idea...
Would this work?:
Jquery:
$(document).ready(function(){
$("form#get_stuff").change(function() {
var product_id= $('#product_id').attr('value');
$.ajax({
type: "POST",
url: get_stuff.php,
data: "product_id="+product_id,
success: function(data){
$('#product_inputs').html(data);
}
});
return false;
});
});
the 'data' being:
from the php page?
This is my first foray into Jquery ajax, so I really have no idea.
Edit:
Sorry, I just read this over and it's kind of confusing....
Here is the workflow I'm trying to accomplish:
Page loads:
using php, echo product name, price, sku. (This is the default)
Drop-box change:
using jquery, dynamically change the hidden inputs with new information based off the product_id, and the size selected from the drop-box (Update 4 hidden inputs based off the value from one value from a select menu)
Instead of using AJAX when the select box changes, you can also load the SKU and product ID when the page loads and add them as data on the option tags. One way to do this is to add them as classes like so:
<select id="single" name="options">
<option name="option_price" class="sku3445 id123" value="19.99">Default - $19.99</option>
<option name="option_price" class="sku3554 id321" value="18.99">Test Size: 18.99</option>
</select>
Then using a little RegEx you can extract these values from the selected option in your change() function and update the hidden inputs accordingly:
function displayVals() {
var $single = $('#single'),
singleValues = $single.val(),
singleClasses = $single.find('option:selected').attr('class'),
singleSKU = singleClasses.match(/\bsku\d+\b/)[0],
singleID = singleClasses.match(/\bid\d+\b/)[0].replace('id','');
$("#item_price").html(singleValues);
$("#price").val(singleValues);
$('#product_sku').val(singleSKU);
$('#product_id').val(singleID);
}
$("select").change(displayVals);
displayVals();
Here is a working example →
Using Ajax is the way to go. When the dropdown value changes, you will want to trigger the Ajax call to a PHP method, which I assume would query a backend database for necessary information using the dropdown value as a parameter, then return that information to populate the hidden fileds. All these steps should happen in your Ajax call.

Resources