How to call php variable input text from another Php page - php

My question is how do I call input value of user selected which has show by php code from another php.
Normal simple way we get the input this way
$calendar_id_val = $_GET['calendar_id'];
and now it is not working:
For example Show.php, I have one form which show the values from Database and show the result with php variable with While Loop.
<input name="calendar_id" value="<?php echo $calendar_id;?>">
and when user is submit that form I will carry these user selected value and perform insert.php

While you are doing echo in the input use echo $calendar_id_val

You should use form like,
<form action="insert.php" method="get">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>"/>
<input type="submit" name="submit_id" value="Insert"/>
</form>
Insert.php
echo $calendar_id_val = $_GET['calendar_id'];

Does this answer your question?
<form action="insert.php" method="GET">
<input name="calendar_id" value="<?php echo $calendar_id;?>">
</form>
If you want to redirect the user back to show.php, add this to the end of your insert.php script
header('Location: show.php');
exit();

I suggest $_POST var like this:
<form action="insert.php" method="POST">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>" />
<input type="submit" name="submit" value="Submit" />
</form>
insert.php file:
echo $calendar_id = $_POST['calendar_id'];

Related

PHP isset not accepting form name

I am having an issue trying to get my form with a hidden input field to be recognized when I check if it isset. Not sure if it is because I am running the form in a loop or what. I do know when I click submit for any of the fields from the loop the value of the hidden field is registering properly, it is just somehow the 'form1' is not registering as isset. Any ideas?
<?php
if(isset($_POST['form1']))
{
echo $_POST['cid'];
} else {echo "nope!";}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" name="form1">
// gets array
$listchars = $user->getCharacterList($_SESSION['user_id']);
// loops through array
foreach($listchars as $chardata){
echo $chardata['name']; ?>
<input type="hidden" name="cid" value="<?php echo $chardata['cid'];?>">
<input type="submit" name="submit" value="submit">
<?php } ?>
</form>
The form name is not submitted
Use
<input type="submit" name="form_name">
or
<input type="hidden" name="form_name">

Can't get form value with PHP

I am trying to get the value of a form (text field) with _POST, and store it to a text file but it doesn't work. Here's my code:
HTML
<form>
<input type="text" name="test" id="test" value="">
<input type="button" onclick="location.href='example.com/index.php?address=true';" value="ODOSLAŤ" />
</form>
PHP
if (isset($_GET['address'])) {
$email = $_POST['test'];
$myfile = fopen("log.txt","a") or die("Error.");
fwrite($myfile, "\n".$email);
// this prints nothing
echo $email;
}
I can't get the value of that text field. Nor GET nor POST doesn't work for me. What am I doing wrong?
You are missing a post method in your form.
<form method="post" action="example.com/index.php?address=true">
<input type="text" name="test" id="test" value="">
<input type="submit" value="ODOSLAŤ" />
</form>
You also have to change the following line.
if (isset($_GET['address'])) {
With
if (isset($_POST['address'])) {
You want to post after triggering an action as FreedomPride mentioned
Conclusion
You want to declare for example a post methodif you know that you would like to post that data in the future.
As FreedomPride also mentioned :
You are using a GET, but if a user does not input anything your script won't work, there by it is recommended to use a POST
You are not submitting your form.
Change your code as follows....
<form method="post" action="example.com/index.php?address=true">
<input type="text" name="test" id="test" value="">
<input type="submit" value="ODOSLAŤ" />
</form>
You'll get what you want....
This is what you're missing as Tomm mentioned.
<form method="post" action="yourphp.php">
<input type="text" name="address" id="test" value="">
<input type="submit" name="submit"/>
</form>
In your PHP, it should be post if it was triggered
if (isset($_POST['address'])) {
$email = $_POST['test'];
$myfile = fopen("log.txt","a") or die("Error.");
fwrite($myfile, "\n".$email);
// this prints nothing
echo $email;
}
Explanation :-
In a form, an action is required to pass the action to the next caller with action. The action could be empty or pass it's value to another script.
In your PHP you're actually using a GET. What if the user didn't input anything. That's why it's recommended to use POST .

Passing Input values from one PHP page to another within hidden fields

$sql="SELECT vName,id FROM employee WHERE vName LIKE '%$my_data%' ORDER BY vName";
$result = mysql_query($sql);
if($result)
{
while($row=mysqli_fetch_array($result))
$hid='<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />';
echo($hid);
echo $row['vName']."\n";
}
How to pass the value of a hidden input field to another PHP script? I am using auto complete. how to pass the value auto complete page to index page
You have two options:
Sessions
PHP Sessions
Session support in PHP consists of a way to preserve certain data across subsequent accesses.
eg:
<?php
// Page1.php
session_start();
$_SESSION["key"] = "random value";
Then:
<?php
// Page2.php
session_start();
echo $_SESSION["key"];
// Output would then be ... random value
POST
Using the PHP $_POST
Taking what you currently have, you'd do:
<form method="post" action="somescript.php">
<input type="hidden" name="xyz" id="abc" value="<?=$row['id'] ?>" />
<button type="submit" name="submit" value="submitForm" />
</form>
Then on somescript.php if you do:
<?php
print_r($_POST);
You'll see an array with the data from your form, hidden value included
Create a form
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
And Get value on action_page.php
$_GET['xyz']
You enter your html code inside php code like this
<?php
while($row=mysqli_fetch_array($result))
{
?>
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
<?php
echo $row['vName']."\n";
}
?>

Form submit to another page and then review or write to a file

I have a script that users can submit certain data, and after a submit they can "review" the output and go back to previous page or submit to it again and there is my proble, the submitted page: How can I submit it again to write it to a file?
form.html
<form method="post" action="vaihe2.php">
<input name="laskuttaja" type="text" value="Nimi" size="25">
<input name="submit" type="submit" value="Lähetä" />
</form>
vaihe2.php
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
?>
So how can I post that $data to next page(vaihe3.php) with submit and let the script write it to a file. I know how php write file works but the post to third page is not working.
If you wat to go back, the secret is in the value of the input.
<input name="laskuttaja" type="text" value="<?php echo(isset($_POST['laskuttaja'])?$_POST['laskuttaja']:"Nimi";?>" size="25"/>
To 'save' data to the next page use $_SESSIONs. They're simple to use. Just remember everywhere you use them, you must have session_start(); on LINE 1! Can't stress that enough!
$_SESSION['data']=$data;
on your third page:
echo$_SESSION['data'];
More on sessions here.
In vaihe2.php
<form method="post" action="vaihe3.php">
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
echo "<input name=\"laskuttaja\" type=\"hidden\" value=\"".$laskuttaja."\" size=\"25\">";
?>
<input name="submit" type="submit" value="anything" />
</form>
Here you are passing laskuttaja as hidden field and on post will be available to you in third page.
Now data flow as per your requirement. User fills data in form.html -> reviews on vaihe2 and confirms -> gets written in vaihe3.
Could you post the form conditionally back to itself until validated by checkbox? the action would change to "vaihe3.php" ?
<form method="post" action="<?php if ($_POST["valid"]==1) {echo 'vaihe3.php';} ?>">
<input name="laskuttaja" type="text" value="<?php if ($_POST['laskuttaja']!=='') {echo '$_POST[laskuttaja]'} else {echo 'Nimi';} ?>" size="25">
<?php if (isset ($_POST['laskuttaja') && $_POST['laskuttaja']!=="") {
echo 'Please Confirm your answers: <input name="valid" type="checkbox" value="1" />'; } ?>
<input name="submit" type="submit" value="Lähetä" />
</form>
Otherwise, the mention above about CURL would be another option. Or - since your using PHP anyways, you could write the values of form submission to a session array and make them available to all pages until you empty the array.

passing value in hidden field from one page to another in php

I have a simple registration form.
I want to pass value entered in one page to other in a text field.
how to pass and access it from next page in php.
this is my first php page.
Thanks in advance.
You can add hidden fields within HTML and access them in PHP:
<input type="hidden" name="myFieldName" value="someValue"/>
Then in PHP:
$val = $_POST['myFieldName'];
If you're going to ouput this again you should use htmlspecialchars or something similar to prevent injection attacks.
<input type="hidden" name="myFieldName" value="<?=htmlspecialchars($_POST['myFieldName']);?>"/>
Suppose this the form input in page A
<form name="" action="" method=post enctype="multipart/form-data">
<input type="text" name="myvalue" value="">
<input type=submit>
</form>
In page B
In the page you want to get values put this code
<?PHP
foreach ($_REQUEST as $key => $value ) {
$$key=(stripslashes($value));
}
?>
<form name="" action="" method=post enctype="multipart/form-data">
<input type="text" name="myvalue" value="<?PHP echo $myvalue" ?>">
<input type=submit>
</form>
So yo can use or attach variable value to another form do what else you want to do
use following code, that should help you.
<form action ="formhandler.php" method ="POST" >
<input name = "inputfield" />
<input type="submit" />
</form>
on formhandler.php file yo need to enter following code to get the value of inputfiled.
$inputfield = isset($_POST['inputfield'])?$_POST['inputfield']:"";
// now you can do what ever you want with $inputfield value
echo($inputfield);

Categories