A php 'if statement' example from a tutorial by Kevin Yank doesn't seem to work. Specifically, both branches of the conditional appear when the page loads.
I have also tried changing to the non-shorthand 'if' syntax, to no avail.
Is the problem something other than syntax?
The code is as follows:
<!DOCTYPE html>
<html>
<head>
<title> Sample Page </title>
</head>
<body>
<?php if(isset($name)) : ?>
<p> Your name: <?php echo ($name); ?> </p>
<p> This paragraph contains a link that passes the name variable on to the next document. </p>
<?php else : ?>
<!-- No name has been provided, so prompt the user for one -->
<form action="<php echo($PHP_SELF); ?>" method="get">
Please enter your user name:
<input type="text" name="name">
<input type="submit" value="ok">
</form>
<?php endif; ?>
</body>
</html>
This script left out a very important element >>> $name=$_GET['name']; <<<
The name must first be declared as a variable.
It has been added inside this line: <?php if(isset($name)) : ?>
See my code below: (tested and working)
<HTML>
<HEAD>
<TITLE> Sample Page </TITLE>
</HEAD>
<BODY>
<?php $name=$_GET['name']; if (isset($name)): ?>
<P>Your name: <?php echo($name); ?></P>
<P>This paragraph contains a
<A HREF="newpage.php?name=<?php echo(urlencode
($name)); ?>">link</A> that passes the
name variable on to the next document.</P>
<?php else: ?>
<!-- No name has been provided, so we
prompt the user for one. -->
<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD="GET">
Please enter your name: <INPUT TYPE="TEXT" NAME="name">
<INPUT TYPE="SUBMIT" VALUE="GO">
</FORM>
<?php endif; ?>
</BODY>
</HTML>
Plus as an added bonus (tested and working), you can use the PHP code below, inside your goodone.php file to echo the name after you clicked on the link after you submitted the form. You can do whatever you wish from hereonin, for database use, etc.
<?php
echo($_GET['name']);
// do something else
?>
Related
Hi I am in New In Php.
I am asking why we are using hidden text box and given value =1.
<input type="text" name="form_submitted" value="1"/>
<html>
<head>
<title>Registration Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<?php if (isset($_POST['form_submitted'])): ?>
<?php if (!isset($_POST['agree'])): ?>
<p>You have not accepted our terms of service</p>
<?php else: ?>
<h2>Thank You <?php echo $_POST['firstname']; ?></h2>
<p>You have been registered as <?php echo $_POST['firstname'] . ' ' . $_POST['lastname']; ?> </p>
<p> Go <a href="sample.php" >back</a> to the form</p>
<?php endif; ?>
<?php else: ?>
<h2>Registration Form</h2>
<form action="sample.php" method="POST">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname"><br>
Agree to Terms of Service: <input type="checkbox" name="agree"> <br>
**<input type="hidden" name="form_submitted" value="1"/>**
<input type="submit" value="Submit">
</form>
<?php endif; ?>
</body>
</html>
In this case, the reason the developer have done this, is because it is used in the first IF-statement.
if (isset($_POST['form_submitted'])):
When the form is submitted, it is sent to the same file. When a form is submitted, the form values is accessible via the $_POST paremeter in php. So if $_POST['form_submitted'] is set, then he executes the following code, and if not, the code inside else: is executed
I also have to say that this code is not a good example of how to handle form submissions, and should be improved.
Developers using <input type="hidden" name="form_submitted" value=""> because sometimes they use it as reference for the form submission. They can use it for conditions or whatever functionality they want to use with the hidden input.
Instead of showing the exact input (input text), they're hiding it for more cleaner form.
Here i entered name nd email id but when i click on submit button my entered information is blank like below image.
In PHP clicking on the submit button doesn't print the information. I used $GET and $POST, both are not working.
<!DOCTYPE HTML>
<html>
<body>
<form action="php_forms.php" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
It should be work.Change your action (php_forms.php) file code.I have tried in my server.Hope it will be helped.
<html>
<body>
Welcome <?php echo htmlspecialchars($_POST["name"]); ?><br>
Your email address is: <?php echo htmlspecialchars($_POST["email"]); ?>
</body>
</html>
Read More dealing with html forms in PHP.NET.
Your form uses get method and php_forms.php uses post method. Try
<form action="php_forms.php" method="post">
Does anyone know how to change PHP Variable values by HTML Form ? I am using php script to display the values on HTML Pages
but i want to change the values by form not by editing php files but i don't know how to do it
here is my php script [data.php]
<?php
$web_title_en = "JDST BLOG"; // Website Title
$web_subtitle_en = "blah blah blah"; // Website Subtitle
?>
and html (for display the values) [index.html]
<html>
<head>
<?php include 'data.php';?>
</head>
<body>
<h1><?php echo $web_title_en; ?></h1>
<h4><?php echo $web_subtitle_en; ?></h4>
</body>
</html>
If you want to process html forms with php you should read this w3c tutorial. Your form would be something like this:
<html>
<body>
<form action="data.php" method="post">
Title: <input type="text" name="title"><br>
Subtitle: <input type="text" name="subtitle"><br>
<input type="submit">
</form>
</body>
</html>
And this would be data.php (a merged version with your index.html)
<?php
$web_title_en = $_POST['title']; // Website Title
$web_subtitle_en = $_POST['subtitle']; // Website Subtitle
?>
<html>
<body>
<h1><?php echo $web_title_en; ?></h1>
<h4><?php echo $web_subtitle_en; ?></h4>
</body>
</html>
I'm not sure about your exact situation (perhaps the question could be reworded?), but this might be a useful document on how to handle form data:
http://www.w3schools.com/php/php_forms.asp
Here is my Code :
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" action="../../../wamp/www/abc.php" method="get">
<input type="text" name="text1" id="username" />
<input type="submit" />
</form>
</body>
</html>
The abc.php file is located in C:/wamp/www/abc.php
<html>
<body>
Hello World
<?php
echo "Hello";
echo $_GET["username"];
?>
</body>
</html>
In the form, when I press the Submit button, only "Hello World" is displayed.
The value entered in the textbox is not displayed at all. Even the "Hello" message printed inside the php code is not displayed.
How can i display the Value ?
text1 is the name of your input, so the correct PHP code would be.
<?php
echo "Hello ";
echo $_GET['text1'];
?>
Try the below, get array is created using name attr not id one.
echo $_GET["text1"];
There is two solution for you should use :
First Way :Change your php code:
<?php
echo "hello";
echo $_GET["text1"];
?>
Second Way: Chnage your HTML
<input type="text" name="username" id="username"/>
If second "Hello" from echo is not displayed, there is a problem with your PHP installation.
I have a simple task , and i'm using a MVC methods and CI framework for this task.
I have made a view to input data to database, and it's work, and i make a 2 anchors, those are an [Update] and [Delete], the function delete is working, but the update isn't working.
After user click anchor [Update], it will linked to another view(update_view), and i want to show the content which i've clicked in the (update_view). and i think it use a set_value to set a second parameter, to show a value in my update_view.
This is my code in a view(update_view)
<!-- update_view.php -->
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>Update</h2>
<?php echo form_open('site/update'); ?>
<p>
<label>Judul : </label>
<input type="text" name="judul" id="judul" value="<?php echo set_value('judul','??')?>"/>
</p>
<p>
<label>Konten : </label>
<input type="text" name="konten" id="konten" size="100px" value="<?php echo set_value('konten','??')?>"/>
</p>
<p><input type="submit" value="Ubah" /></p>
<?php echo form_close(); ?>
</body>
</html>
What should i put in input tag in value attributes, i want to show a value in input fields (judul, konten) from page before where i clicked the anchors.
I still can't show a image for a view before click, because i'm still don't have 10 rep to share images. so i'will show the coding where the view(options_view) i've clicked the anchors.
This below is the code in a view(options_view) :
<!-- options_view.php -->
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>Create</h2>
<?php echo form_open('site/create'); ?>
<p>
<label>Judul : </label>
<input type="text" name="judul" id="judul" />
</p>
<p>
<label>Konten : </label>
<input type="text" name="konten" id="konten" size="100px"/>
</p>
<p><input type="submit" value="Simpan" /></p>
<?php echo form_close(); ?>
<hr/>
<h2>Read</h2>
<?php if(isset($records)): foreach($records as $baris) : ?>
<h3><?php echo $baris->judul ?></h3>
<div><?php echo $baris->konten ?></div>
<?php echo anchor("site/view_update/$baris->id","[Update]"); ?>
<?php echo anchor("site/delete/$baris->id","[Delete]"); ?>
<?php endforeach; ?>
<?php else : ?>
<h3>Tidak ada data.</h3>
<?php endif; ?>
</body>
</html>
i'm still doubt, whether i should add code in my controller or my view(set_value).
So anyone can help me to solve this problem.
Thank's for help
set_value is codeigniter form_helper function. That helps u to print previous input value when form_validation fails.
In your case if u want to show your data u need to pass data to the view in controller.
E.G:
Controller:
$data["me"] = $this->model->getData($id);
$this->load->view("update_view",$data);
View:
<input type="text" name="judul" id="judul" value="<?php echo $me->judul;?>"/>