I want the user to be able to pick two different variables from a website (from a drop down menu) and hit a button to bring them to a page where files are to download based on the variables picked.
I have the html ready to go.. and i have both menus in an array in php.. i was wondering how to pass both variables through to another site and then have unique content depending on which ones picked..
how do i get php to make it's own site?
Try something like this:
page1.php
<?php
$ar = array('foo', 'bar');
?>
<form action="page2.php" method="post">
<select name="choice">
<?php foreach($ar as $value) { ?>
<option value="<?php echo $value ?>"><?php echo $value ?></option>
<?php } ?>
</select>
<input type="submit" value="go to page2">
</form>
page2.php
<?php
$choice = $_POST['choice']; // sanitize as needed
if( $choice === 'foo' ) {
// do foo choice
}
else if( $choice === 'bar' ) {
// do bar choice
}
?>
Query strings are also ok, just change the form method to GET, and $_POST to $_GET.
have you tried using query variables? og perhaps posting the info to the page?
I would suggest using the query like: http://www.yourdomain.com/speciallayoutpage.php?layout=2&colortheme=1
Related
I have url variables that was submitted from a form from the previous page. So my url looks something like site.com/submitted.php?first_name=hello&last_name=bye.
Now I am using a link to keep my submitted variables while I go to my second page
<a href="secondPage.php?first_name=hello&last_name=bye>pageLink</a>
On this second page, it's basically a drop-down using the select tag with a submit button that generates a table from mysql server which then links back into the same page. It looks like:
<FORM ACTION="secondPage.php?first_name=hello&last_name=bye" METHOD="GET">
<select name='selectedOption' >
<option value="op1">option1</option>
<option value="op2">option2</option>
<option value="op3">option3</option>
</select>
<INPUT TYPE="SUBMIT" VALUE = "Search">
</FORM>
But lets say I choose option1 and submit, my url does not keep the variables first_name and last_name but it just replaces it :
secondPage.php?selectedOption=op1
instead of:
secondPage.php?first_name=hello&last_name=bye&selectedOption=op1
Any help is appreciated.
I'm not sure how you want to pass data around but here is a suggestion to get the values you want with php.
Change this:
<FORM ACTION="secondPage.php?first_name=hello&last_name=bye" METHOD="GET>
to this:
<form action="secondPage.php" METHOD="post">
Your code for secondPage.php coud look something like this:
// the 'if' statements aren't necessary. just an idea for simple server-side
// validation
if(isset($_REQUEST['selectedOption']) && $_REQUEST['selectedOption'] != ''){
$option = $_REQUEST['selectedOption'];
}
if(isset($_REQUEST['first_name']) && $_REQUEST['first_name'] != ''){
$fname = $_REQUEST['first_name'];
}
if(isset($_REQUEST['last_name']) && $_REQUEST['last_name'] != ''){
$lname = $_REQUEST['last_name'];
}
All the values you need to do whatever with are now in $option, $fname, and $lname
You say your new to php, welcome :), and don't ever trust user input. You should take some time and read about SQL Injection.
Hope some of this helps and good luck.
ON THE SECOND PAGE. In form make two hidden fields <input type="hidden" value="<?php echo $_REQUEST['first_name'] ?>" name="first_name"/> and
<input type="hidden" value=""<?php echo $_REQUEST['last_name'] ?>"" name="last_name"/>
my suggestion use post method
Basically, I've been working myself with something exactly like that, but I've found a great solution and a great approach in my own opinion.. let me give you some bit and piece of code as an example if you don't mind ^_^..
to be organised and clear what you did is : -
##**FIRST PAGE** : -
the url: - secondPage.php?first_name=hello&last_name=bye <--- this was the result..
Then you travelled to the 2nd page: - <-- result was still associated
##**SECOND PAGE** : -
$firstname = $_GET['first_name'];
$lastname= $_GET['last_name'];
<FORM ACTION="secondPage.php?first_name=<?php echo $firstname; ?>&last_name=<?php echo $lastname ?>" METHOD="POST">
<select name='selectedOption' >
<option value="op1">option1</option>
<option value="op2">option2</option>
<option value="op3">option3</option>
</select>
<INPUT TYPE="SUBMIT" name ="submit" VALUE = "Search">
</FORM>
##**THIRD PAGE** The page that the form is going to go to: -
$option = $_GET ['selectedOption'];
// you can also get the other data on this page through same method i've done on page two..
I am building a site on wordpress and I have encountered a problem that I am not quite sure how to solve.
The Situation:
User makes a selection from a <select> tag on one page and submits their choice
User arrives at a page featuring the same <select>. When the user selects an option, it displays content corresponding with that option using a little bit of jQuery.
-
$('.result').hide();
$('#servicearea').change(function() {
$('.result').hide();
var optionValue = $ (this).attr('value');
$('#'+optionValue).show('fast');
});
The Problem:
I need the selection from the first page to carry over to the second page then run this script.
Solutions:
I'll be honest, I don't know where to start with this, maybe cookies? I was hoping there was a jQuery type of solution as I am sort of comfortable with jQuery.
Any suggestions would be appreciated.
javascript doesnt know what you have posted to the page.
you can put the value into the javascript from php with something like this:
<script>
doSomething(<?=$_POST['selected'?>);
</script>
or you can put it into a hidden form field
<input type="hidden" id="hiddenField" value="<?=$_POST['selected']?>">
and catch the value with
$('#hiddenField').val()
Hidden form fields will work. But if you decide to go with PHP sessions instead, take a look here.
From the above link...
<?php
// page1.php
session_start();
echo 'Welcome to page #1';
$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();
// Works if session cookie was accepted
echo '<br />page 2';
// Or maybe pass along the session id, if needed
echo '<br />page 2';
?>
After viewing page1.php, the second page page2.php will contain the session data. Read the session reference for information on propagating session ids as it, for example, explains what the constant SID is all about.
<?php
// page2.php
session_start();
echo 'Welcome to page #2<br />';
echo $_SESSION['favcolor']; // green
echo $_SESSION['animal']; // cat
echo date('Y m d H:i:s', $_SESSION['time']);
// You may want to use SID here, like we did in page1.php
echo '<br />page 1';
?>
UPDATE
Judging by your snippet, I believe you would set the session variable by doing something like this:
<form action="" method="post">
<select type="select" name="servicearea" >
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['optionValue'] = $_POST['servicearea'];
}
echo $_SESSION['optionValue'];
?>
Hi just looking for some direction, I have a HTML form which has several fields and basically what I am wanting to do is save the data entered from the form to a PHP config file and also have the ability to edit the saved data by accessing and submitting the form again. I would prefer to do this without the use of a database.
So here's an example:
<form method="post" name="config_form">
<div id="field">
<label>Keywords</label>
<br />
<input type="text" name="keyword">
</div>
<br />
<select name="color">
<option value="green">Green</option>
<option value="orange">Orange</option>
<option value="blue">Blue</option>
<option value="red">Red</option>
</select>
</form>
So the user enters 'computer' as the keyword and selects the color 'blue'. I want to then save this data into my config.php file as variables allowing my other website pages to access this data. Should this all be in an array as well?
<?php
//config file
$keyword = "computer";
$color = "blue";
?>
Also when I go back access the form again can I make it so the fields are prefilled with the data from the config.php file?
Any help would be much appreciated thank you!
If you're dedicated to storing this sort of thing in a file, then probably the easiest way is to just store all the data in an array in the form of $keyword => $value then use the serialize() and unserialize() functions to transform them into a format that can be easily stored into and read from a file.
Keep in mind that if there is only one file, then a change made by one user will affect them all, so if that's not acceptable, then you'll need to come up with a way to determine the user and which file to use.
A much better way of doing this is to just store these values in a database. Create a table called options with two fields - option and value - which will store the configuration options. If you want different users to have their own options, then you could add another field - userid (as a foreign key to a users table) - to track which user an option pair applies to.
Further, if there are a predefined set of options a user can set, then you could have fields in the table for each option, with default values, and you can create a row for each user with the specific config options set in a single record for that user.
You can include your configuration file in your main php script file:
// main.php
<? php include("config.php"); ?>
and build the form with something like this:
// main.php
<?php
?>
<form method="post" name="config_form">
<div id="field">
<label>Keywords</label>
<br />
<input type="text" name="keyword">
</div>
<br />
<select name="color">
<option value="green" <? if ($color == "green") echo "SELECTED"; ?> >Green</option>
<option value="orange" <? if ($color == "orange") echo "SELECTED"; ?> >Orange</option>
<option value="blue" <? if ($color == "blue") echo "SELECTED"; ?> >Blue</option>
<option value="red" <? if ($color == "red") echo "SELECTED"; ?> >Red</option>
</select>
</form>
<?
?>
finally you can save the form data in your config.php file using fopen() and fwrite() functions on form submit:
$key = $_POST["key"];
$color = $_POST["color"];
if ($key != '' && $color != '') {
$f = fopen('config.php', 'w') or die("can't open file");
fwrite($f, '<?php $keyword=' . $key . ';$color=' . $color . ';?>');
fclose($f);
} else { // write default values or show an error message }
You can do this in multiple ways. Best way would be to use a database such as MYSQL. You are asking for persistence and that is what DBs are for. Try this.
$key = $_POST["key"];
$color = $_POST["color"];
mysql_query("INSERT INTO smeTbl VALUES ('1',$key,$color)");
THen in the config file or what ever other file you have you can retrieve these values.
$query = mysql_query("SELECT * FROM smeTbl WHERE id='1'");
$fetch = mysql_fetch_array($query);
$keyword = $fetch["key"];
$color = $fetch["color"];
This is just an example and you can refine it based on your needs
when you submit the form and you want to store the submitted form's data in one php file,ni must use the file action functions fopen the config.php,and write php code into it.
when you display the form,you can fopen the config.php,and use the function "eval" to get data.forgive my english.
//when submit form
$string = '<?php $keyword="computer";$color="blue";?>';
$fp = fopen('config.php', 'w');
fwrite($fp, $string);
fclose($fp);
//when display form
include("config.php");
//so you can use $keyword and $color
<select name="gamelist" id="gamelist">
<option value="1">Backgammon</option>
<option value="2">Chess</option>
</select>
<input type="submit" name="submit" id="submit" value="Submit" />
i want to grab the selected value and place in in a var
any idea?
thanks
Depends on your form tag.
<form method="post">
Will pass the value to $_POST['gamelist']
While
<form method="get">
Will pass the value to $_GET['gamelist']
Ofcourse, only after hitting the submit button. As morgar stated, this is pretty basic form procession. I doubt you've used google or followed a tutorial, this is almost one of the first things one learn when working with forms and PHP. We arent here to give you full solutions, take this as an example and create the full page yourself:
if($_SERVER['REQUEST_METHOD'] == "Y") {
$choice = $_Y['gamelist'];
// other stuff you want to do with the gamelist value
} else {
echo '<form method="Y" action="file.php">';
// the rest of your form
echo '</form>';
}
Replace Y with either GET or POST.
$choice = $_REQUEST['gamelist']; //works with get or post
$choice = $_POST['gamelist']
if it is a POST, or $_GET if not.
I have a drop down box which is pulling data from my database. when a user inputs data , should I still validate the drop down data on the server?
Yes. Always validate any information you are receiving from a client if you are storing, reading or performing some operation based on that data. Someone can always spoof a request not using a browser at all.
An easy way to validate it is...
<?php
$array = array(1 => 'a', 2 => 'b');
if ($_POST) {
if ( ! in_array($_POST['choose'], array_keys($array)) {
echo 'Invalid input';
}
}
?>
<form action="?" method="post">
<select name="choose">
<?php foreach($array as $value => $node): ?>
<option value="<?php echo $value; ?>"><?php echo $node; ?></option>
<?php endforeach; ?>
</select>
</form>
Which you must do, otherwise it may as well be a text input :)