Redirect selected option on submit laravel - php

How can I redirect my selected option when I click submit?
I am trying to make a modal Sign Up that is located in the header for different users. When the user choose one, they will then be redirected to specific sign up page.
This is from my header.blade.php
<div id="modal1" class="modal">
<div class="modal-content">
<h4>Choose Roles/Plan</h4>
<div class="input-field col s12">
<select id="select-action" name = "action">
<option value="" disabled selected>Choose your option</option>
<option value="basicsignup">Basic</option>
<option value="advancedsignup">Advanced</option>
<option value="teamsignup">Team</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="submit">Submit</button>
</div>
</div>
routes.php
Route::get('/basicsignup', function(){
return view('actions.basicsignup');
})->name('basicsignup');
Route::get('/advancedsignup', function(){
return view('actions.advancedsignup');
})->name('advancedsignup');
Route::get('/teamsignup', function(){
return view('actions.teamsignup');
})->name('teamsignup');
Edit: Original examples were animals, flowers, cars.. edited it for clarity at least.

You can simply use JavaScript onsubmit function to call a JS function than get your selected value and then you can submit a form with your customised url.
Example
<form method="post" id="myForm" onsubmit="return submitForm()">
<select id="select-action" name="action">
<option value="" disabled selected>Choose your option</option>
<option value ="animals">Animals</option>
<option value ="flowers">Flowers</option>
<option value ="cars">Cars</option>
</select>
<button type="submit">Submit</button>
</form>
<script type="text/javascript">
function submitForm() {
var selectedOption = $('#select-action').val();
var url = "";
if(selectedOption == 'animals') {
url = '{{ route('your/route')}}';
} else if (selectedOption == 'flowers') {
url = '{{ route('your/route')}}';
}
.
.
.
$('#myForm').attr('action', url);
$('form#myForm').submit();
return false;
}
</script>

Related

How to display an alert message if the value is blank in the form

I have a form (bootstrap) in the script and need it if the user does not select the language and the value "" is blank to display the alert message and that he had to choose a language to proceed on the next step.
(as you can see the empty value in the form: value" " represent "Select Language")
I am using that form for google widget tool for translation, and when the visitor dont click anything and proceed to save file into the database , in the database I get value: "Select language" , I need that they pick one of the languages from the dropdown menu.
This is the form code:
<div class="form-group">
<label for="recipient-name" class="col-form-label">Language:</label>
<input type="text" class="form-control langinput" id="country" value="" readonly>
<select class="form-control langselect" aria-label="Language Translate Widget" id="country" style="display: none">
<option value="">Select Language</option>
<option value="English">English</option>
<option value="Afrikaans">Afrikaans</option>
<option value="Albanian">Albanian</option>
<option value="Amharic">Amharic</option>
<option value="Arabic">Arabic</option>
here is the button code for saving files into the database:
<button style="margin-top: -5%" background-color="#357ae8" type="button" class="btn btn-success" data-toggle="modal" data-target="#exampleModal" data-whatever="#getbootstrap" onclick="lang1()">Save to Database</button>
lang1 Function:
var langPath = $("select.goog-te-combo option:selected").text();
/*document.getElementById("country").options[document.getElementById("country").selectedIndex].text = langPath;
document.getElementById("country").options[document.getElementById("country").selectedIndex].value = langPath;*/
document.getElementById("country").value = langPath;
//$(".langinput").value = langPath;
//$('input[type="text"][class="langinput"]').prop("value", "langPath");
//$('.langinput').text(langPath);
$('.langinput').attr('value', langPath);
//$(".langinput").display = "inline";
}
/*function stoppedTyping(){
if($('#subtitle').val().length > 1 && $('#country').val().length >0 ) {
document.getElementById('submit_button').disabled = false;
} else {
document.getElementById('submit_button').disabled = true;
}
}
function stoppedSelect(){
if($('#subtitle').val().length > 1 && $('#country').val().length >0 ) {
document.getElementById('submit_button').disabled = false;
} else {
document.getElementById('submit_button').disabled = true;
}
}*/
With bootstrap just add required to the select:
<select class="form-control langselect" aria-label="Language Translate Widget" id="country" style="display: none" required>
<option value="">Select Language</option>
<option value="English">English</option>
<option value="Afrikaans">Afrikaans</option>
<option value="Albanian">Albanian</option>
<option value="Amharic">Amharic</option>
<option value="Arabic">Arabic</option>
</select>
Edit
Change the buttons onclick to onsubmit:
<button style="margin-top: -5%" background-color="#357ae8" type="button" class="btn btn-success" data-toggle="modal" data-target="#exampleModal" data-whatever="#getbootstrap" onsubmit="lang1()">Save to Database</button>
Just recommending to rewrite this: <option value="">Select Language</option>
to this: <option selected disabled>Select Language</option>
When user clicks on the next button check if the select's value length is 0 and return it.

php if dropdown option value 0 then hide div

as I am not experienced with php, I need some help solving my issue.
I pasted the snippet of php I believe needs editing.
<div class="selectBox" id="products-variation-container">
<select class="product-variation-dropdown" name="product-variation" id="sel-product-variation">
<option value="0"><?php _e('Select variation', 'wp-woo-leasing'); ?></option>
</select>
</div>
<!-- <input type="submit" name="btn-submit" class="button alt" id="leasingAddItem" value="<?php _e('Add Item', 'wp-woo-leasing'); ?>"
data-value="<?php _e('Add Item', 'wp-woo-leasing'); ?>" />-->
</div>
</div>
<div class="leasing-single-container" >
<div class="leasing-col1" >
<div id="product-description" class="product-description"></div>
<div id="product-price-details" class="product-price-details"></div>
</div>
My issue is that I want my code to be like:
<select class="product-variation-dropdown" name="product-variation" id="sel-product-variation">
<option value="0"><?php _e('Select variation', 'wp-woo-leasing'); ?>
If option value = "0" then hide the following div, upon loading site, since default is value is 0.
<div id="product-description" class="product-description"></div>
I hope you understand my question, and I hope that you can guide me to approaching this issue, thanks.
You can use jQuery to show and hide div on basis of select value.
Wordpress have already included jquery file, so you no need to add it.
(https://developer.wordpress.org/reference/functions/wp_enqueue_script/).
Here is code you can use:
<div class="selectBox" id="products-variation-container">
<select class="product-variation-dropdown" name="product-variation" id="sel-product-variation" onchange="getval();">
<option value="0">Select Option</option>
<option value="1">Select Option1</option>
</select>
</div>
<div class="leasing-single-container" >
<div class="leasing-col1" >
<div id="product-description" class="product-description">dgsdgsdgsdgsdgsd</div>
<div id="product-price-details" class="product-price-details"></div>
</div>
</div>
<script>
jQuery(document).ready(function() {
var sel = jQuery('select[name=product-variation]').val();
if(sel == 0) {
jQuery('#product-description').attr('style', 'display:none');
}
jQuery('#sel-product-variation').on('change', function() {
var sel = jQuery('select[name=product-variation]').val();
if(sel == 0) {
jQuery('#product-description').attr('style', 'display:none');
}
else {
jQuery('#product-description').attr('style', 'display:block');
}
});
});
</script>
Let me know incase of any concern for the same.

PHP jQuery Data-Attribute populate dropdown

I have an HREF with various data-attributes (about 8) that come from a PHP query and loaded into a table. When you click the button, it takes the data-attributes via jQuery and sends them to a modal window, where I can use them in a form.
I can open the window and place the attributes into an INPUT field with no problem.
What I would like to do is place the attribute in the first OPTION in a SELECT dropdown.
Here is the button with the attributes:
<tr><td><a href='' class='modAccount'
data-modcode=".$row[partner_code]."
data-modname=\"".$row[partner_name]."\"
data-boiler=\"".$row[boilerplate]."\"
data-surcharges=\"".$row[no_new_surcharges]."\"
data-ccalog=\"".$row[cca_logistics]."\"
data-toggle='modal'>Click</a></td></tr>
I have quite a few data-attributes. I had to eliminate a few for this question.
Here is the jQuery used to retrieve the data-attributes and send them to a modal window, called modifyModal.
Here is the jQuery:
$(function()
{
$('.modAccount').click(function(e)
{
e.preventDefault();
$partnerCode = $(this).attr('data-modcode');
$partnerName = $(this).attr('data-modname');
$boiler = $(this).attr('data-boiler');
$surcharges = $(this).attr('data-surcharges');
$ccalog = $(this).attr('data-ccalog');
// the 5 below currently populate the INPUT fields for the ID's listed
$('#modpartnerCode').val($partnerCode);
$('#modpartnerName').val($partnerName);
$('#modplatform').val($platform); // <- should go to dropdown
$('#modboiler').val($boiler); // <- should go to dropdown
$('#modsurcharges').val($surcharges); // <- should go to dropdown
});
});
$(document).on("click", ".modAccount", function ()
{
$('#modifyModal').modal('show');
});
As you've probably read in the note above, I can send the attributes to the INPUT fields with their respective IDs.
I need to be able to populate the first OPTION of a dropdown SELECT field.
I don't think I need to show the MODAL window code. As stated, I can populate the INPUT fields using the ID's I listed.
How can I get the IDs to populate the dropdown SELECT first OPTION value?
Here is the code for the modal:
<div class="modal fade" id="modifyModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
</div>
<div class="modal-body">
<form role="form" action="api/modifyAcct.php" method="get" id="modAcctForm" name="modAcctForm">
<input readonly type="text" class="form-control" id="modpartnerName" name="modpartnerName" />
<input readonly type="text" class="form-control" id="modpartnerCode" name="modpartnerCode" />
<select class="form-control" id="modboiler" name="modboiler" />
<option value=""></option> // <- #modboiler goes here
<option value="Y">Yes</option>
<option value="N">No<option>
</select>
<select class="form-control" id="modsurcharges" name="modsurcharges" />
<option value=""></option> // <- #modsurcharges goes here
<option value="Y">Yes</option>
<option value="N">No</option>
</select>
<select class="form-control" id="modplatform" name="modplatform" />
<option value=""></option> // <- #modplatform goes here
<option value="Y"></option>
<option value="N"></option>
</select>
</div>
</div>
</div>
</div>
Use jquery to select the first option and set the value.
$('#modplatform option:first').val($platform);
$('#modboiler option:first').val($boiler);
$('#modsurcharges option:first').val($surcharges);

Laravel 4.2 Form SUbmit Friendly URL

I have a form in Laravel 4.2
{{Form::open(array('url'=>'search', 'method'=>'get', 'class'=>'navbar-form navbar-left' ,'role'=>'search'))}}
<div class="form-group ">
<input list="browsers" name="topic" class="form-control " placeholder="Search">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
</div>
<button class="gray-button"><span>Search</span></button>
{{Form::close()}}
When I Click on submit button, I get URL like
http://localhost/car/public/search?topic=dfg
But I want to have a URL like
http://localhost/car/public/search/dfg
Do I need to change some code in Form?
Or in Router.
Can anyone help me please?
Thanks in advance for helping.
You do need JavaScript if you don't want to do a server redirect as #lukasgeiter pointed in the comments. You also need to setup a route for that particular search URL. The route should look something like this using a route closure:
Route::get('search/{topic}', function ($topic)
{
// code goes here
});
For the client side you need to stop the form from submitting and go to a manually built URL when submitting the form. So you could have something like this:
{{Form::open(array('url'=>'search', 'method'=>'get', 'id' => 'searchForm', 'class'=>'navbar-form navbar-left' ,'role'=>'search'))}}
<div class="form-group ">
<input list="browsers" name="topic" class="form-control " placeholder="Search">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
</div>
<button class="gray-button"><span>Search</span></button>
{{Form::close()}}
<script>
document.getElementById('searchForm').onsubmit = function (event)
{
// Prevent the form from submitting
event.preventDefault();
// Build the url using the form action and the topic value
var topic = document.querySelectorAll('input[name="topic"]')[0];
window.location.href = this.action + '/' + encodeURIComponent(topic.value);
};
</script>

why my contents are not getting loading when i come from some page and get loaded when i refresh it in jquery mobile

I need help on the following. there are 3 pages 1st is static and second is dynamic based on that third is also dynamic.
When it navigates from 1 st to 2nd it get the content as displays it but when it navigates from 2nd to 3rd it unable to load the content. But when i refresh the page it loads the content. I am not able to get what's wrong in this. Could anyone help me on this?
Javascript:
<script type="text/javascript">
$(document).ready(function () {
var b_id = getUrlVars()["b_id"];
$.ajax({
url: 'http://localhost/ajax_practice/php/get_categories.php?b_id=' + b_id,
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success: function (data, status) {
$.each(data, function (i, item) {
var output = "";
$(".main_category").append(output);
$(".main_category").listview("refresh");
output += "<li>";
output += "<a href='directory.html?c_id=" + item.id + "&b_id=" + b_id + "' data-transition='slide' rel='external'>";
output += item.category_name;
output += "</a>";
output += "</li>";
$(".main_category").append(output);
$(".main_category").listview("refresh");
});
}
});
});
</script>
Html page
<div data-role="page" id="page">
<div data-role="header">
<ul class="header_box" data-theme="d">
<li>
<select data-native-menu="false" name="selectmenu" id="selectmenu" data-theme="d">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<select data-native-menu="false" name="selectmenu2" id="selectmenu2" data-theme="d">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
</li>
<li>
<a>
<img src="images/logo1.png"><span>www.justclick.co</span>
</a>
</li>
<li>
<select data-native-menu="false" name="selectmenu2" id="selectmenu3" data-theme="d">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<select data-native-menu="false" name="selectmenu2" id="selectmenu4" data-theme="d">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
</li>
</ul>
<div class="header_search">
<div class="search_input">
<input type="text" placeholder="What ? e.g. Hotel" data-theme="d">
<input type="text" placeholder="Where? eg. Area" data-theme="d">
</div>
<div class="search_button">
<a>
<img src="images/search_button.png">
</a>
</div>
<div class="clear"></div>
</div>
</div>
<div data-role="content">
<div data-role="header">
<div class="service_heading">Business Information & Services</div>
</div>
<ul data-role="listview" class="main_category"></ul>
</div>
</div>
php code
<?php header( 'Content-type: application/json');
include 'connect.php';
$b_id=$_GET[ 'b_id'];
$sql_select=mysql_query( "SELECT * FROM business_category WHERE b_id=$b_id") or die(mysql_error());
$records=array();
if(mysql_num_rows($sql_select)>0) {
while($row=mysql_fetch_array($sql_select)) {
$records[] = $row;
}
echo $_GET['jsoncallback'] . '(' . json_encode($records) . ');';
} else {
echo 'data not selected';
}
?>
I am not able to get the content of the page when i come from previous page but i get it when i refresh the page.Why is it so?
jQueryMobile uses by default an Ajax mechanism for transitions between pages. Note that when a new page is loaded through Ajax, the scripts and styles included inside the head tag are not loaded. jQueryMobile only loads the body element of the new pages and more specifically the data-role="page" element.
However when the page is loaded through HTTP then the scripts and styles inside the head tag are normally loaded. This explains why your page appears properly when opening the page directly.
In order to resolve your issue you have to perform the following actions:
Replace $(document).ready(function() { with $(document).on('pageinit', '#your-page-id', function() {
Move your second page's script from the head tag inside the <div data-role="page"> or place the script inside a common JS file and load it inside the first page's head tag
I hope this helps.

Categories