How to get php to pass a variable to a second page - php

Firstly apologies. I am a complete novice. I wrote a small database over 10 years ago on whatever versions of php and mysql. I have managed to get the mysql piece working now but what I cant do is pass a variable from a selection page to another page.
I want to choose a location and that variable I assign categorynum as the variable. I then want to hit submit and it open up a second page that will use the value of categorynum in another mysql search. However it doesnt pass over. if i hardcode and declate categorynum as a value it works so I am guessing its the passing from page 1 to page 2
PAGE 1
<form method="get" action="page2.php">
<select name="categorynum">
<?php
foreach ($catByNum as $num=> $name){
print "<option value=\"$name\">$name</option>\n";
}
?>
</select>
<input type="text" name="categorynum" value="Submit">
</form>
Page 2
will pull in the word 'Submit' from the Value= piece (This is the wording that is on the button of the form on page 1 for submitting the choice)
Sorry if its really obvious

In your current page, change the name of your submit button <input type="text" name="categorynumSubmit" value="Submit"> then inside the second page (page2.php), use $_GET["categorynum"] to get the categorynum value
EDITS: Note that type="submit" not text

Related

PHP. How to get $_POST value after table disappears from HTML

I am sorry for the title. That was my best shot to explain the situation with the least words.
I am trying to make a php program that has a html part with select and option. I am not using ajax or mysql. Just using a JSON file and xampp for apache.
if you select one of the options,`
if(isset($_POST["choice"]))
this php code will work in the html, and show a series of input boxes where you can type in what ever you want. Each option has an array within a JSON file.
So, I have put it in
$file[$_POST["choice"]]
`
and iterated it with a key => value. and shoved it in the input box. The value of the input box would be initially the value of the JSON file I called. I wanted the user to erase that text and type in their own. There could be several input boxes depending on the choice the user makes.
The name of the input box would be the KEY.
Then if you hit the edit button which is a input type submit, the series of input boxes will disappear.
I wanted to get the return with a
$_POST[KEY]
But, whatever I choose, the $_POST[KEY] will just return me the very first option of the select option html.
IS there a way I can solve this?
I need to get the corresponding array of the selected choice.
My goal is to get the values of the input box and update a JSON file.
<select name = "muscle">
<option value = "chest">Chest</option>
<option value = "back">Back</option>
<option value = "leg">Leg</option>
</select>
<br>
<input type="submit" name="choice" value="choose">
<br><br>
<?php if(isset($_POST["choice"])) : ?>
<h3> Current Workout Program </h6>
<?php
foreach ($program[$_POST["muscle"]] as $key => $val):
?>
<p><?= $key. ":" . $val;?></p>
<input type="text" name="<?=$key?>" value="<?=$val?>">
<?php endforeach;?>
<br><br>
<input type="submit" name="edit" value="edit">
<br>
</form>
<?php endif;?>
The iteration of the Key value above works fine.
But if I do a
if (isset($_POST["edit"])){
print_r($program[$_POST["muscle"]]);
}
After submission, It will give me the array for "chest" only.
As I understood your code, if you submit the mentioned form the $_POST would be as the following box:
//$_POST is like the following
[
"muscle" => "chest",
"choice" => "choose"
]
So in the result page, if(isset($_POST["choice"])) condition check would be always true, And the progress will go right.
I think on the destination page (eg. edit page) you have to add a hidden input as the following to make sure you tell the system which muscle you are editing:
<input type="hidden" name="muscle" value="<?= $_POST['muscle'] ?>">
Please note that the input type is hidden and it's not shown to the person who is working with the form.
Check the solution out and let me know if there are any other issues with the response.

need to paas value from one page to another in php

am fresh to coding, so please advice if i had done any mistake.
I am having 2 pages, one am using to input the value and the other need to display the value.i had created the input page with one text box and submit button, once i click submit button the text box content should go to the next page and display the value.
this is the second page where values should come and display
enter image description here
This is the first page am entering values
enter image description here, in this i had add one submit button
Kindly help and advice
thanks in advance
you can use session in php to do it, in the first php file:
session_start();
$_SESSION['var']=yourValue;
now in the second php file:
session_start();
echo $_SESSION['var'];
First write your form with with get method like
<form method="get" action="second.php">
<input name="text_field_name1" value=""/>
<input name="text_field_name2" value=""/>
</form>
than you form value pass second page with url.
You can get value from second page by echo $_GET['text_field_name1']; and echo $_GET['text_field_name2'];

Sending value from a form to a html tag counter

I am looking for a bit of code to do the following:
A form containing a single text field and a submit button, must send the value of the text field to a landing page that automatically counts how many html tags that this page contains.
E.g. if the text field states stackoverflow.com, the landing page should say (H1 tags = 20) with many more parameters to come.
How is this done? I know how to make a form, but I do not know how to make it send its value to the landing page.
<form action="landingpage.php/" method="post">
The URL
<input type="text" name="cf_name">
<input type="submit" value="Submit">
</form>
This piece of code is a perfect answer to your question.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
Type In Something: <input name="random-info" type="text" size="25">
<input type="submit" value="Submit">
</form> <br>
<?php
echo "You Typed: " . $_GET['random-info'];
?>
you get the method into the url, then you can use them on another page.
To access data from a form it depends on the method. Since your code shows a post message you simply access it in the php on the landing page by user $POST_['cf_name'].
To learn more you can check out:
http://w3schools.com/php/php_post.asp about the post method and http://w3schools.com/php/php_get.asp about the get method.
Also an invaluable source is php manual itself.
As far as counting the tags, not really sure what you are trying to achieve.
If you are counting the tags in the page you create, just make a variable and add to it each time you put that specific tag on the page.
Then you can put those values in a hidden field of the form to be passed into your landing page.

PHP Redirect based on form selections issue

I have setup a form that allows a user to make selections from drop-down lists, and clicking the submit button will redirect them to the appropriate page based off of their selections. Here is the PHP:
<?PHP
function redirect($where){
header("Location: $where");
}
if ($_REQUEST['os1'] == 'xp' && $_REQUEST['browser'] == 'ffx'){
redirect('http://mysite.com/index.php/prodemo/prodemo-categories/73-prodemo-xp- firefox');
}elseif($_REQUEST['os1'] == 'xp' && $_REQUEST['browser'] == 'ie8'){
redirect('http://mysite.com/index.php/prodemo/prodemo-categories/72-prodemo-xp-ie');
}elseif($_REQUEST['os1'] == 'win7' && $_REQUEST['browser'] == 'ie8'){
redirect('http://mysite.com/index.php/prodemo/prodemo-categories/74-prodemo-win7-ie');
}elseif($_REQUEST['os1'] == 'win7' && $_REQUEST['browser'] == 'ffx'){
redirect('http://mysite.com/index.php/prodemo/prodemo-categories/75-prodemo-win7- firefox');
}
?>
I am manually telling the query where to take the user.
My dilemma is that I want a simple line that says:
"Hi, _. How can I help you?"
The blank would be filled in by the name that is inputted on the form as the Caller's name.
Here is an example of the form:
<form action="" method="post" name="form1">
Caller's Name: <input type="text" name="callersname" /><br />
<select name="os1"> <option selected="selected" value="xp">XP</option>
<option value="win7">Win7</option> </select> <br />
<select name="browser"> <option selected="selected" value="ie8">IE8</option>
<option value="ffx">Firefox</option></select> <br />
<input type="submit" value="Next" />
I'm very new to PHP. If I create a PHP page to post the caller's name, then how do I avoid the page redirecting to that php page instead of the redirect I have manually setup with the drop-down menu options selected?
Thanks for any advixce
If you want to store the user's name on a page other than the first page or the same page that is processing the form values (IE the script that's getting the redirect) you want to store that information either in a database, or more simply in a session.
First, initialize the session on each page where you need to pull that information from:
session_start();
Then, create a session variable (unique to every user/browser) like so:
$_SESSION['user_name'] = $_REQUEST['callersname'];
Now, on the page you wish to retrieve their name (IE Hi ) just do so like this:
echo "Hi {$_SESSION['callersname']} welcome to mcdonalds how may I help you?";
In HTML a form will submit to the url that is given in the value of the action attribute of the form tag. In your example above you have:
action=""
This tells the browser to submit the form to the current page.
It sounds like you are trying to do two somewhat unrelated things in the same step. Are you trying to get the caller's name or have them choose what url to go to? If you want to do both, you may be better off by just accepting the os1 and browser values as POST variables, and dynamically changing the page based on the value of those variables rather than redirecting the user to entirely separate pages.
Try using $_POST instead of $_REQUEST... It may be the problem and too, add action="yourpage.php" to tell the code to send the form to the current page or another page...

Search MySQL with PHP and display results on the same page

This is definately a novice question, but if you could be of any help i would be very grateful.
Basically, i'm building a database management page, and it of course includes a search function.
So, the search form looks something like this
<form name="name" function="search.php" method="get">
But, whenever i use it, i will of course get redirected to search.php. What i want is a way to display the results on the same page i did the search from (let's say index.php), without having to build an entire identical page around search.php
Thankful for answers.
Use a hidden field in the form that indicates that the form has been submitted.
In your form page (e.g. index.php)
<form name="name" action="index.php" method="post">
{OTHER_FORM_FIELDS}
<input type="hidden" name="doSearch" value="1">
</form>
So in your php code (could be in the index.php page or in a php script included)
<?php
if($_POST['doSearch']==1) {
//query database
//get results
} ?>
in your index.php page
<?php if($_POST['doSearch']) { //a search request was made display my search results ?>
HTML_CODE
<?php } ?>
Let the page submit to itself:
<form name="name" function="index.php" method="get">
In the handler for the page, check whether or not you have parameters and display either the input box or the results as appropriate.
You could even take it one step futher. You could use AJAX to insert the results directly into the page content when the submit button is pressed, rather than causing a page refresh.

Categories