Jump to content

Dynamically update on a page with forms?


hawk__0

Recommended Posts

Hey guys, I'm pretty new to PHP, and I did take C a year ago, but im a bit rusty.  What my goal here is, is to add something into an array (using a standard HTML form) and once i hit submit, its displayed on the screen.  Currently I only attempted to have it display one thing for testing purposes.  Anyway, heres my code:

 

<html>
<body>
<form method="post">
<table>
<tr>
        <td>Company:
        <td><input type="text" name="company">
</tr>
<tr>
        <td>Name:
        <td><input type="text" name="name">
</tr>
<tr>
        <td>Address:
        <td><input type="text" name="address">
</tr>
<tr>
        <td>City:
        <td><input type="text" name="city">
</tr>
<tr>
        <td>Postal Address:
        <td><input type="text" name="postal">
</tr>
<tr>
        <td>Telephone Number:
        <td><input type="text" name="tel">
</tr>
<tr>
        <td>Email Address:
        <td><input type="text" name="email">
</tr>
<tr>
        <td>Comments:
        <td><input type="text" name="comments">
</tr>
<tr>
        <td><input type="submit">
</tr>
</table>
<?php
$array = array("company","name","address","city","postal","tel","email","comments");
echo $array["company"];
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/102440-dynamically-update-on-a-page-with-forms/
Share on other sites

so if im right? you want to display what they enter in the form on the page, try this

<?php
$array = array("company","name","address","city","postal","tel","email","comments");
// i've never used arrays so im not sure if this way works
if(isset($_POST['submit']))
{
echo $array["company"]; // add anything you want displayed
}
else
{
?>
<html>
<body>
<form action='<?php $_SERVER['PHP_SELF']; ?>' method='POST'>
<table>
<tr>
        <td>Company:
        <td><input type="text" name="company">
</tr>
<tr>
        <td>Name:
        <td><input type="text" name="name">
</tr>
<tr>
        <td>Address:
        <td><input type="text" name="address">
</tr>
<tr>
        <td>City:
        <td><input type="text" name="city">
</tr>
<tr>
        <td>Postal Address:
        <td><input type="text" name="postal">
</tr>
<tr>
        <td>Telephone Number:
        <td><input type="text" name="tel">
</tr>
<tr>
        <td>Email Address:
        <td><input type="text" name="email">
</tr>
<tr>
        <td>Comments:
        <td><input type="text" name="comments">
</tr>
<tr>
        <td><input type='submit' value='Submit' name='submit' />
</tr>
</table>
<?php
}
?>

Hm, it still doesn't seem to be working.  Let me elaborate on exactly what I'm trying to do here:

I'm making a form, where the following information can be submitted and as soon as submit is clicked, it dynamically displays at the bottom of my page (the results).  I haven ow also added my next form which will be used for searching for a user through the array.

 

here is the current code now:

<html>
<body>
<u>Input a user</u>
<br />
<form method="post" action='<?php $_SERVER['PHP_SELF']; ?>'>
<table>
<tr>
<td>Company:
<td><input type="text" name="company">
</tr>
<tr>
<td>Name:
<td><input type="text" name="name">
</tr>
<tr>
<td>Address: 
<td><input type="text" name="address">
</tr>
<tr>
<td>City: 
<td><input type="text" name="city">
</tr>
<tr>
<td>Postal Address: 
<td><input type="text" name="postal">
</tr>
<tr>
<td>Telephone Number: 
<td><input type="text" name="tel">
</tr>
<tr>
<td>Email Address: 
<td><input type="text" name="email">
</tr>
<tr>
<td>Comments: 
<td><input type="text" name="comments">
</tr>
<tr>
<td><input type="submit" value="Submit">
</tr>
</table>
<br />
<hr>
<br />
<u>Find a user</u>
<!----Get Info---->
<form method="get">
<table>
<tr>
<td>Company:
<td><input type="text" name="company">
</tr>
<tr>
<td>Name:
<td><input type="text" name="name">
</tr>
<tr>
<td>Address: 
<td><input type="text" name="address">
</tr>
<tr>
<td>City: 
<td><input type="text" name="city">
</tr>
<tr>
<td>Postal Address: 
<td><input type="text" name="postal">
</tr>
<tr>
<td>Telephone Number: 
<td><input type="text" name="tel">
</tr>
<tr>
<td>Email Address: 
<td><input type="text" name="email">
</tr>
<tr>
<td>Comments: 
<td><input type="text" name="comments">
</tr>
<tr>
<td><input type="submit" value="Search!">
</tr>
</table>
<br />
<hr>
<u>Search Results:</u>
<br />
<br />
<?php
$array = array("company" => $company,"name","address","city","postal","tel","email","comments");
if(isset($_POST['submit']))
{
echo $array["company"];
}
else
{
}
?>
</body>
</html>

ok, let me change the code a bit

<?php
$company = $_POST['company']
$name = $_POST['name']
$city = $_POST['address']
$postal = $_POST['postal]
$tel = $_POST['tel']
$email = $_POST['email']
$comments = $_POST['comments']

if(isset($_POST['submit']))
{
echo $company<br>$name<br>$city<br>$postal<br>$tel<br>$email<br>$comments; // add anything you want displayed
}
else
{
?>
<html>
<body>
<form action='<?php $_SERVER['PHP_SELF']; ?>' method='POST'>
<table>
<tr>
        <td>Company:
        <td><input type="text" name="company">
</tr>
<tr>
        <td>Name:
        <td><input type="text" name="name">
</tr>
<tr>
        <td>Address:
        <td><input type="text" name="address">
</tr>
<tr>
        <td>City:
        <td><input type="text" name="city">
</tr>
<tr>
        <td>Postal Address:
        <td><input type="text" name="postal">
</tr>
<tr>
        <td>Telephone Number:
        <td><input type="text" name="tel">
</tr>
<tr>
        <td>Email Address:
        <td><input type="text" name="email">
</tr>
<tr>
        <td>Comments:
        <td><input type="text" name="comments">
</tr>
<tr>
        <td><input type='submit' value='Submit' name='submit' />
</tr>
</table>
<?php
}
?>

OK, still not working... Current code:

<html>
<body>
<?php
$company = $_POST['company'];
$name = $_POST['name'];
$city = $_POST['address'];
$postal = $_POST['postal'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$comments = $_POST['comments'];

if(isset($_POST['submit']))
{
echo "$company<br>$name<br>$city<br>$postal<br>$tel<br>$emaili<br>$comments"; // add anything you want displayed
}
else
{

?>
<u>Input a user</u>
<br />
<form method="post" action='<?php $_SERVER['PHP_SELF']; ?>'>
<table>
<tr>
<td>Company:
<td><input type="text" name="company">
</tr>
<tr>
<td>Name:
<td><input type="text" name="name">
</tr>
<tr>
<td>Address: 
<td><input type="text" name="address">
</tr>
<tr>
<td>City: 
<td><input type="text" name="city">
</tr>
<tr>
<td>Postal Address: 
<td><input type="text" name="postal">
</tr>
<tr>
<td>Telephone Number: 
<td><input type="text" name="tel">
</tr>
<tr>
<td>Email Address: 
<td><input type="text" name="email">
</tr>
<tr>
<td>Comments: 
<td><input type="text" name="comments">
</tr>
<tr>
<td><input type="submit" value="Submit">
</tr>
</table>
<br />
<hr>
<br />
<u>Find a user</u>
<!----Get Info---->
<form method="get">
<table>
<tr>
<td>Company:
<td><input type="text" name="company">
</tr>
<tr>
<td>Name:
<td><input type="text" name="name">
</tr>
<tr>
<td>Address: 
<td><input type="text" name="address">
</tr>
<tr>
<td>City: 
<td><input type="text" name="city">
</tr>
<tr>
<td>Postal Address: 
<td><input type="text" name="postal">
</tr>
<tr>
<td>Telephone Number: 
<td><input type="text" name="tel">
</tr>
<tr>
<td>Email Address: 
<td><input type="text" name="email">
</tr>
<tr>
<td>Comments: 
<td><input type="text" name="comments">
</tr>
<tr>
<td><input type="submit" value="Search!">
</tr>
</table>
<br />
<hr>
<u>Search Results:</u>
<br />
<br />
<?php
}
?>
</body>
</html>

 

and here's the page if you wanna try it to see what it does:

www.ninjasown.com/assig1.php

are there any errors, and change the code to this.

<?php
$company = $_POST['company'];
$name = $_POST['name'];
$city = $_POST['address'];
$postal = $_POST['postal'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$comments = $_POST['comments'];

if(isset($_POST['submit']))
{
echo "$company<br>$name<br>$city<br>$postal<br>$tel<br>$emaili<br>$comments"; // add anything you want displayed
}
else
{

?>
<u>Input a user</u>
<br />
<form method="post" action='<?php $_SERVER['PHP_SELF']; ?>'>
<table>
<tr>
<td>Company:
<td><input type="text" name="company">
</tr>
<tr>
<td>Name:
<td><input type="text" name="name">
</tr>
<tr>
<td>Address: 
<td><input type="text" name="address">
</tr>
<tr>
<td>City: 
<td><input type="text" name="city">
</tr>
<tr>
<td>Postal Address: 
<td><input type="text" name="postal">
</tr>
<tr>
<td>Telephone Number: 
<td><input type="text" name="tel">
</tr>
<tr>
<td>Email Address: 
<td><input type="text" name="email">
</tr>
<tr>
<td>Comments: 
<td><input type="text" name="comments">
</tr>
<tr>
<td><input type="submit" value="Submit">
</tr>
</table>
<?php
}
?>

 

first of all and once this bit works, work on the search.

 

Time to lay down the law.  *Pulls out pistol*

Anyway, here:

<?php
if (isset($_POST['submit1'])) {
$company = $_POST['company'];
$name = $_POST['name'];
$city = $_POST['address'];
$postal = $_POST['postal'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$comments = $_POST['comments'];
}

?>
<u>Input a user</u>
<br />
<form method="post" action='<?php $_SERVER['PHP_SELF']; ?>'>
<table>
<tr>
<td>Company:
<td><input type="text" name="company">
</tr>
<tr>
<td>Name:
<td><input type="text" name="name">
</tr>
<tr>
<td>Address: 
<td><input type="text" name="address">
</tr>
<tr>
<td>City: 
<td><input type="text" name="city">
</tr>
<tr>
<td>Postal Address: 
<td><input type="text" name="postal">
</tr>
<tr>
<td>Telephone Number: 
<td><input type="text" name="tel">
</tr>
<tr>
<td>Email Address: 
<td><input type="text" name="email">
</tr>
<tr>
<td>Comments: 
<td><input type="text" name="comments">
</tr>
<tr>
       <input type="hidden" name="submit1" value="TRUE" />
<td><input type="submit" value="Submit">
</tr>
</table>
<?php
if(isset($_POST['submit1']))
{
echo "Company: $company<br>Name: $name<br>City: $city<br>Postal Address: $postal<br>Telephone Number: $tel<br>Email: $emaili<br>Comments: $comments"; 
}
?>

 

Try that. =)

 

EDIT: Missed a }. D:  Try it now.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.