dropdown select changes back unwantedly after input is submitted - php

I'm using this following simplified HTML code integrated in a PHP code, which is left out.
The user can choose between two options of the dropdown selection and then enters an input to be submitted.
The problem is that whenever clicking on the submit button after an input is entered and option2 is selected, then the dropdown selection is changed back to option1.
Now, I want option1 to be the default when the page is first loaded, but I'd also like that whenever option2 is selected - that it would be "anchored", that is it wouldn't be changed unless the user selects a different option or when the page is refreshed....
Does anybody know how to do that?
Thanks in advance!
P.S - don't know if this matters but in the original code, I entered the PHP file name in the "action" attribute inside the form tag.
<html>
<head>
</head>
<body>
<form action="" method="post" >
Category:
<select id="searchType" name="searchType" size="1">
<option value="1" class="bold-option">
Option1
</option>
<option value="2">Option2</option>
</select>
<br/>
<input type="text" name="name" class="input_field" id="input_txt" dir="auto" >
<input type="submit" value = "submit!" class="submit_button"/>
</form>
</body>
</html>

Related

Reading text values from selected fields in php

When I try to read the dropdown with $data['fieldname'] , I expect to get back the text that’s displayed in the field, but that isn’t what happens. Dropdowns always point to either another record instance or an element of a list; these instances and elements all have an internal ID, and that internal ID is what I get back.
But what if I actually want the text that’s displayed in the dropdown?
When submitting a form, what you get is just the value of the selected option, and not the text. However, there is a way to bypass it if you really need the text. The strategy is to add a hidden input to your form, and fill it with the text of the selected option whenever a new option is selected. It will be something like the following code:
<form action="somewhere.php" method="POST">
<select name="field" id="field" onchange="document.getElementById('content1').value=this.options[this.selectedIndex].text]">
<option value="1">First Value</option>
<option value="2">Second Value</option>
</select>
<input type="hidden" name="fieldname" id="content1" value="" />
<input type="submit" name="submit" value="submit">
</form>
Explanation: here we have a dropdown named field, and there is an onchange event for this dropdown. Whenever a new option is selected, we put the text of that option into the hidden input which is called content1. The name of this hidden input is fieldname, so when this form is submitted, you can get the text in the post variable: $_POST['fieldname'];

Drop-down list, click direct to new URL, no submit button, posting hidden PHP variables

I want to make a drop-down list, each selection being a new URL link, where there is no Submit button. Instead the user goes straight to the URL when they select from the drop-down. I can make this work, using the following HTML:
<select name="mydropdown" class="class1" onChange="document.location = this.value" value="GO">
<option value="page1.php">Page1</option>
<option value="page2.php">Page2</option>
<option value="page3.php">Page3</option>
</select>
However, I also want to post two hidden PHP variables to the selected page, $username and $password.
Any help much appreciated.
You can try this by submitting the form by jquery. Also pass the password in encoded format. Check below code ==>
function callredirect(pagename){
$("#frmdropdown").attr("action",pagename);
$("#frmdropdown").submit();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post" id="frmdropdown">
<input type="hidden" name="username" value="abc" />
<input type="hidden" name="password" value="password" />
<select name="mydropdown" class="class1" onChange="callredirect(this.value)" >
<option value="page1.php">Page1</option>
<option value="page2.php">Page2</option>
<option value="page3.php">Page3</option>
</select>
</form>
The best way to pass variables to other page is using the $_SESSION, since you don't have form and user doesn't want to know what variables/ values you are passing. read the docs here about PHP Sessions
NOTE: Advise not to pass password through out the page, it make your
system vulnerable for attackers. Best practice is verify user in each
page if you want or encrypt and store the password.

Submit a html form without submit button [duplicate]

I am creating a page in JSP where I have a dropdown list and once the user selects a value he has to click on the go button and then the value is sent to the Servlet.
</select>
<input type="submit" name="GO" value="Go"/>
How do I make it so that it does it on change? E.g. when the user selects John all his details are retrived from the DB and displayed. I want the system to do it without having to click the go button.
Just ask assistance of JavaScript.
<select onchange="this.form.submit()">
...
</select>
See also:
HTML dog - JavaScript tutorial
Simple JavaScript will do -
<form action="myservlet.do" method="POST">
<select name="myselect" id="myselect" onchange="this.form.submit()">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
</select>
</form>
Here is a link for a good javascript tutorial.
other than using this.form.submit() you also can submiting by id or name.
example i have form like this : <form action="" name="PostName" id="PostID">
By Name : <select onchange="PostName.submit()">
By Id : <select onchange="PostID.submit()">
To those in the answer above. It's definitely JavaScript. It's just inline.
BTW the jQuery equivalent if you want to apply to all selects:
$('form select').on('change', function(){
$(this).closest('form').submit();
});

Create Clickable Links for Dropdown Menu Options in PHP

I'm making a system where a user can search through a library by specific fields.
I currently have a PHP document called index.php that contains a drop down box, a text box, and a submit button. The user selects from the drop down box which field they want to search through (for example Author or Title), then enters their search criteria in the text box (for example "Tolkien" or "The Hobbit") and then clicks on the submit button which then loads simpleSearch.php. The text the user searches for and the field they are searching through are defined on simpleSearch.php as follows:
$queryString = $_GET["userSearch"]; // get user's search string
$queryType = $_GET["searchType"]; // get user's search type
What I would like to have is a different layout but the same functionality on the index.php page. The layout above the double blue line is the current layout. The layout below the double blue line is my desired layout.
Instead of choosing a field from the drop down box and clicking on Submit the user would enter their search criteria and then click on one of the fields. The button would act as the drop down selector AND the submit button at the same time.
Here is my code for the current layout:
<form action="simpleSearch.php" method="get">
<select name="searchType">
<option value="all">All Fields</option>
<option value="author">Author</option>
<option value="title">Title</option>
<option value="subject">Subject</option>
</select>
<input type="text" name="userSearch">
<input type="submit">
</form>
Any help at all would be really appreciated! Thanks!
what about use:
<form action='simpleSearch.php' method='get'>
<input type="text">
<input type="submit" value="all" name="userSearch">
<input type="submit" value="author" name="userSearch">
<input type="submit" value="title" name="userSearch">
<input type="submit" value="subject" name="userSearch">
</form>
And in simpleSearch.php u can get value by $_GET['userSearch']
and dont forget to SQL inject danger ;)

PHP Dropdown button, MySQL update

I've been looking around on SO and I don't -think- this is something that's up here, either that or it's something I've missed. If so, please point me there and I'll simply mark this question as answered.
I have a completely working PHP form where my users input information into input boxes and there's a button at the bottom that submits the information to the MySQL database, so that part works perfectly. What I'm trying to do now is this: have 2 drop down menu's, each one with a static list of choices and I'd like those choices to also get sent to the database.
For instance, currently in my form I have First Name (frame) and Last Name (lname) that the user can input and then if I query the database it spits out First Name and Last Name perfectly. So now I'd like to add to my form a drop down box where the user can pick, for example, Boy or Girl, and then after doing that click the submit button that's already there (I don't want the drop-down to submit the data, and I don't want the drop-down to be populated from the database.)
I'm guessing I need to use Javascript for this part? But I really don't know.
Any advice would be appreciated.
<html>
<head>
<title>MyForm</title>
</head>
<body>
<form id="form" name="form" action="" method="post">
<Label>First Name/Organization</label>
<input type="text" name="firstname" value="<?php echo $firstname; ?>"/>
<input type="submit" name="submit" value="Add entry">
</form>
</body>
</html>
So, as part of the same form, you simply treat it just like the input boxes. Here's an example:
If the dropdown is named:
<select name="gender">
<option value="boy">Boy</option>
<option value="girl">Girl</option>
</select>
Then in your php, you would simply get the value for gender:
$gender = $_POST["gender"];
And add to your SQL statement where you are saving the first/last name the additional field and value for gender.
Of course, you would have to first modify the table to have the column for gender....
Hope that helps!
It sound s like you just want a simple html drop down as part if your form. Just use
<select name = 'gender'>
<option value = 'boy'> Boy </option>
< option value = 'girl'> Girl </option>
</select>
Then access $_POST['gender'] in the receiving script to put it into the database (after validation of course)

Categories