Page Redirection Issue- Cakephp3 using Jquery - php

I have a page where I have kept a dropdown where user can switch over languages. I am using theme so I have kept this dropdown on login.ctp page and posting form values to controller's method using Jquery. I have two form elements within login.ctp I don't know but my dropdown form element is not reaching to particular controller instead it is redirecting to login controller.
login.ctp
<form id="locale_form" method="post" action="changeLanguage">
<div id="language" class="language" align="right">
Please Select Language
<select id="languageselect" name="languageselect">
<option name="select" value="lang">Select A Language</option>
<option name="en_EN" value="en_EN">English</option>
<option name="de_DE" value="de_DE">German</option>
</select>
</div>
</form>
<div id="login-page">
<div class="container">
<!-- <form class="form-login"> -->
<?= $this->Flash->render('auth') ?>
<?= $this->Form->create('',['class'=> 'form-login','id'=>'login_form']) ?>
-----------------Login Form Code----------------
Using Jquery onchange I am making switch over languages.
<script type="text/javascript">
$("select").change(function(){
$("#locale_form").submit();
});
</script>
My Userscontroller withing that method:
use Cake\I18n\I18n;
class UsersController extends AppController
{
public function changeLanguage()
{
/*I18n::locale('de_DE');
echo "reached here";*/
print_r($this->request->data());die();
if ($this->request->is('post'))
{
$lang = $this->request->data('languageselect');
$this->request->session()->write('locale', $lang);
I18n::locale($lang);
return $this->redirect(['action' => 'login']);
}
}
I don't know why But my form element with id="locale_form" is not reaching to its action element. I am using Cakephp3 and I don't know where I have messed up.

Add this method changeLanguage to allow like below.
$this->Auth->allow('changeLanguage');

Related

PHP/HTML form-action attribute

I have a small problem. I have one form, for search. After submitting this form is can't stay on same page.
My url:
www.localhost/index.php?vehicletype=car
On this url, the car search is visible.
After the submit I get this:
localhost/index.php?body_type=any&fuel_type.....
But I want this,
www.localhost/index.php?vehicletype=car?body_type=any&fuel_type....
I tried $_SERVER["REQUEST_URI"] but nothing. Thanks for the help, and sorry for my English grammar!
EDIT: The code:
<? if($_GET['vehicletype']=='car'){ ?>
<div class="search">
<form method="get" action="">
....
<button class="btn btn-search" type="submit">Search <hr> (658 found)</button>
</form>
</div>
First thing following url is wrong
"www.localhost/index.php?vehicletype=car?body_type=any&fuel_type...."
after "?" it will set all argument in get method you can access that variable using $_GET['variablename']
if you want to be on same page you can action like following way
<form action="#" method="GET">
When someone click on submit button same page will be called with form params.
You can get all form param by using $_GET method.
Method 1.
<?php
if(isset($_GET['vehicletype']) && $_GET['vehicletype'] == 'car')
{
//write business login that you want to show once search action perform by user
}
else
{
?>
Show HTML Form here
<?php } ?>
Method 2.
<?php
if(isset($_GET['vehicletype']) && $_GET['vehicletype'] == 'car')
{
//write business login that you want to show once search action perform by user
}
<form action="#" method="GET">
<input type="text" name="vehicletype" value="<?php if (isset($_GET['vehicletype'])) { echo $_GET['vehicaletype']; } ?>">
</form>
Let me know if you have further query.

Populating select tag from selected items in php

i am totally new on PHP and I have an issue, i hope i can explain my problem exactly.
I have two HTML5 forms where one of the form is being populated by php for loop and inside the same form there is there is a submit button code as follows:
<form class="" name="upgradeChosen" role="form" method="post" action='index.php?action=upgradeChosen'>
<div class="col-md-5">
<select name="upgradeSelected" id="ID_UPGRADE_SELECTED">
<?
foreach ($arrayUpgrade as $value) {
$pro = $value->getnewProduct();
echo '<option value="'.$value->getId().'">'.$pro->getName()." "."===>". $pro->getPrice(Currency::getCurrentCurrency()).'</option>';
}
?>
</select>
</div>
<button style="margin-top: 40px" id="ID_CREATE_ESTIMATE" type="submit" class="btn btn-default"><i class="icon-rocket"></i>Create estimate</button>
</form>
Inside the another PHP file there is a function which is called function upgradeChosen and it parses the selected data from select tag. at the second form i would like to generate a list and populate the selected items from first listbox or select tag. Is this possible ?
Here is my second form code as follows:
<form class ="" name ="upgradeEstimate" role="form" method="post" action='index.php?action=Frontend_upgradeEstimate'>
</form>
and the function which is called upgradeChosen code as follows:
public function upgradeChosen (){
$idUpgradeLine = $this->getRequestAttribute("upgradeSelected");
$upgrade = new UpgradeRates($idUpgradeLine);
$product = $upgrade->getNewProduct();
}
please help me to achieve this if there is a way of doing it only with php i will be happy i do not want to use Jquery, JavaScript or AJAX at first stage if there is a way how to do it with php only.
For this you must use javascript.
You can use onchange event of your select for call a javascript function and reload your page with a post param. Then write your second select in php.
I think there aren't any other method.
Fabio Del Rosso.

redirect with php buttons in existing item-post.php

<div class="row">
<label for="AdsType"><?php _e('Ads Type', 'modern'); ?></label>
<select id="AdsType" name="AdsType" >
<option>Standard</option>
<option>Premium</option>
</select>
</div>
once user selects Standard button I want it to be redirected to www.site.com/complete.php
once user selects Premium button I want it to be redirected to www.site.com/pay.php
What is the extra coding I add in my existing php file before:
<button type="submit"><?php _e('Publish', 'modern'); ?></button>
You can change action url which is in the form using javascript
// call to change_action() function onChange event
<select id="AdsType" name="AdsType" onChange="change_action();" >
<option value="Standard">Standard</option>
<option value="Premium">Premium</option>
</select>
// change action url
<script language="javascript">
function change_action()
{
if(document.getElementById("AdsType").value=="Standard")
{
form_name.action="www.site.com/complete.php";
}
else
{
form_name.action="www.site.com/pay.php";
}
}
</script>

Jquery not submitting my form

Hi I want to submit my form using JQuery but I don't know why it won't work for me. here's the code:
$("#search_result_fake_div2").live("click", function () {
$("#search_result_present_list2").show("fast");
$('body').one('click',function() {
$("#search_result_present_list2").hide();
});
event.stopPropagation();
});
$(".search_result_list_item2").live("click", function () {
$("#search_result_fake2").val($(this).html());
$("#getattendees").submit();
$("#search_result_present_list2").hide();
});
and my HTML code:
<div>
<div id="search_result_fake_container">
<div id="search_result_fake_div2"></div>
<form method="GET" id="getattendees">
<input type="text" id="search_result_fake2" value="<?php if(!empty($city)){echo $city;} else{echo "Select Event";}?>" name="event_name">
</form>
</div>
<div id="search_result_present_list2">
<?php foreach ($events as $events1): ?>
<div class="search_result_list_item2" id="search_result_item_12" style="text-align:center;"><?php echo $events1['event_name']; ?></div>
<?php endforeach ?>
</div>
</div>
This is a drop down menu using div which came from my other question. One thing that I want to happen is for the form to submit once I've clicked a content from the drop down selection. I tried using onchange="javascript: document.getattendees.submit();" on my input box but nothing happens so I'm thinking of using jQuery instead but still the problem persists. I'm just a newbie on the jQuery and in fact I'm not that well versed on this one.
try this:
$('form#getattendees').trigger('submit');

Jquery form submit with PHP

I need help with what I thought would be a simple form submit problem with Jquery, but am running into issues.
I have a form, #listing_step_1, in which the user first selects a category from a dropdown. When a category has been selected, if the user clicks "next", the subcategory dropdown is revealed. When both a category and subcategory is selected, clicking next will take them to step 2 of the process using a redirect. On submission, the form is reloading the same page using $_SERVER['PHP_SELF'].
I want to autosubmit the form so clicking on next is not required to reveal the subcategories. I'm trying to do this on an onchange event using JQuery, but the following code does not work.
$('#item_category').change(function(){
$('#listing_step_1').submit();
});
I would appreciate any help with this as I am new to form submission using JQuery and AJAX. Manually, it's working fine - i.e., pressing the page button and with the PHP page refresh. I thought by simply submitting the form in the onchange instance it would reload and execute the required actions, but this does not appear to be the case.
Form code:
<form method="post" name="listing_step_1" id="listing_step_1" action="<?php $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data" <?php if($errormessage != ""){ echo ' style="margin-top: 30px"';}?>>
<div class="formField fullwidth">
<label for="item_category">Category *</label>
<select id="item_category" name="item_category">
<option value="0">Select category...</option>
<?php $cd = new CategoryListing("<option_credits>", "</option>"); ?>
</select>
</div>
<div class="formField fullwidth">
<label for="item_subcategory">Subcategory*</label>
<?php if($dropdownlist != "") { ?>
<select id="item_subcategory" name="item_subcategory">
<?php echo $dropdownlist; ?>
</select>
<?php } else { ?>
<select id="item_subcategory" name="item_subcategory" disabled="true">
<option value="0">Select subcategory...</option>
</select>
<?php } ?>
</div>
<input type="submit" value="Next" id="submit" name="submit" style="width: 100px;" />
</form>
Form Submission:
if(isset($_POST['submit'])){
if($_POST['item_category'] != 0){
$dropdownlist = CategoryListing::getSubCategoryDropdown($_POST['item_category']);
}
else {
$errormessage = "Please select a category";
}
}
Jquery:
$(document).ready(function(){
$('#item_category').change(function(){
this.form.submit();
});
});
Try this instead:
$(function() {
$('#item_category').change(function(){
this.form.submit();
});
});
in your description, you say:
I have a form, #listing_stage_1, in
which the user first selects a c...
then in code you say:
$('#item_category').change(function(){
$('#listing_step_1').submit();
});
I imagine you are just using the wrong selector... change your code to this:
$('#item_category').change(function(){
$('#listing_stage_1').submit();
});

Categories