Good Day!.
I need a kinda little help for PHP. I’m really really newbie for PHP and I already search it to google and can’t find any solution.
My Problem is:
I want to get the value of textbox1 then transfer it to another page where the value of textbox1 will be appeared in the textbox2.
Below is my codes for PHP:
<html>
<body>
<form name='form' method='post' action="testing2.php">
Name: <input type="text" name="name" id="name" ><br/>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
I also add the code below and the error is “Notice: Undefined index: name”
<?php
$name = $_GET['name'];
echo $name;
?>
or
<?php
$name = $_POST['name'];
echo $name;
?>
Your method is post, so use $_POST
Also, try wrapping it around an isset function:
if (isset($_POST['name'])){
echo $_POST['name'];
}
This will also handle the undefined error
This would be your textbox2:
<input type="text" name="textbox2" id="name" <?php echo (isset($_POST['name']) ? 'value=' .htmlspecialchars($_POST['name']). ' : ''); ?>/>
If you want to use the GET method, change post to get in your form and $_POST to $_GET in the php code of textbox2
Related
I am working on a PHP project - I had one form post a date to another form
I made some changes (although none to the input in question)
Now all other inputs are updated with their Posted values, except the date
If I manually set the date in HTML it works:
<div><input type="date" class="form-control" id="DateCourse" name="DateCourse" value="2009-01-01"></div>
If I set it to the following, it doesn't:
<div><input type="date" class="form-control" id="DateCourse" name="DateCourse" value="<?php echo (isset($DateCourse))?$DateCourse:'';?>"></div>
The below:
$DateCourse = ($_POST["DateCourse"]);
var_dump($_POST["DateCourse"]);
var_dump($DateCourse);
Returns:
string(10) "2019-01-05" - means the post value is set
Notice: Undefined variable: DateCourse in /home/bitecons/bts.biteconsulting.co.za/v2/editccr.php on line 119 - how can it be undefined, I just defined it
NULL
What on earth am I doing wrong? Apart from using PHP :P
Flow as requested:
Records.php:
This is the function to prepopulate my posted fields:
function Prefill(x) {
TabletoEdit = x.closest('table').id;
SelectedRow = x.rowIndex;
document.getElementById("EntryEditing").value = x.cells[19].innerHTML;
document.getElementById("DateCourse").value = x.cells[0].innerHTML;
document.forms["records"].submit();
}
Then I also have:
<form action="editrec" method="post" id="records">
<input type='hidden' name='Period' id='Period' />
<input type='hidden' name='Month' id='Month' />
<input type='hidden' name='res' id='res' />
<input type='hidden' name='CustName' id='CustName' />
<input type='hidden' name='DateCourse' id='DateCourse' />
</form>
The Prefill gets called, then submits the form
I have tracked and DateCourse has data, but when getting to the other form, it "disappears":
if(!empty($_POST)) {
$DateCourse = ($_POST["DateCourse"]);
$CustName = ($_POST["CustName"]);
}
For example, CustName is filled in, but not DateCourse?
Side question:
Would this return true if another post var is not set (unrelated to this one):
if(!empty($_POST))
I think You use wrong Code
you Must Submit First Form And Then Use $DateCourse this In another Form in POSTBACK
One of the best way is to define $DateCourse too like
<?php
$DateCourse = "";
if(!empty($_POST["DateCourse"])) {
$DateCourse = ($_POST["DateCourse"]);
}
?>
<div><input type="date" class="form-control" id="DateCourse" name="DateCourse" value="<?php echo $DateCourse;?>"></div>
Okay, apologies folks, but it may help others in the future.
I had a function call to an old function - this failed, causing my variable to never get defined... I knew it was something stupid, but sometimes one needs a sound board...
I just sent data to a page called diak_o.php with post method but I need to use this data on an another page. How can I send it to two pages or send from the first page to the next?
<form action="diak_o.php" method="post">
<input type="text" name="name"><br>
<input type="submit" value="Bejelentkezés" />
</form>
You could store it in Sessions and access it on multiple pages like this:
Page 1:
<form action="page2.php" method="post">
<input type="text" name="page1text"/>
<input type="submit"/>
</form>
Page 2:
<?php
session_start();
$dataFromPage1 = $_SESSION['page1text'] = $_POST['page1text'];
echo $dataFromPage1;
?>
You can use $_SESSION or just but i think $_POST should still work in the next file too...
when you send that data from this page to second page like diak_o.php in this page you can access it by below code.
in diak_o.php write code like below.
<?PHP
session_start();
echo $_POST['name'];
$_SESSION["name"] = $_POST['name'];
?>
in the other page you can use $_SESSION["name"] by accessing it.
you can also use COOKIE OF PHP.
On this URL there are different methods for passing data from one page to another.
http://www.discussdesk.com/how-to-get-data-from-one-page-to-another-page-in-php.htm
Thanks.
You need to give your button a name attribute, then on diak_o.php you check if the button isset, after that you check if the text input is not empty, else assign a session to the text input
Your Form
<form action="diak_o.php" method="post">
<input type="text" name="name"><br>
<input type="submit" value="Bejelentkezés" name="submit" />
</form>
diak_o.php
<?php
session_start();
if(isset($_POST['submit'])){
if(empty($_POST['name'])){
die("enter name");
}else{
$_SESSION['name']= $_POST['name'];
}
}
?>
anotherpage.php
<?php
session_start();
echo $_SESSION['name']; // will output the value from form.
?>
when ever your wanna use the value on your pages, just use $_SESSION['name'];
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I'm trying to study php and I'm already on the sessions part where I want to input something on my first page that would then be an output for the second page
1stpage.php
<?php session_start();?>
<form method="post">
Enter a number: <input type="number" name="num1" min="1" max="20" value="<?php echo $_SESSION["$num1"];?>">
<a href ="2ndpage.php">
<input type="button" name="select" value="select">
</a>
</form>
2ndpage.php
<?php
session_start();
echo $_SESSION[$num1];
?>
Well, it does'nt work and I'm getting lots of undefined index error. Any fix guys? Thanks in advance.
Take a look at form handling:
http://www.w3schools.com/php/php_forms.asp
If you still want to save the value into a session use:
$_SESSION['num1'] = $_POST['num1'];
1stpage.php
<?php session_start();?>
<?php if(isset($_SESSION['num1'])) $num1 = $_SESSION['num1']; else $num1 = ''; ?>
<form method="post" action="2ndpage.php">
Enter a number: <input type="number" name="num1" min="1" max="20" value="<?php echo $num1;?>">
<input type="submit" name="select" value="select">
</form>
2ndpage.php
<?php
session_start();
$_SESSION['num1'] = $_POST['num1'];
echo $_SESSION['num1'];
?>
What I have done here is I have first looked into the session if the num1 offset has been set into the session. If its not then I give $num as blank else I set the assign the value from session to $num.
Now when we input something and post it on 2nd page, the posted value is assigned to the variable in session and is displayed as well. So that next time you visit 1stpage.php while in same session, you will see your last posted value.
Let us know if it solves your practive problem or if this is not what you wanted.
I believe this is what you're trying to do:
Page 1:
<?php
session_start();
if(isset($_POST['select'])){
$num1 = $_POST['num1'];
$_SESSION['num1'] = $num1;
}
?>
<form method="post">
Enter a number: <input type="number" name="num1" min="1" max="20" value="<?php echo $_SESSION['num1']; ?>">
<input type="submit" name="select" value="select">
Click here to See the Value
</form>
Page 2:
<?php
session_start();
echo $_SESSION['num1'];
?>
Where, at first you press the select to enter the value inside
num1 then click on Click here to See the Value to see it.
I don't think you understand how all this works. You're having PHP output a session variable into a text box and expecting that that text box will somehow magically update your session. Somewhere you have to pass that data back to PHP and update it. So let's make your code into a process
<?php session_start(); ?>
<form method="post" action="process.php">
Enter a number: <input type="number" name="num1" min="1" max="20" value="<?php echo $_SESSION["$num1"];?>">
<input type="submit" name="select" value="select">
</form>
What I've done is make this form do a basic POST. If you want to avoid this you can use AJAX instead (same result using JS instead of HTML)
Next, let's make a basic process.php intermediate page
<?php
session_start();
$_SESSION['num1'] = (int)$_POST['num1'];
header('Location: 2ndpage.php');
Now we have a logistics chain for the data. You accept the data in your HTML and POST it to this page. It then takes it, sanitizes it (note I cast it to an (int) so we are certain this is a number) and then issues a redirect to the browser which takes you to your original landing page, which should now work.
This is all oversimplified for the process of teaching. You could have your form submit directly to 2ndpage.php and process there. Or, as I said before, you could use AJAX to send to process.php and when it returns some success parameter you then use JS to redirect. You have many options here.
Put this on the first page:
$_SESSION['num1'] = $num1;
and this on the second
$num1 = $_SESSION['num1'];
Let me explain more in details what I am trying to do.
Okay so, I am doing a huge form to send in a particular e-mail. My problem right now is that only my radio buttons in my form show an error when the form is submit and return false. Here's an example of what I am talking about :
<?php
if (isset($_POST['submit'])) {
$radio = $_POST['number'];
$name = $_POST['name'];
}
?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
<input type="radio" name="number" value="allo">
<input type="radio" name="number" value="yo">
<input type="text" name="name">
<input type="submit" name="submit">
</form>
So as you see, if the customer press on "submit" and didn't filled values inside of radio and input field, $name will show no error but $radio will show an error.
My Error :
Notice: Undefined index: number in D:\xampp\htdocs\test\preg.php on line 5
What you are getting is the server warning and not an actual error.
To avoid it you have flags that you can set to ignore warning, but this isn't a good practice.
I would suggest you to check if the form are set and handle them if not.
Code:
<?php
if (isset($_POST['submit'])) {
$radio = (isset($_POST['number']) ? $_POST['number'] : "");
$name = (isset($_POST['name']) ? $_POST['name'] : "");
}
?>
Edit: Of course, lets not forget, the better practice will be handle them against malicious code.
You get that error because $_POST['number'] is not passed to the page.
You should set at least one of the radio buttons, and if you want to be sure of not getting an error even if the users modifies the page, you can check with an isset as others as wrote in the other answers.
If you were wondering why you didn't get any error from the name, that's because it contains the empty string "".
Check this code and view the source of the page to see what print_r writes in a formatted way
<?php
if (isset($_POST['submit'])) {
$radio = $_POST['number'];
$name = $_POST['name'];
}
print_r($_POST)
?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
<input type="radio" name="number" value="allo" checked="true">
<input type="radio" name="number" value="yo">
<input type="text" name="name">
<input type="submit" name="submit">
</form>
You can write
if (isset($_POST['name'])) $name = $_POST['name'];
but even better is
isset($_POST['name']) ? $name = $_POST['name'] : $name = '';
if (isset($_POST['number']))
{
$radio = $_POST['number'];
}
I am just starting to learn php, how would I initiate a echo statement after a submit button is pushed, or even a anchor tag.
Here is my code so far
form name="myform" method="get" actions="madlib01.php"
Name: <input type="text" name="name" /> <br />
<input type="submit" name="submit" />
form
<?php
$Name = $_GET['name'];
$hello .= "Hello $Name";
echo $hello //I would prefer the echo to happen after the submit button is hit
?>
the correct attribute for your form tag is "action", not "actions"
When the form is submitted, a new request is sent to the server (in your case, using GET).
So to do it all in one page:
form.php:
<form action="form.php" method="GET">
<input type="text" name="name"/>
<input type="submit">
</form>
<?PHP
if (! empty($_GET['name'])){
echo 'Hello, ' . $_GET['name'];
}
?>
You will first need to check if PHP has received your GET parameter using isset or array_key_exists:
if(isset($_GET['name']) && !empty($_GET['name'])) {
$Name = $_GET['name'];
echo "Hello $Name";
}
or:
if(array_key_exists('name', $_GET) && !empty($_GET['name'])) {
$Name = $_GET['name'];
echo "Hello $Name";
} else {
//example: default to something if nothing has been passed
echo "Hello Guest";
}
Also note, if you're submitting to the same page, you can omit the action attribute from your form tag altogether:
<form method="GET">
echo $hello
You've just gained an HTML-injection vulnerability. If someone sends your user to:
http://www.example.com/madlib01.php?name=<script>stealYourCookies()</script>
you've got problems.
Yes, this is a My First PHP Script. That doesn't make security optional. This is a mistake every tutorial makes: teaching bad practice from the start, treating correctness (and security, which is a subset of correctness) as an optional extra.
The result is that most PHP code out there is full of holes. But there's no need for yours to be! Every time you place a pure-text string into a surrounding HTML context, escape it properly:
echo htmlspecialchars($hello);
I tend to define a function with a shorter name than ‘htmlspecialchars’ to do that for me, as I'm lazy.
<?php
function h($text) {
echo(htmlspecialchars($text, ENT_QUOTES));
}
$name= '';
if (isset($_REQUEST['name']))
$name= trim($_REQUEST['name']);
?>
...
<?php if ($name!=='') { ?>
<p> Hello, <?php h($name); ?>! </p>
<?php } ?>
<form method="get" action="madlib01.php">
<p>
<label for="namefield">Name:</label>
<input id="namefield" type="text" name="name" />
</p>
<p>
<input type="submit" />
</p>
</form>
Now if you say your name is Mister <script>, the page will greet you exactly as such, angle brackets and all, instead of trying to run JavaScript. This is the correct output and thus also secure.