Laravel 4.2 Form SUbmit Friendly URL - php

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>

Related

how to give selected option in html5?

I have a problem with my code, when i clicked option and refresh browser will give back to default option
html
<select id="harga">
<option value="renceng" isi="10" selected>Renceng</option>
<option value="pack" isi="40">Pack</option>
<option value="dus" isi="800">Dus</option>
</select>
<input id="hasil" type="text" name="harga" placeholder="isi" value="10" readonly>
js
$("#harga").on("change", function(){
var nilai = $("#harga :selected").attr("isi");
$("#hasil").val(nilai);
});

focus specific part of page after redirect in php

On button click event, I want to redirect to a specific section of page (a form in my case) and also put a focus on that section.
How do I achieve that focus?
<button class="btn btn-primary" id="addNewRequest" onclick="window.location='<?php echo Utility::getBaseUrl();?>leave/request/#addEmergencyLeave'">Add New Request</button>
Here's a form I want to point to and focus
<div>
<form id="addEmergencyLeave" method="POST" action="<?php echo Utility::getBaseUrl();?>addEmergencyLeave">
<select name="user_id" class="select2-container form-control" id="employee_list">
<option></option>
<option></option>
</select>
<select id="EmergencyLeaveType" class="select2-container form-control" name="EmergencyLeaveType">
<option></option>
<option value="2">Personal Leave</option>
<option value="1">Sick Leave</option>
<option value="6">Substitute/Others Leave</option>
<option value="7">Special Leave</option>
</select>
<select name="emergency_leave_length" class="select2-container form-control" id="emergency_leave_length">
<option></option>
<option value="0">Full Day</option>
<option value="1">First Half</option>
<option value="2">Second Half</option>
</select>
<div class="form-group">
<button class="btn btn-default" type="submit" value="" name="EmergencyLeaveSubmit">Submit and Approve Leave</button>
</div>
</form>
</div>
I have managed to redirect to that specific #addEmergencyLeave portion but couldn't put a focus on it.
Any help is very much appreciated. Thanks.
What do you mean, when saying focus a form? If you'd like to focus first select in your form just use #employee_list in your href.
Another way is to use an autofocus attribute on your select, but I think it's not exactly what you was looking for.
You can't focus a form, you can only focus a form element, and it is usually achieved using javascript.
Add this code into your HTML (before the closing </body> element), it will check if the hash #addEmergencyLeave is in the URL, and in this case it will focus on the form's first element:
<script>
function onload() {
if (location.href.indexOf('#addEmergencyLeave') != -1) {
document.getElementById('employee_list').focus();
}
}
window.addEventListener('load', onload, false);
</script>
addEventListener is the correct way to add an event listener on javascript, here it is the load event on the window element, usually if you use jQuery you will prefer the document ready event which is way faster.

How to link a HTML <select> list with a submit button

I have a webpage which consists of a select list with 6 options, along with a submit button. I need to code it in such a way that when one option is selected in the list and the submit button is clicked, it should open another linked page, whereas when another option is selected and submit button is clicked it should open some another page.
Basically I want each option to open some linked page when the submit button is clicked.
By googling a bit, I understood that I have to use php for this but I am not getting the appropriate code for the same.
I do not want the submit button to open only 1 specific page. Rather,I want it to open 6 different pages corresponding to different options selected.
Code Snippet :
<div>
<H1><FONT="TIMES ROMAN" FONT-COLOR="BLUE" > SELECT An subject:</H1>
<select>
<option value=""></option>
<option value="physics">physics</option>
<option value="chemistry">chemistry</option>
<option value="biology">biology</option>
<option value="maths">maths</option>
<option value="cs">cs</option>
<option value="electrical">electrical</option>
</select>
<br>
<input class="SubmitButton" type="submit" name="SUBMITBUTTON" value="Submit" style="font-size:20px; " />
</div>
Also i have learnt that it cannot be done using href tag because an option list cannot directly open pages, a submit button is required to perform an action.
Need help to resolve this.
I think ts wants to open multiple window according to selected values.
So here is example:
<div>
<H1>SELECT An subject:</H1>
<form id="link">
<select multiple="multiple">
<option value="">Choose links</option>
<option value="http://stackoverflow.com/">Stackoverflow</option>
<option value="http://google.com/">Google</option>
<option value="http://yahoo.com/">Yahoo</option>
<option value="http://bing.com/">Bing</option>
<option value="http://php.net/">PHP official</option>
<option value="http://w3c.org">W3C</option>
</select>
<br>
<input class="SubmitButton" type="submit" name="SUBMITBUTTON" value="Submit" style="font-size:20px; "/>
</form>
</div>
<script src="http://code.jquery.com/jquery-3.0.0.min.js"></script>
<script>
$('#link').on('submit', function (e) {
e.preventDefault();
var $form = $(this),
$select = $form.find('select'),
links = $select.val();
if (links.length > 0) {
for (i in links) {
link = links[i];
window.open(link);
}
}
});
</script>
First, you must set multiple attribute to select. Then via jquery you can open each link in new popup. Be careful browsers may block this popups.
UPDATE
If you want to open just one window, you can use #Anant's solution
Simplest solution based on jquery:-
<div>
<H1><FONT="TIMES ROMAN" FONT-COLOR="BLUE" > SELECT An subject:</H1>
<select id ="dropDownId"> <!-- give an id to select box-->
<option value="">Select Option</option>
<option value="physics">physics</option>
<option value="chemistry">chemistry</option>
<option value="biology">biology</option>
<option value="maths">maths</option>
<option value="cs">cs</option>
<option value="electrical">electrical</option>
</select>
<br>
<input class="SubmitButton" type="submit" name="SUBMITBUTTON" value="Submit" style="font-size:20px; " />
</div>
<script src = "//code.jquery.com/jquery-3.0.0.min.js"></script> <!-- add jquery library-->
<script type = "text/javascript">
$('.SubmitButton').click(function(){ // on submit button click
var urldata = $('#dropDownId :selected').val(); // get the selected option value
window.open("http://"+urldata+".html") // open a new window. here you need to change the url according to your wish.
});
</script>
Note:- This code will do below things:-
1.Select box page is never refreshed.
2.Based on selected value your URL'S are open in new window.
3.Also you can change URL format according to your requirement. I just gave a sample example.
Add this script in your html code
function showPage() {
var sel = document.getElementById('subjects');
var option = sel.options[sel.selectedIndex].value;
window.open(option + ".html");
}
and copy below html code and replace with yours
<select id="subjects">
<option value=""></option>
<option value="physics">physics</option>
<option value="chemistry">chemistry</option>
<option value="biology">biology</option>
<option value="maths">maths</option>
<option value="cs">cs</option>
<option value="electrical">electrical</option>
</select>
<input class="SubmitButton" type="button" value="Submit" onclick="showPage()" style="font-size:20px; " />
hope this helps :)
You can do this with PHP, but you will need to do 2 things:
Put the select and input into a form in your HTML, like this:
<form action="" method="post">
<select name="opt">
<option value="1">Link 1</option>
<option value="2">Link 2</option>
<!-- etc -->
</select>
<input type="submit" name="submit">Submit</input>
</form>
Have PHP catch the form's POSTed data and redirect to the appropriate page, like this: (put this at the top of your PHP page with the form)
<?php
if (isset($_POST['submit']))
{
if (isset($_POST['opt']))
{
if ($_POST['opt'] == '1') { header('Location: page1.php'); }
elseif ($_POST['opt'] == '2') { header('Location: page2.php'); }
// etc...
}
}
?>
Header redirects only work if there has been no HTML output before they're called, so make sure there's no HTML code before the PHP code block.

how to redirect to new page by selecting an option in datalist

I have created a datalist here how can I add a link to option so that whenever i select a option and click on submit i should redirect to another page based on selected option.I have googled a lot but I couldn't any information about this.Thanks in advance
<input list="category" name="category">
<datalist id="category">
<option value="fruits">
<option value="animals">
<option value="vechiles">
<option value="ornaments">
</datalist>
<input type="submit">
You can try below code :
<input list="category" name="category" id="textcat">
<datalist id="category">
<option id="www.google.com" value="fruits">
<option id="www.fb.com" value="animals">
<option id="www.ymail.com" value="vechiles">
<option id="www.msn.com" value="ornaments">
</datalist>
<input id="btn" type="button" value="submit">
<script>
$('#btn').click(function(){
var textval = $('#textcat').val();
$('#category option').each(function(){
if($(this).val() == textval){
alert($(this).attr('id'));
window.location = $(this).attr('id');
}
});
});
</script>
Make some formHandler.php in which you will some routing, and point your form (action="formHandler.php") that includes this select
$redirectLink=$_REQUEST['category'];
header('Location:'.$yourmainSitePath.'/'.$redirectLink)
Are you satisfied ? :D

PHP URL that's identified by the ID?

I'm still learning to use PHP with MySQL tables and I'm sorry if this is a novice question but how would I change the dropdown code below to be able to insert normal a href links (with an image or text) that link to the playerMenu.php page? Here's the dropdown menu code I have:
<form action="playerMenu.php" method="get">
<select id="players" name="selectvalue" onchange="showMe(this.value);">
<option value="">Select</option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
</select>
<input type="submit" value="Submit">
<form>
Submit
Thanks in advance.
Here's how using JQuery. It works! Try it out!
<!-- Your Form -->
<form>
<select id="players" name="selectvalue" onchange="showMe(this.value);">
<option value="">Select</option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
</select>
<input id="button" type="button" value="Submit">
<form>
<!-- Include JQuery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
var sub_link = "playerMenu.php?electvalue="
$("#button").click(function() {
sub_link = sub_link + $("#players").find('option:selected').text();
// Here's your link
alert(sub_link);
// And now we do a javascript redirect
window.location.replace(sub_link);
});
</script>
From the PHP side, once you submit that form then the url will be
playerMenu.php?players=1&submit=Submit
If the option A was chosen. You can remove the onchange and onclick javascript, if this is what you want to achieve.
From there you have passed in the value of the players select box. You would want to then likely save this as a variable and what you want with the code. If you are doing something with your database then make sure to santize the variable as well.

Categories