TwoPHP blocks in file - php

i have two HTML forms and two PHP blocks in one file (index.php). for example i want the second php script belonged to the second form. i dont know, how to do it. What i write to the action atribute ?
here is my code:
<form method="post" action="htmlspecialchars $_SERVER ["PHP_SELF"]">
<input type="text" name="name"> <br>
<input type="submit">
</form>
<form method="post" action="htmlspecialchars $_SERVER ["PHP_SELF"]">
<input type="text" name="age"> <br>
<input type="submit">
</form>
<?php
echo $_POST ["name"];
?>
<?php
echo $_POST ["age"];
?>

Hope it helps you,
First form,
<form method="post" action="<?php echo $_SERVER ["PHP_SELF"];?> ">
<input type="text" name="name"> <br>
<input type="submit" name='submit' >
</form>
<?php
if(isset($_POST['submit'])){
echo $_POST ["name"];
}
?>
Second form
<form method="post" action="<?php echo $_SERVER ["PHP_SELF"];?>">
<input type="text" name="age"> <br>
<input type="submit" name='submitsecond' > // name submitsecond indicates as second form
</form>
<?php
if(isset($_POST['submitsecond'])){
echo $_POST ["age"];
}
?>

You can use a hidden input field to distinguish both scripts. And you'll have to echo/print the script name ($_SERVER['PHP_SELF']), htmlspecialchars is not needed...
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input type="hidden" name="form" value="name_form" />
<input type="text" name="name"> <br>
<input type="submit">
</form>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input type="hidden" name="form" value="age_form" />
<input type="text" name="age"> <br>
<input type="submit">
</form>
<?php if($_POST['form'] == 'name_form'): ?>
The name form is submitted.<br>
Name: <?php echo $_POST['name']; ?>
<?php endif; ?>
<?php if($_POST['form'] == 'age_form'): ?>
The age form is submitted.<br>
Age: <?php echo $_POST['age']; ?>
<?php endif; ?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER ["PHP_SELF"]); ?>">
<input type="text" name="name"> <br>
<input type="submit" name="name_sub">
</form>
<?php
if(isset($_POST ["name_sub"])) // check if name form is submit
echo $_POST ["name"];
?>

Related

How to put a string with a php tag into a php variable?

I want to include
action="<?php echo $_SERVER['PHP_SELF']; ?>"
in $render_form variable. But the following code does not work:
<?php $render_form = '<form method="post" action="<?php echo
$_SERVER['PHP_SELF']; ?>">
<input type="text" name="name">
<br>
<input type="submit" name="submit" value="Submit Form">
<br>
</form>';
?>
The following code (without php tag) works:
<?php $render_form = '<form method="post" action="">
<input type="text" name="name">
<br>
<input type="submit" name="submit" value="Submit Form">
<br>
</form>';
?>
If I understood you, this should fit your issue
<?php $render_form = '<form method="post" action="'. $_SERVER['PHP_SELF'] .'">
<input type="text" name="name">
<br>
<input type="submit" name="submit" value="Submit Form">
<br>
</form>';
?>

Read value from user via html ,php ,mysql

I should create a database table and for this thanks to php and html I get number of fields and fields name from the user.And I can not handle it I try to fill toKeep array but I can't do it.My goal is keep fields in toKeep and then create tables.
Please enter the number of fields
<form name="getNumber" action="<?php $_PHP_SELF ?>" method="POST">
<input type="text" name="number" >
<input type="submit" name="field" id="field">
</form>
<?php
$number=$_POST["number"];
$num=(int)$number;
$i=1;
$toKeep = array();
while ($i<=$num){
echo "please enter fieldname";
$fieldname = "field" . $i;
?>
<form name="X" action="<?php $_PHP_SELF ?>" method="POST">
<input type="text" name="<?php echo htmlspecialchars($fieldname);?>">
</form>
<?php
$toKeep[i]=$_POST['$fieldname'];
//echo $fieldname;
echo $toKeep[i];
$i=$i+1;
}
?>
<form name="X" action="<?php $_PHP_SELF ?>" method="POST">
<input type="submit" name="field" id="field">
<?php
if(isset($_POST['field'])){
echo $toKeep[0];
echo $toKeep[1];
}
?>
You have 2 typos there .
You need to change
$toKeep[i]=$_POST['$fieldname'];
to
$toKeep[$i]=$_POST[$fieldname];

can I use $_POST 2 times with different page?

can we use $_POST 2 times with different page ?
this example ..
action.php
<form action="next.php" method="post">
<input name="test" value="test" />
<button type="submit" name="test">submit</button>
</form>
next.php
<?php
$test=$_POST['test'];
?>
<form action="next1.php" method="post">
<input name="test1" value="<?php echo $test; ?>" />
<button type="submit" name="test1">submit</button>
</form>
next1.php
<?php
echo $test2=$_POST['test1']; >> in here i didnt get test1 value . why ?
?>
Within next.php and action.php you need to change your input type like as
<input type="text" name="test" value="test" />
<input type="text" name="test1" value="<?php echo $test; ?>" />
Within next.php and action.php you were missing the type attr of input tag
and you have same name attributes for submit and input within next.php need to remove or change the value from respective
<input type="submit" value="submit"/>
Below code i have tried my local server and it executed successfully.
action.php:-
<form action="next.php" method="POST">
<input type="text" name="test"/>
<input type="submit" value="submit"/>
</form>
next.php:-
<?php
$test = $_POST['test'];
echo $test;
?>
<form action="next1.php" method="POST">
<input name="test1" value="<?php echo $test; ?>" />
<input type="submit" value="submit"/>
</form>
next1.php:-
<?php
$test2=$_POST['test1'];
echo "------".$test2;
?>
Hope this will help.I think this is achieved your requirement.
If you want to do it within one file e.g. index.php then here is the code
<?php
if(isset($_POST['test'])){
if(isset($_POST['test_text'])){
echo 'Output from NEXT: '.$_POST['test_text'];
}
}
elseif(isset($_POST['test1'])){
if(isset($_POST['test_text1'])){
echo 'Output from NEXT1: '.$_POST['test_text1'];
}
}
?>
<table style="width:100%;">
<tr >
<td style="width:50%;">
<form action="" method="post" name="next">
<input type="text" name="test_text" value="<?php echo isset($_POST['test_text']) ? $_POST['test_text']:''; ?>" />
<button type="submit" name="test">submit test1</button>
</form>
</td>
<td style="width:50%;">
<form action="" method="post" name="next1">
<input type="text1" name="test_text1" value="<?php echo isset($_POST['test_text1']) ? $_POST['test_text1']:''; ?>" />
<button type="submit" name="test1">submit test1</button>
</form>
</td>
</tr>
</table>
I hope this will help.
You need to send the POST value with the second form again, e.g. in a hidden field, otherwise the value won't be sent.
Furthermore your submit button shouldn't have the same name as the input field you want the content from. So change the name of your submit button, e.g.:
action.php
<form action="next.php" method="post"> <input name="test" value="test" /> <button type="submit" name="submit">submit</button> </form>
next.php
<form action="next1.php" method="post">
<input name="test1" value="<?php echo $test; ?>" />
<button type="submit" name="submit">submit</button>
</form>

How pass variables for alternative if -syntax?

<?php
$a=3;
$dir= '/php/';
?>
<?php if($a ==3){ ?>
<form action="/php/form.php" method="post">
<p>Name: <input type="text" name="name" /></p>
<p>Age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
<?php } ?>
I want to pass $dir in form action ,like action=$dir.form.php. It's possible?
Yes it is possible you need to simply pass $dir to action Try
<form action="<?php echo $dir;?>form.php" method="post">
You can embed php tags anywhere in your html code. Rewrite your snippet like this:
<?php
$a=3;
$dir= '/php/';
?>
<?php if($a ==3){ ?>
<form action="<?php echo $dir."form.php" ?>" method="post">
<p>Name: <input type="text" name="name" /></p>
<p>Age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
Yes it is possible
<?php
$a=3;
$dir= '/php/';
if($a ==3){ ?>
<form action="<?php echo $dir.'form.php'; ?>" method="post">
<p>Name: <input type="text" name="name" /></p>
<p>Age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
<?php } ?>

How can i pass values from two html pages to php?

I have 2 html pages :
page1.html
<html>
<body>
<form action="page2.html" method="post">
Enter First name: <input type="text" id="text1">
<input type="submit" value="Next">
</form>
</body>
</html>
page2.html
<html>
<body>
<form action="test.php" method="post">
Enter Last name: <input type="text" id="text2">
<input type="submit" value="Submit">
</form>
</body>
</html>
Now, i would like to retrieve the value of text1 from page1.html and text2 from page2.html. How can i go about it?
Looks like you are looking for forms. See this tutorial http://www.w3schools.com/php/php_forms.asp. Post your form data from page1.php to page2.php. On page2.html you can access them via $_POST.
Please be aware: This is not a secure example, just for showing purposes! When you want to show user generated data in your frontend, please use sanitizing and validation http://www.php.net/manual/en/filter.examples.sanitization.php.
page1.php:
<form action="page2.php" method="post">
Enter first name: <input type="text" name="firstName"><br>
<input type="submit">
</form>
page2.php
<h1>Hey <?php echo $_POST['firstName']; ?></h1>
<form action="lastpage.php" method="post">
Enter last name: <input type="text" name="lastName"><br>
<input type="hidden" name="firstName" value="<?php echo $_POST['firstName']; ?>">
<input type="submit">
</form>
lastpage.php
<h1>Yo, my mate <?php echo $_POST['firstName']; ?> <?php echo $_POST['lastName']; ?>!</h1>

Categories