HTML select always posts first option when form is submitted - php

I have the simplest possible form, with one select with 3 options and a submit button. The problem is that when I click the submit button the selection in the select element, returns to the first option. And when I do the var dump of POST variable on the page that is the form handler, I get the string that is the first option. Why is this happening? What do I need to do? I have a bootstrap modal window on another page, with two select elements and it works fine there?
Here is the html code:
<form class="navbar-form navbar-center" method = "POST" action="sortiranje.php" enctype="multipart/form-data">
<select class="cities typeahead form-control" id="kriterijum" name="kriterijumSortiranja">
<option value=""></option>
<option value="CenaR">Cena rastuće</option>
<option value="CenaO">Cena opadajuće</option>
<option value="Tip">Tip</option>
</select>
<button type="submit" class="btn btn-default navbar-btn">Sortiraj</button>
And in sortiranje.php there is only:
<?php
var_dump($_POST['kriterijumSortiranja']);
?>
And the result is:
string '' (length=0)

Related

Why can't I save values in Session to later use in other pages?

I have an admin page where I want to add new products. There is a dropdown options menu where the user selects which type of products he wants to add. I want to save the value of the dropdown in $_SESSION['productType']. When the form is submitted the user is redirected to a new page where they can add the new product depending of the selected type which is supposed to be stored in $_SESSION['productType'] but it is not.
This is the html of the dropdown select:
<form action="AddNewItem.php" method="post">
<select name = "option" class="form-select mx-auto" aria-label="Default select example">
<option value="Foods">Foods</option>
<option value="Toys">Toys</option>
<option value="Electronics">Electronics</option>
</select>
<input onclick="location.href='AddNewItem.php';" type="submit" name="submit" class="btn btn-sm btn-primary btn-block" value="Add new Item">
</form>
Here I add the value from the form to the $_SESSION:
session_start();
if(isset($_POST['option']))
{
$option = $_POST['option'];
$_SESSION['productType'] = $option;
exit();
}
And here I try to get the value from the $_SESSION to check what type of product the user selected so I can hide the inputs for different type of products:
<div class="form-group" <?php if($_SESSION['productType'] != 'Foods') echo 'hidden';?>>
What am I doing wrong?
Can you try removing onclick="location.href='AddNewItem.php';" from the input element?
It looks like you are navigating to the new page on submit rather than submitting the form.

How to interact with submit input type with no name in php using curl?

<select name="_limit">
<option value='50' >50</option>
<option value='200' >200</option>
<option value='300' >300</option>
<option value='500' >500</option>
</select>
<input type="submit" value="Filter" class="btn btn-success"/>
Here, the page displays list containing 50 data by default. I want to change the display limit to higher value like 200.
How can it be done using curl?
Note: The list is filtered only when the 'Filter' submit button is clicked.
For select option I can use something like
$post_str = "_limit=200";
But what about the submit input type as there is no name?

how to submit php post data with jquery

I have a form with action attribute as searchbycat.php. in the form there is a selection field based upon which contents in the php page will be dynamically made. so I am passing the selected value by GET and processing the value in searchbycat.php. so I am implementing jQuery's click event when the user clicks the submit button and changing the action attribute value with the selected value from the drop down menu. but it does not seem to work. I searched for this in stackoverflow and found that it can be done in jquery ajax call. but those answers is not really helping me. can somebody please explain how to do this?
here is my code:
<form action="" id="form1" method="POST">
<select name="category">
<option>Search By Category</option>
<option value="mobile">Mobile Phone</option>
<option value="computer">Computer Electronics</option>
<option value="book">Books</option>
<option value="fashion">Fashion and Beauty</option>
<option value="furniture">Furniture</option>
<option value="house">House Rent</option>
<option value="vehicle">Vehicle</option>
</select>
<input type="submit" name="submit" id="search" value="Search"/>
</form>
and here is the Jquery part I am trying to do:
<script type="text/javascript">
$(document).ready(function(){
$('#search').click(function(){
$('#form1').attr("action","searchbycat.php?category=<?php echo $_POST['category']?>");
});
});
Just change your form to method = 'GET' and action="searchbycat.php"
It will send automatically like you want. You don't really will need the jquery in this situation

Read data from HTML Select Tag before sending to PHP

I need to send a option to a PHP script, but the instead of a user clicking on a submit button, they simply click a type link.
For instance:
<select name="test">
<option value="1">1</option>
</select>
Next
So the user will select 1, then click on the link that says "Next". It's almost like i need to read the select tag BEFORE sending it to the php by a GET request (php.php?select=1). Any ideas how I can pass over this roadblock?
You Have to use <form> tag to make this possible or use ajax submit tu submit your form with <form> you can do in this way
<form action="next.php" method="POST">
<select name="test">
<option value="1">1</option>
</select>
<!-- Then after this don't use anchor tag <a> use input submit instead-->
<input type="submit" name="submit" value="Next"/>
</form>
Or you can also submit it with <button> :
<form action="next.php" method="POST">
<select name="test">
<option value="1">1</option>
</select>
<!-- Then after this don't use anchor tag <a> use button instead-->
<button type="submit">Next</button>
</form>
as you mentioned in question you want to get the value of select before submit to php you can use jQuery ajax to do this look below
<form id="my_form" action="">
<select id="my_tag" name="test">
<option value="1">1</option>
</select>
<button type="submit">Next</button>
</form>
Ajax:
$('#my_form').submit(function(e){
if($('#my_tag').val() !== '') {
var select = $('#my_tag').val();
$.ajax({
type: 'Get', // Could be GET/POST
url: 'php.php?select='+select, //targeting url with value from select tag
success: function(data){
alert(data); // alerting the data which php file returned.
}
});
} else {
alert('error'); // if select tag is empty we will show an error.
}
e.preventDefault();
});

Populate inputs on selects JQUERY

i have a dropdown menu generated by my php when the page loads filling with my rows in the sql table, being the table ID(Value) and they NAME.
<select>
<option value="0"></option>
<option value=6>Alientech</option>
<option value=2>FNAC</option>
<option value=5>Logitech</option>
<option value=1>MHR</option>
</select>
Then i have:
<input type="text" name="editname">
<input type="text" name="editmail">
<input type="text" name="editwebsite">
<input type="text" name="editphone">
<input type="text" name="editfax">
<input type="text" name="editadress">
<input type="text" name="editincharge">
What i want to do is on selecting the option above he gets the id of the row in mysql and fills the inputs accordingly to the values in the SQL table so a person can edit the values on submitting the form.
How can i with jQuery do this?
Where is your code attempt?
The way to do this is on select of the jquery
you would take the value of the select where
<select id="some_id">
also you will need to ID all your fields of input.
Your PHP script should return a JSON where your on success you parse the JSON and set it to the value of your INPUT field via ID.
$.ajax({
url: some_php_mysql.php
type:post,
data: {some_id:$('#some_id').val()},
success:function(data){
//json object returned - parse and then ID each value
});
First, you could reload the page :
<form action="" method="post" id="myform">
<select name="myselect">
<option value="0" disabled="disabled"></option>
<option value=6>Alientech</option>
<option value=2>FNAC</option>
<option value=5>Logitech</option>
<option value=1>MHR</option>
</select>
</form>
With a little bit of jQuery...
$("form#myform select").change(function(){
$("form#myform").submit(); // Submit the form if the value is changed.
});
And when you load your page, if you find a value for $_POST["myselect"] then you have to load something in the fields (except if it's 0).
You could also do things asynchronously with Ajax (instead of submitting the form, send a HTTP request and parses the result to fill the form). However, if the purpose of your page is only to edit an entry, the you should use the above method.
Ajax reference : http://www.w3schools.com/ajax/

Categories