Passing Javascript variable to PHP, through select form - php

So my exact problem is, I have a select form. There are 4 options in there, with values 1, 2, 3, and 4. There's a button form next to the select. Whenever I click on the button, it should navigate me to the select form's selected option's location (for example index.php?page=1). How can I solve this problem? I've read its impossible due to the php's server sides mechanic, etc, but there must be a way.. So far I've got an onclick event for the button, like...: location.href='index.php?page=.. and whats going in there ..

it will work
<select id="some_id">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<button type="button" onclick="window.location.href='index.php?page='+document.getElementById('some_id').value;">Go</button>

A normal GET form should work.
<form action="index.php" method="get">
<select name="page">
<option value="1">Page Name</option>
...
</select>
<input type="submit" value="Submit">
</form>

Related

Passing a value outside the form to another page in url PHP

Im trying to build a web app where a user selects the value from a dropdown list and clicks the button next to the item. But only the value of select gets sent over to the next page.
echo "<td>".$row["idTaco"]."</td><td>".$row["nameTaco"]."</td><td>".$row["priceTaco"]."</td><td>".$row["descTaco"]."</td>";
?>
<td>
<form action="Includes/dodajVKosarico.inc.php?idTaco="<?php $row["idTaco"] ?> method="get">
<select name="kolicina">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<button type="submit" name="submit" class = "dodaj">Dodaj</button>
</form>
</td>
The row['idTaco'] works fine in the echo but when i try to send it using this form i cant pass the value to another page. Im sorry im still new to this.
Rather than using a query string value, one (arguably neater and easier to see) option is to just add the value again as a hidden field in your form, then it will get submitted along with the rest of the form data.
<form action="Includes/dodajVKosarico.inc.php" method="get">
<input type="hidden" name="idTaco" value="<?php echo $row["idTaco"] ?>"/>
...etc
Or you could just fix the broken quotes (and missing echo) in your existing code:
action="Includes/dodajVKosarico.inc.php?idTaco=<?php echo $row["idTaco"] ?>"
Everything is fine just missing an echo before your $row["idTaco"], You must do as follows:
<form action="Includes/dodajVKosarico.inc.php?idTaco=<?php echo $row['idTaco']; ?>" method="get">

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.

how to pass the id value of a select list in a form using post or get?

A select list in a form, I know there's no problem to pass the option value using post or get. But if I also want to pass the select id value, is it possible and how?
For example:
<form action="Test.php" method="POST">
<select name="select" id = '1'>
<option value="">Select a Status</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" value="submit"/>
</form>
And here is the simple print php code:
<?php
print_r($_POST['select']);
?>
It can print the value of the option that has been chosen, but how to modify the code if I also want to post the id value, which is 1 here. I want to do so because I want the id to become a variable to store some int numbers. Thank you!
The id attribute is designed purely for use in client side code. If you want to pass extra data, then use hidden inputs — that is what they are designed for.
<input type="hidden" name="select_extra" value="1">
<select name="select">
<option value="">Select a Status</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
ID's won't get passed automatically in HTTP.
You need either:
Use an input hidden
Use a special name. I.E. <select name="select_1" id="1">...</select

HTML form submit to PHP script

I am making an HTML form. I want the results to appear in the PHP script.
<form action="chk_kw.php" method="post"> <br />
<select> name="website_string"
<option value="" selected="selected"></option>
<option VALUE="abc"> ABC</option>
<option VALUE="def"> def</option>
<option VALUE="hij"> hij/option>
</select>
<input type="submit" name="website_string" >
</form>
The problem is I cannot get the value passed to the PHP. if I use value:
<INPUT TYPE="submit" name="website_string" value="selected" >
It always passes the text in the quotes. In this case "selected". How do I pass one of the strings from the option?
Try this:
<form method="post" action="check.php">
<select name="website_string">
<option value="" selected="selected"></option>
<option VALUE="abc"> ABC</option>
<option VALUE="def"> def</option>
<option VALUE="hij"> hij</option>
</select>
<input TYPE="submit" name="submit" />
</form>
Both your select control and your submit button had the same name attribute, so the last one used was the submit button when you clicked it. All other syntax errors aside.
check.php
<?php
echo $_POST['website_string'];
?>
Obligatory disclaimer about using raw $_POST data. Sanitize anything you'll actually be using in application logic.
<form method="POST" action="chk_kw.php">
<select name="website_string">
<option selected="selected"></option>
<option value="abc">abc</option>
<option value="def">def</option>
<option value="hij">hij</option>
</select>
<input type="submit">
</form>
As your form gets more complex, you
can a quick check at top of your php
script using print_r($_POST);,
it'll show what's being submitted an the respective element name.
To get the submitted value of the element in question do:
$website_string = $_POST['website_string'];
It appears that in PHP you are obtaining the value of the submit button, not the select input. If you are using GET you will want to use $_GET['website_string'] or POST would be $_POST['website_string'].
You will probably want the following HTML:
<select name="website_string">
<option value="" selected="selected"></option>
<option value="abc">ABC</option>
<option value="def">def</option>
<option value="hij">hij</option>
</select>
<input type="submit" />
With some PHP that looks like this:
<?php
$website_string = $_POST['website_string']; // or $_GET['website_string'];
?>
Assuming you've fixed the syntax errors (you've closed the select box before the name attribute), you're using the same name for the select box as the submit button. Give the select box a different name.
For your actual form, if you were to just post the results to your same page, it should probably work out all right. Try something like:
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> method="POST>
Here is what I find works
Set a form name
Use a default select option, for example...
<option value="-1" selected>Please Select</option>
So that if the form is submitted, use of JavaScript to halt the submission process can be implemented and verified at the server too.
Try to use HTML5 attributes now they are supported.
This input
<input type="submit">
should be
<input name="Submit" type="submit" value="Submit">
whenever I use a form that fails, it is a failure due to the difference in calling the button name submit and name as Submit.
You should also set your enctype attribute for your form as forms fail on my web host if it's not set.

php passing multiple selected values into a querystring

I have 8 select fields with different options in each and im trying to pass each selected value into a querystring but im not sure how this work.
<form method="post">
<fieldset>
<legend class="hidden">Choose your options</legend>
<ol>
<li><label>option 1</label>
<select>
<option value="">Select one..</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</li>
<li><label>option 2</label>
<select>
<option value="">Select one..</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</li>
<li><label>option 3</label>
<select>
<option value="">Select one..</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</li>
</ol>
</fieldset>
</form>
so ive got 8 of these and I want to select a value from each one and then press submit which will then bring up a best match from the values passed...
Read about Dealing with Forms.
You must give the form elements a name, e.g.:
<li><label>option 1</label>
<select name="option1">
<option value="">Select one..</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</li>
Then you can access the value via $_POST['option1'].
Note: As you specified POST as form method, the data is not sent via the URL to your PHP script, but is contained in the request body. If you want to have the data in the URL (as querystring) you have to change the method to GET.
I'm nut sure exactly what you're looking for, but if you put a name=".." attribute into your select tags, they'll end up into the querystring?
You are missing NAME argument from the SELECT TAG
Once you have this, options will be received by the php script in $_POST array.
So, for example:
<SELECT NAME="mySelect">
<OPTION VALUE="V1">Value 1</OPTION>
<OPTION VALUE="V2">Value 2</OPTION>
...
</SELECT>
You would read the value in your php script from $_POST['mySelect']
Also, keep in mind that you need to enter ACTION tag for your form, defining the php script that will execute once you send your form.
Each <select> needs to have a name, for example, the first one could be <select name="firstSelection">. When you click submit, the browser sends something like firstSelection=1&secondSelection=&thirdSelection=1.

Categories