show form php if statement true - php

<?php
if($dun==0){
show form1 and add vaules to database
}
else if($toplam==0){
show form2 and add values to database
}
else if($toplam==$dun){
header('Location: member.php');
}else{
echo "<script> alert('Error.');history.go(-1);</script>";
}
?>
How i can show two different from by the if statement

You could use inline HTML by closing the <?PHP tag with ?> like this:
<?php
if($dun==0){
// start parsing HTML
?>
<form>[...]</form>
<?PHP
} else if($toplam==0){
// start parsing second HTML
?>
<form>[...]</form>
<?PHP
}
else if($toplam==$dun){
header('Location: member.php');
}else{
echo "<script> alert('Error.');history.go(-1);</script>";
}
?>

If OOP is not used and you are unfamiliar with OOP programming and MVC framework or html rendering then you could do the following:
form1.php file holds the HTML for form 1
form2.php file holds the HTML for form 2
if ($dun == 0) {
include('form1.php');
//add values to database
} else if ($toplam == 0) {
include('form2.php');
//add values to database
}

Couldn't you just echo the forms the same way you echo'd the javascript?
<?php
if($dun==0){
echo '
<form name="form1"><input type="submit" name="submit1" /></form>
';
}
else if($toplam==0){
echo '
<form name="form2"><input type="submit" name="submit2" /></form>
';
}
else if($toplam==$dun){
header('Location: member.php');
}else{
echo "<script> alert('Error.');history.go(-1);</script>";
}
?>
And if you wanted to insert data into your database depending on the if statements then you could add functions:
<?php
function doForm1(){
//MYSQL HERE
}
function doForm2(){
//MYSQL HERE
}
if($dun==0){
echo '
<form name="form1"><input type="submit" name="submit1" /></form>
';
doForm1();
}
else if($toplam==0){
echo '
<form name="form2"><input type="submit" name="submit2" /></form>
';
doForm2();
}
else if($toplam==$dun){
header('Location: member.php');
}else{
echo "<script> alert('Error.');history.go(-1);</script>";
}
?>

Related

Print php output in pop window on submitting the form

I would like to print the output of PHP in a pop window after the submission of html form. Below is sample of my code.
<?php
if(isset($_POST['button1']))
{
if("condition match")
{
echo "output1";
}
else
{
echo "output2";
}
}
?>
<form class='n-form' action='#' method='post'>
<div>
<button type='submit' name="button1" class='button'>Submit</button>
Cancel
</div>
</form>
It should be able to print the echo output in the pop window based on the if condition.
Looking forward to hear.
There is no popout windows in php you have to do it in javascript
<?php
if(!empty($_POST))
{
$text = (isset($_POST['button1'])) ? "output1" : "output2";
echo '<script>alert("' . $text . '")</script>';
}
?>

Get session array from different file

I can't access array from other file and still don't figure it out, whether my data already stored into array or not. Should i put $_SESSION into the function?
Lat3_3a.php
<form id="form1" name="form1" method="post" action="Lat3_3b.php">
Insert number: <input type="number" name="num" id="num" />
<input type="submit" name="button" id="button" value="OK" />
</form>
Lat3_3b.php
<?php
session_start();
$_SESSION["num"] = $_POST["num"];
if (empty($_SESSION["num"]))
echo "Please, insert number";
else {
$val=$_POST['num'];
echo " Factorial " .$val. " ! = " .factorial($val)."<br/>";
echo "<a href='Lat3_3c.php'>Link</a>";
}
function factorial($val){
if($val<=1){
$result=1;
return $result;
}elseif($val>1){
for($i=1; $i<=$val; $i++){
$result=$val * factorial($val-1);
}
return $result;
}
$data=array($val,$result,"12345", "Travis");
$_SESSION["var"]=$_POST["data"];
}
?>
Lat3_3c.php
<?php
session_start();
if(empty($_SESSION["var"]))
echo "Variable not found";
else
echo "Data : ". $_SESSION["var"];
?>
Ok looking a bit more in to your code, you need to learn more about the session, post and functions. For that a look at the PHP manual site.
Site http://php.net
For your code to work you need to use something like this:
<?php
session_start();
$_SESSION["num"] = $_POST["num"];
$_SESSION["var"]=array();
if (empty($_SESSION["num"])){
echo "Please, insert number";
}
else {
$val=$_POST['num'];
$function_result=factorial($val);
$data=array($val,$function_result,"12345","Travis");
$_SESSION["var"]=$data;
echo " Factorial " .$val. " ! = " .$function_result."<br/>";
echo "<a href='Lat3_3c.php'>Link</a>";
}
function factorial($val){
if($val<=1){
$result=1;
return $result;
}elseif($val>1){
for($i=1; $i<=$val; $i++){
$result=$val * factorial($val-1);
}
return $result;
}
}
?>
I'm not looking into your math.

textarea content auto clears whenever i click the form submit button [PHP]

I have a textarea with a form submit button. Whenever I click the submit button, the content on my textarea clears. but i dont want to clear the content of my textarea. here is my code
codepage.php
<?php
$ans = "hello";
if (isset($_POST['textcode'])) {
{
if ($_POST['textcode'] == $ans) {
echo "<div id=errorPlace>proceed to next lesson</div>";
}
else
{
echo "<div id=errorPlace>Error</div>";
}
}
}
?>
<form method="POST" name="validatePHP">
<textarea name="textcode"></textarea>
<input type="submit" class="btnSubmit" title="Submit Code" name="add" value=""></input>
</form>
thanks for the answers! It worked. now i have another question, what if the textarea has already a preloaded text in it and when I type in another text in it and click the submit button, the textarea should have now have the text that i inputted and the preloaded text in the textarea.
here is my updated code
<?php
$ans = "hello";
if (isset($_POST['textcode'])) {
{
if ($_POST['textcode'] == $ans) {
echo "<div id=errorPlace>proceed to next lesson</div>";
}
else
{
echo "<div id=errorPlace>Error</div>";
}
}
}
?>
<form method="POST" name="validatePHP">
<textarea name="textcode"><?php if(isset($_POST['textcode'])) {
echo htmlentities ($_POST['textcode']); }?>hell</textarea>
<input type="submit" class="btnSubmit" title="Submit Code" name="add" value=""></input>
</form>
Try render content after submit
<textarea name="textcode"><?= $_POST['textcode']; ?></textarea>
<textarea name="textcode">
<?php if(isset($_POST['textcode'])) {
echo htmlentities ($_POST['textcode']); }?>
</textarea>
Maybe you could do it with $_SESSION?
at the top of your page type session_start();
then inside
if ($_POST['textcode'] == $ans) {
echo "<div id=errorPlace>proceed to next lesson</div>";
}
add the code $_SESSION['textareaMsg'] = $_POST['textcode']; like this...
if ($_POST['textcode'] == $ans) {
echo "<div id=errorPlace>proceed to next lesson</div>";
$_SESSION['textareaMsg'] = $_POST['textcode'];
}
then where your text area is set just replace it with this.
<?php
if(isset($_SESSION['textareaMsg'])){
echo '<textarea name="textcode">'.$_SESSION['textareaMsg'].'</textarea>';
}else{
echo '<textarea name="textcode"></textarea>';
}
?>
This works by saving the text area as a session variable when you submit the form, and checking if its set when you load the form, if it is then it will replace the contents of the text area with what is set in the session. Hope this helps!
Try following code
<?php
$ans = "hello";
$textcode = ""; //declare a variable without value to avoid undefined error
if (isset($_POST['textcode'])) {
{
$textcode=$_POST['textcode']; //assign the value to variable in you if statment
if ($textcode == $ans) { //useing variable in if statment
echo "<div id=errorPlace>proceed to next lesson</div>";
}
else
{
echo "<div id=errorPlace>Error</div>";
}
}
}
?>
<form method="POST" name="test.php">
<!--echo user input -->
<textarea name="textcode"><?php echo $textcode; ?></textarea>
<input type="submit" class="btnSubmit" title="Submit Code" name="add" value=""></input>
</form>

PHP: One php file - many pages

I'm supposed to make 1 PHP file called "access.php" that contains 11 pages. I have to do this with 1 PHP file. I'm supposed to accomplish this using functions to display each page. The "welcome" page allows a user to select "Administrator" or "User" via radio button. After hitting submit, the corresponding page should appear. Right now the admin/user page functions simply echo "Administrator Page" or "User Page" for testing purposes.
Ideally, a new page should appear that displays one of those centered at the top of the screen. But what's happening right now is the text "Administrator Page/User Page" just appears at the bottom of the welcome screen.
Here is my code:
<?php
#---Functions---
#**PHP Header**
#function phpHeader(){
#print <<< EOF
#<!DOCTYPE html>
#<head>
#</head>
# <body>
#EOF
#}
#**PHP Footer**
#function phpFooter() {
#echo "</body> </html>";
#}
#**Admin Page**
function adminPage(){
#phpHeader();
echo "<center><h1>Administrator Page</h1></center>";
#phpFooter();
}
#**User Page**
function userPage(){
#phpHeader();
echo "<center><h1>User Page</h1></center>";
#phpFooter();
}
#**Welcome Page**
function welcomePage(){
#phpHeader();
echo "<center><h1>Welcome</h1>";
echo "<br><br><br>";
echo "<h3> Select Login Type</h3>";
echo "<br><br><br>";
echo '<form name="access" action="" method="post">';
echo '<input type="radio" name="AccessLevel" value="admin" />Administrator <br />';
echo '<input type="radio" name="AccessLevel" value="user" /> User <br /><br />';
echo '<input type="submit" name="submit" value="Choose Level" />';
echo '</form></center>';
$AccessLevel = $_POST['AccessLevel'];
if (isset($_POST['submit'])) {
if (! isset($AccessLevel)) {
echo '<h2>Select an option </h2>';
}
elseif ($AccessLevel == "admin") {
adminPage();
}
else {
userPage();
}
}
#phpFooter();
}
welcomePage();
?>
Move this entire block at the top of your page, right under your opening <?php tag.
Sidenote: You can use return; or exit; or die();, the choice is yours; I used return;
$AccessLevel = $_POST['AccessLevel'];
if (isset($_POST['submit'])) {
if (! isset($AccessLevel)) {
echo '<h2>Select an option </h2>';
}
elseif ($AccessLevel == "admin") {
echo adminPage();
return;
}
else {
userPage();
return;
}
}
Which will echo only "Administrator Page" or "User Page" at the top of the page and nothing else.
<?php
function adminPage(){
return "<center><h1>Administrator Page</h1></center>";
}
function userPage(){
return "<center><h1>User Page</h1></center>";
}
function welcomePage(){
return '<center><h1>Welcome</h1>
<br><br><br>
<h3> Select Login Type</h3>
<br><br><br>
<form name="access" action="" method="post">
<input type="radio" name="AccessLevel" value="admin" />Administrator <br />
<input type="radio" name="AccessLevel" value="user" /> User <br /><br />
<input type="submit" name="submit" value="Choose Level" />
</form></center>';
}
$AccessLevel = $_POST['AccessLevel'];
if(isset($_POST['submit'])){
if(!isset($AccessLevel)){
echo welcomePage();
}elseif($AccessLevel=="admin"){
echo adminPage();
echo welcomePage(); // if you want to repeat that content at the bottom again
}elseif($AccessLevel=="user"){
echo userPage();
echo welcomePage(); // if you want to repeat that content at the bottom again
}
}else{//no form submitted
echo welcomePage();
}
?>

Using html in php

I have a problem with this statement:
after this i just get blank page.
so the whole code looks like this :
im tyrying to use html in php and than php again in html:
the whole rest works and if i replace '' with add2.php it works but it writes something before i even pick something
<?php
function login()
{
echo "?";
}
$get = $_GET['Login'];
$get = $_POST['Login'];
echo $get;
var_dump($get);
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<div class="content">
<?php
require 'connection.php';
include 'user_verify.php';
include 'access_verify.php';
mysql_select_db("idoctor_db") or die("Bląd podczas wybierania bazy danych");
$select = 'SELECT * FROM users;';
$query = mysql_query($select);
// Ustaw domyślny element; tutaj są ustawione kreseczki, żeby nic nie sugerować ;P
echo '<form action="'<?php login(); ?>'" method="post">
Jezyk <select name="Login"><option value="0">------------------</option>';
while ($language = mysql_fetch_object($query))
{
echo '<option value="'.$language->Login.'" selected>'.$language->Login.'</option>';
}
echo '</form>';
?>
<input type="submit" value="Login" name="submit"/>
</div>
</body>
</html>
The HTML form element doesn't render anything on screen... add some content and you will see it is working.
<?php
function login()
{
echo "?";
}
?>
<?php
echo '<form action="<?php login(); ?>" method="post">';
echo 'Hello World!';
echo '</form>';
?>
Make sure your quotes are properly closed too. (I'm not sure if the missing closing single quote in your sample was a copy/paste error or not)
<?php
echo '<form action="<?php login(); ?>" method="post">
?>
Is a syntax error, you forgot the closing single quote and semicolon.
That said, I do not think this code will do what you expect: By using echo you will send "<?php login(); ?>" to the browser, not run it in the PHP interpreter.
This:
<?php
echo '<form action="<?php login(); ?>" method="post">
?>
Should be:
<?php
echo "<form action='" . login() . "' method='post'>";
?>
But then your login function needs to be fixed because printing out ? is not going to work.
function login()
{
return "login.php";
}

Categories