How to change link when submit form - php

I have a sample code:
$category_id = $_POST['category_id'];
<form action="search.php&category_id=$category_id" method="post">
<p class="categories">
<select name="category_id">
<option value="1">Category 1</option>
<option value="2">Category 2</option>
</select>
</p>
<p class="submit">
<input class="button" type="submit" value="Tìm game" />
</p>
</form>
When I submit form is URL is search.php&category_id=0
How to fix this problem, URL is search.php&category_id=1 // OR 2

Change your <form> tag to
<form action="search.php?category_id=<?php echo $category_id?>" method="get">
The category_id in the action attribute will have it's value replaced by the value of the select.

Change method="post" to method="get" if you only want to send get data.
Keep method="post" if you want to take the user to that url and submit post data.
$("select").onchange(function(){
var id = $(this).val();
$("form").attr("action", $("form").attr("action") + "&category_id=" + id);
});

Related

fews trigger, reset forms

I have two form which works after form submit on change. I want add button which will reset value in form to deafult. I added below juqery but works only first value.
<form action="xxxx.php" method="post">
<select name="first" onchange="this.form.submit()">
<option>1</option>
<option>2</option>
<option>3</option>
</form>
<form action="xxxx.php" method="post">
<select name="two" onchange="this.form.submit()">
<option>A</option>
<option>B</option>
<option>C</option>
</form>
<a href="#" id="clean" class="btn btn-transparent"><span>Clean</span><
$("#clean").click(function () {
$('select[name="first"]').val("1").trigger('change');
$('select[name="two"]').val("A").trigger('change');
});
/* Create a delegated event listener that process the `button` click events. */
document.addEventListener('click',e=>{
/*
if the currently clicked element is the button then query the dom
for all forms and reset the elements within by calling the `reset` method
on the form
*/
if( e.target instanceof HTMLSpanElement && e.target.parentNode instanceof HTMLAnchorElement ){
// an anchor element is not a button - prevent the default behaviour!
e.preventDefault();
// find the forms, iterate through and call `reset()` on each form.
document.querySelectorAll('form').forEach(form=>form.reset())
}
});
<!--
added options to the select and a new input to each form so that the effect of the
`button` click can be observed.
-->
<form action="xxxx1.php" method="post">
<select name="status_date" onchange="this.form.submit()">
<option>1
<option>2
<option>3
<option>5
<option>6
<option>8
</select>
<input type='text' name='sport' />
</form>
<form action="xxxx2.php" method="post">
<select name="status_employees" onchange="this.form.submit()">
<option>A
<option>B
<option>C
<option>F
<option>P
<option>T
</select>
<input type='text' name='fruit' />
</form>
<span>Clean</span>

get the form values in php in the same page of the form

i want to take the < select > value to the php code (above) when i press the submit button
I tried to send the form values to the same page (with action) but it didnt work.
what should I do?
<form name="open_file">
<select name="file" style="width:40%; font-size:20px;">
<option value="A1" selected="selected" >A1</option>
<option value="A2" >A2</option>
<option value="A3" >A3</option>
</select>
<button type="submit" name="submit">UPLOAD</button>
</form>
<?php
//the <select> value should be in $file.
$file= ...?
?>
thank you !
Without defining the action attribute, form element WILL NOT send the request to any page via any method.
In addition, using button, probably would the server not send the request. Use input element instead.
If you really wish to send the data to the same page, use:
<form name="open_file" action="" method="GET">
<!--your link to this page in action attribute-->
<select name="file" style="width:40%; font-size:20px;">
<option value="A1" selected="selected" >A1</option>
<option value="A2" >A2</option>
<option value="A3" >A3</option>
</select>
<input type="submit" name="submit" value="UPLOAD" />
</form>
Plus, it is not a good practice to process form data after outputting HTML. Put the PHP code at the beginning of the document.
First, you have to define your form action and method like :
<form name="open_file" action="" method='GET'>
<select name="file" style="width:40%; font-size:20px;">
<option value="A1" selected="selected" >A1</option>
<option value="A2" >A2</option>
<option value="A3" >A3</option>
</select>
<button type="submit" name="submit">UPLOAD</button>
</form>
<!-- GET YOUR SELECT VALUES USING PHP -->
<?php
if(isset($_GET['file'])){
$file = $_GET['file'];
// Now you can use $file variable according to your code requirements
}
?>
For more detail : PHP Form Handling

Which li tag is clicked after form is submitted?

<form method="post" action="" id="myForm">
<ul>
<li>Male</li>
<li>Female</li>
</ul>
<input type="submit" name="submit" />
</form>
After the form is submitted .How can i know which li tag is clicked and i want to post that to php
give ids and classes to the li tags to register the click event, on that click event update a hidden field with the id, index, or text of the li so that it will be available on post. If you are unable to give unique id's or text values you can just use the index() of the li.
<form method="post" action="" id="myForm">
<ul>
<li id='male' class="sex">Male</li>
<li id='female' class="sex">Female</li>
</ul>
<input type="hidden" id="selectedsex" />
<input type="submit" name="submit" />
</form>
$('.sex').click(function () {
var val = $(this).attr('id');
$('#selectedsex').val(val);
});
Like Amritpal Singh suggested:
<form method="post" action="" id="myForm">
<select name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<input type="submit" name="submit" />
</form>
Plain <li> elements are not clickable and wont provide any data to your POST variable. You can use radio buttons for this.
index() is zero based
$('#myForm li').click(function(){
alert( $(this).index()) ;
/* or */
alert( $(this).text());
})
You could also toggle a Class on each click.... really not clear what you are asking to do

Passing two variables via GET to PHP using HTML form

I'm trying to pass two variables (a search query (String) and a column name (also a String)) via a HTML form. The first string is entered into a text box and the second is selected from a drop-down menu.
Obviously this method doesn't work:
<h3>Search for a Customer</h3>
<form action="search.php" method="get">
<input type="text" id="sString">
<select name="sField">
<option value="Name">Name</option>
<option value="HostID">Host ID</option>
<option value="OrderNumber">Order Number</option>
<option value="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
You need to give your <input> field a name in order for this to work. Unnamed inputs won't get passed through the form post.
You don't have a 'name' parameter on your sString input boxes. The name parameter is what gets sent back to the server, not the field's ID:
<input type="text" id="sString" name="sString">
and then in PHP
$search = $_GET['sString'];
You need to use attribute name instead of value.
<h3>Search for a Customer</h3>
<form action="search.php" method="get">
<input type="text" id="sString" name="sString"/>
<select name="sField">
<option name="Name">Name</option>
<option name="HostID">Host ID</option>
<option name="OrderNumber">Order Number</option>
<option name="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
Try:
<form action="search.php" method="get">
<input type="text" name="sString" id="sString" value="">
<select name="sField">
<option value="Name">Name</option>
<option value="HostID">Host ID</option>
<option value="OrderNumber">Order Number</option>
<option value="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
You should be able to access form entered variables by $_GET array in search.php. To see what is going on try var_dump($_GET);

PHP - Pass Drop Down List option through query string

I have a drop down list that has options that need to be passed through a query string. How would I go about doing this? Also, would anyone be able to list a way to do this both using a button and without using a button? Thank you!
<form method="get">
<select multiple="multiple" name="things[]">
...
</select>
<input type="submit" value="submit"/>
</form>
<?php
if(isset($_GET['things'])) {
foreach($_GET['things'] as $thing) {
echo $thing . '<br />';
}
}
?>
Based on Jani's response, are you wanting to have the form submit without a button but still have a backup button if the user doesn't have javascript? You can use noscript to cover that:
<form action="script.php" method="get">
<div>
<select name="options" onchange="this.form.submit()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<noscript>
<input type="submit" value="Go!" />
</noscript>
</div>
</form>
Without a button:
<form method="get">
<select multiple="multiple" name="things[]" onchange="this.form.submit()">
...
</select>
</form>

Categories