Getting button input - php

Why does this not work. I have look at many sites and this works I look long time. Fine no no.
Edited code to this it echo pressed when I havent even pressed the button
<form method='POST' name="form1" action="index.php">
Username: <input name="username"><br>
Password: <input name="password"><br>
<input type = 'submit' name = 'submit' value = 'click me'>
</form>
<?php
if (isset($_POST['submit'])){
echo "Pressed button";
}
?>

You missed the tag <form></form>. Therefore it is not working

you need to add form tag and in action tag of form give your php page name. Here is index.php but you need to specify your own script name.
<form name="form1" method="post" action="index.php">
Username: <input name="username"><br>
Password: <input name="password"><br>
<input type = 'submit' name = 'submit' value = 'click me'>
</form>
<?php
if (isset($_POST['submit'])){
echo "Pressed button";
}

You need summat like
<form method="post" action="myscript.php">
etc

Your code is running even without closing form tag. But below is the formal way to use form.
<form method='post' action="">
Username: <input name="username"><br>
Password: <input name="password"><br>
<input type = 'submit' name = 'submit' value = 'click me'>
</form>
<?php
if (isset($_POST['submit'])) {
echo "Pressed button";
}
?>

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">

How to check if form was submitted

My question is how to check which form was submitted if I have 2 different forms with the same name, and I can't change the name.
For example I have 2 forms and 2 php scripts that evaluate them:
<form action=mypage.php method=post>
<input type=text name=name>
<input type=submit name=ok value=ok>
</form>
<?php
if(isset($_POST['ok'])) {
$name=$_POST['name'];
}
?>
<form action=mypage.php method=post>
<input type=text name=pass>
<input type=submit name=ok value=ok>
</form>
<?php
if(isset($_POST['ok'])) { // <- here is wrong
$pass=$_POST['pass]']; // this code is not executing
}
?>
How I can differentiate these two submits without changing their names?
P.S I can't bring the (Forms) together.
One solution is to add a hidden input on both forms:
<input type="hidden" name="form1" value="name" />
and:
<input type="hidden" name="form2" value="pass" />
Then to check which one has been submitted:
if(isset($_POST['ok']) && isset($_POST['form1'])){
// For the first form
}
And:
if(isset($_POST['ok']) && isset($_POST['form2'])){
// For the second form
}

PHP - cannot get value when input exists

I'm trying to use POST to get values from a form. I moved the part out to a test page:
<form name="form" action="" method="post">
<input type="text" name="subject" id="subject" value="enter something" />
</form>
// following that
<?php
if (isset($_POST["subject"]))
echo $_POST["subject"];
else
echo "input is not set";
?>
The echo is always "input is not set" regardless I set the value of input or not. And the tag "subject" does exist. This confused me. Why can't I get the value?
You should seperate your HTML form from your $_POST processing. This means that you provide the user a page which contains your posted form and then submit of this form is posted to a (different) page.
For example your form is (on form.php):
<form id="form" action="process_form.php" method="post">
<input type="text" name="subject" id="subject" value="" />
<input type="submit" value="Submit">
</form>
Then you create a file process_form.php:
<?php
if (isset($_POST['subject'])) {
echo $_POST['subject'];
}
else {
echo 'input is not set.';
}
?>

Retrieve $_POST for two pages

I need to save my submit form data like:Name for two pages...
For some reason the $_POST only saves data for the "action" page, but cannot be retrived after the action page.
Here's my code:
HTML (form):
<html>
<body> <form name="input" action="staff.php" method="post">
Username: <input type="text" name="Name">
<input type="submit" value="Submit">
</form>
</body>
</html>
Here's the next page after submiting and it works... (staff.php)
<html>
<?php
session_start();
echo "You have choosen". $_POST['Name']; // it shows what you've choosen...
?>
<form name="input" action="staff2.php" method="post">
Age: <input type="text" name="Age">
<input type="submit" value="Submit">
</form>
</html>
Ok and after age submiting Name and Age stop working... (staff2.php)
Here's the code:
<?php
session_start();
echo "You have choosen".
$_POST['Name']; //it does't show Name.. Please help!
$_POST['Age']; // it doesnt't show this either..
?>
Obviously, there is nothing wrong on the first page. So don't change anything.
The second page. The post works. Then add a hidden input to preserve it and carry it on the next one:
<?php
echo "You have chosen: ". $_POST['Name']; // it shows what you've choosen...
?>
<form name="input" action="staff2.php" method="post">
Age: <input type="text" name="Age">
<input type="hidden" name="Name" value="<?php echo $_POST['Name']; ?>" /> <!-- this one -->
<input type="submit" value="Submit">
</form>
On the third and final page. Properly concatenate the variables:
echo 'You have chosen: <br/>';
echo $_POST['Name'] . '<br/>'; // this should carry the hidden input you set on the last page
echo $_POST['Age'];
//^^ you forgot the echo
as you have a session running pass them as session variables.
$_SESSION['name'] = $_POST['name'];

How to call php variable input text from another Php page

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'];

Categories