calling php function from submit button - php

I want to pass the value of input field address to php. So that when i click on submit button it will be store in php.
<input type="text" name="address">
<input type="submit" name="go" method="post">
<?php
if($_POST){
if(isset($_POST['go'])){
call();
}else{
echo "Error";
}
}
function call(){
echo "hi";
print(document.address.value);
}
?>
However when i click on button it response nothing.

You are mixing PHP and JavaScript. If you want a pure PHP solution you could do something like:
<?php
if(isset($_POST['go'])){
$address = $_POST['address'];
echo $address;
}
?>
<form method="POST">
<input type="text" name="address">
<input type="submit" name="go" method="post">
</form>
With PHP once the form is submitted you can access form values with $_POST so use $_POST['address'] instead of document.address.value

what you intend to do can be done as below method.
<form method="post" >
<input type="text" name="address">
<input type="submit" name="go">
</form>
<?php
if($_POST){
if(isset($_POST['go'])){
call();
}else{
echo "Error";
}
}
function call(){
echo "hi";
echo $_POST['address'];
}
?>

Try like this,this is working fine as per your requirement:
<html>
<body>
<form action="" method="post">
Address: <input type="text" name="name"><br>
<input type="submit" name="go" value="call" />
</form>
<?php
if (isset($_REQUEST['name'])) {
call();
}
?>
<?php
function call() {
$address= $_POST['name'];
echo "Address:".$address;
exit;
}
Output:-
For output Click here

use this code
<input type="text" name="address">
<input type="submit" name="go" method="post">
<?php
if($_POST){
if(isset($_POST['go'])){
call();
}else{
echo "Error";
}
function call(){
echo "hi";
echo $_POST['address'];
}
?>

Related

the server request method prints GET even though the form method is POST

i wrote this code below and when i submit the form the $_SERVER["REQUEST_METHOD"] prints GET instead of POST even though the form method = "POST"
<?php
include "init.php";
include $temp . "navbar.php";
echo $_SERVER["REQUEST_METHOD"];
if($_SERVER["REQUEST_METHOD"]=='POST'){
echo $_POST["name"];
}
?>
<input type="hidden" value="admin" class="title">
<form action="index.php" method="POST">
<input type="text" name="name">
<input type="submit" value="ok">
</form>
<?php
include $temp . "foot.php";
?>
You can simply check if $_POST['name'] is set:
if(isset($_POST['name'])) {
// ...
}
And since you are posting to the same page you don't need to include the action attribute in your form.
<?php
include "init.php";
include $temp . "navbar.php";
if(isset($_POST['name']) {
echo $_POST['name'];
}
?>
<input type="hidden" value="admin" class="title">
<form method="POST">
<input type="text" name="name" value="" />
<input type="submit" value="ok">
</form>
<?php
include $temp . "foot.php";
?>

how to refill data after reloading page in php

I'm stuck at one point .I have a php form and after filling all details, user have to press submit button . If data does not submitted properly then I am reloading the page .
so My question is .
I want to set all previous values entered by user into respected
fields without request method.
how can i achieve this?
What I have done so far
My user.php
<?php
$name = '';
if(isset($_REQUEST[name]))
{
$name = $_REQUEST[name];
}
if(isset($_POST["submit"]))
{
$name = $_POST["txtname"];
header('Location:user.php?name=<?php echo $name; ?>');
}
?>
<hrml>
<body>
<form name="form1" id="form1" method="POST">
<label>Name:</label>
<input type="text" name="txtname" id="txtname" value="<?php echo $name; ?>">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
EDIT
apply_now.php
<?php
$CLASS__BL = 'apply_now_bl';
require_once('apply_now_bl.php');
?>
<form name="form1" id="form1" action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="input-label">Full Name: *</label>
<input name="txtname" id="txtname" type="text" class="form-control" value="<?= #$page_bl->full_name; ?>">
</div>
</form>
apply_now_bl.php
<?php
require_once('admin/db_lib/candidates.php');
require_once('includes/general_include.php');
$page_bl = apply_now_bl();
page_load();
class apply_now_bl
{
var $full_name = '';
function page_load()
{
$this->obj_candidates = new candidates();
if(isset($_POST["submit"]))
{
$this->submit_data();
}
}
function submit_data()
{
$this->full_name = get_post_val("txtname");
$this->obj_candidates->full_name = $this->full_name;
redirect(apply_now.php); //common function for page redirect
}
}
No need to redirect through php header location . because action is blank and that means it will redirect to user.php(current page) once form submit.
so below is the updated code
<?php
$name = "";
if (isset($_POST["submit"])) {
$name = $_POST["txtname"];
}
?>
<html>
<body>
<form name="form1" id="form1" method="POST">
<label>Name:</label>
<input type="text" name="txtname" id="txtname" value="<?php echo $name; ?>">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

scope of $_POST[] , trying to access $_POST[] variable, getting the undefined index error

I have these two forms that submits to different blocks of the same script. i am unable to access variable of one block in other, though both variables are in same script.
html form :(1.php)
<html>
<form method="POST" action="2.php" enctype="multipart/form-data">
</br>
Choose a user name:</font>
<input type="text" name="username">
<input type="submit" name="submit" value="Save and Proceed">
</form>
</html>
2.php:
<?php
$name=$_POST['username'];
if ((isset($_POST['username'])) && ($_POST['submit'] == 'Save and Proceed'))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
?>
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
enter age:</font>
<input type="text" name="age">
<input type="submit" name="submit" value="done">
</form>
</html>
<?php
}
}
if((isset($_POST['age'])) && ($_POST['submit'] == 'done'))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "hi" .$name;
echo "your age is ". $age;
echo"you are eligible";
}
}
?>
How do I access $_POST['username'] in the code following the html form in the same script? Thank you in advance.
On the second form you can use a hidden input, i.e.:
<input type="hidden" name="username" value="$name">
Example:
<?php
$name=$_POST['username'];
if (!empty($_POST['username']) && $_POST['submit'] == 'Save and Proceed'))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
echo <<< LOL
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
enter age:</font> <input type="text" name="age">
<input type="hidden" name="username" value="$name">
<input type="submit" name="submit" value="done">
</form>
</html>
LOL;
}
}
if((isset($_POST['age'])) && ($_POST['submit'] == 'done'))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "hi" .$name;
echo "your age is ". $age;
echo"you are eligible";
}
}
?>
If I understand well you want to pass username twice.
Than you can use hidden input, which is not visible (only transports data):
<input type="hidden" name="username" value="<?php echo $_POST['username']; ?>" />

php POST array not set

i am trying to collect data from the user in a form and display the data back to him .
i am using WAMP.
here is my html code
<FORM METHOD="POST" ACTION="submit.php">
<INPUT type="text" name="URL" size=17 value="http://">
<INPUT type="text" name="user" size=17>
<INPUT type="text" name="email" size=17>
<INPUT type="submit" value="Submit" name="submit"/>
<INPUT type=reset value="Clear">
</form>
here is my submit.php code
<?php
if (isset($_POST['URL'])){
echo "set";
}
else
{
echo "not set";
}
?>
when i execute this i am always getting "not set" as the output.
thanks.
It should be $_POST not $post, so your code should be :-
<?php
if (isset($_POST['URL'])){
echo "set";
}
else
{
echo "not set";
}
?>
Also your form tag should not contain encytype="text/plain" because PHP doesn't handle it (and it is not a bug)
Valid values for enctype in <form> tag are:
application/x-www-form-urlencoded
multipart/form-data
So remove encytype="text/plain"
<?php
if (isset($_POST))
{
echo "set";
}
else
{
echo "not set";
}
?>
try this
Remove
ENCTYPE="text/plain"
from the form
So the form should look like
<form method="POST" action="submit.php">
And its $_POST not $POST
if (isset($POST['URL'])){
should be
if (isset($_POST['URL'])){
Here is an example of handling the form
<?php
if(isset($_POST["submit"])){
if (isset($_POST['URL']) && $_POST['URL'] != ''){
echo "set";
}
else
{
echo "not set";
}
}
?>
<FORM METHOD="POST" ACTION="submit.php" >
<INPUT type="text" name="URL" size=17 value="http://">
<INPUT type="text" name="user" size=17>
<INPUT type="text" name="email" size=17>
<INPUT type="submit" value="Submit" name="submit"/>
<INPUT type=reset value="Clear">
</form>

value preserving across multiple pages

i am trying to post the some data on another page at this i have a back button onclick of back button i wanna reload the original page with posted data.
use $_SESSION:
Page1.php
<?php
session_start();
$name = isset($_SESSION['name']) ? $_SESSION['name'] : '';
?>
<html>
<form method="post" action="Page2.php">
<input type="text" name="name" value="<?php echo $name; ?>" />
<input type="submit" value="Submit" />
</form>
</html>
Page2.php
<?php
session_start();
if (isset($_POST['name']))
{
$_SESSION['name'] = $_POST['name'];
}
$name = $_SESSION['name'];
?>
<html>
<form>
<span><?php echo $name; ?>" </span>
Back
</form>
</html>
you can use $_SESSION
<?php
if (!isset($_SESSION)) session_start();
if ($_POST['btnsubmit']){
$_SESSION['data1'] = $_POST['data1'];
$_SESSION['data2'] = $_POST['data2'];
$_SESSION['data3'] = $_POST['data3'];
//... and so on
}
?>
<form>
<input type="text" id="data1" value="<? echo $_SESSION['data1'];?>"/>
<input type="text" id="data2" value="<? echo $_SESSION['data2'];?>"/>
<input type="text" id="data3" value="<? echo $_SESSION['data3'];?>"/>
<input type="submit" id="btnsubmit" value="Submit"/>
</form>
<!-- and so on -->
Hope this gives you an idea

Categories