Passing values between multiple pages php - php

I am using 2 pages. Page 1, the user enters name and age. I have given form action as the same page and user selects either one or both occupation and the data gets pushed to page 2 which displays all the results. Below is the code for the same:
Page1:
<?php
echo "<form action=\"page1.php\" method=\"post\">";
echo "<h2>Your Name. *</h2><input type='text' name='user_name'>";
echo "<br><br>";
echo "<h2>Your Age. *</h2><input type='text' name='age'>";
echo "<br><br>";
echo "<div><input type='submit' value='Review'></div>";
?>
<?php
if((empty($_POST['user_name'])) || (empty($_POST['age'])) ) {
echo "<h2>Please enter your user name and age</h2>";
} else {
$user_name = $_POST['user_name'];
$age = $_POST['age'];
echo "<h2>Below are the details entered:</h2><br>";
echo "<h2>Name: </h2>$user_name";
echo "<h2>Age: </h2>$age";
echo "<form action=\"page2.php\" method=\"post\">";
echo '<td bgcolor="#EAEAEA" style="color:#003399"><input type="checkbox"
name="occupation[]" value="QA">QA</td>';
echo '<td bgcolor="#EAEAEA" style="color:#003399"><input type="checkbox"
name="occupation[]" value="Tester">Tester</td>';
echo "<br>";
echo "<input type=\"submit\" value='Add to DB' >";
echo "</form>";
}
?>
Page2:
<?php
$user_name = $_GET['user_name'];
$age = $_GET['junos_version'];
$occupation = $_GET['occupation'];
echo "<h2>Below are the details entered:</h2><br>";
echo "<h2>Name: </h2>$user_name";
echo "<h2>Age: </h2>$age";
echo "<h2>Occupation selected: </h2>";
for ($i=0;$i<sizeof($occupation);$i++) {
echo " $occupation[$i] ";
}
?>
Please help!!!

Put all of that fields in just one form and validate the action or put validation into php before frontend start, I prefer the first one to your case, it's easier:
FILTERING IN THE ACTION
<?php
if (empty($_POST['user_name'])){
$action = 'page1.php';
$structure = "<h2>Please enter your user name and age</h2>";
$submit = 'Review';
} else {
$action = 'page2.php';
$phrase = '';
$user_name = $_POST['user_name'];
$age = $_POST['age'];
$structure = '<h2>Below are the details entered:</h2><br>
<h2>Name: </h2>$user_name
<h2>Age: </h2>$age
<td bgcolor="#EAEAEA" style="color:#003399"><input type="checkbox" name="occupation[]" value="QA">QA</td>
<td bgcolor="#EAEAEA" style="color:#003399"><input type="checkbox" name="occupation[]" value="Tester">Tester</td>';
echo "<br>";
$submit = 'Add to DB';
}
echo "<form action='{$action}' method=\"post\">";
echo "<h2>Your Name. *</h2><input type='text' name='user_name'>";
echo "<br><br>";
echo "<h2>Your Age. *</h2><input type='text' name='age'>";
echo "<br><br>";
echo $structure;
echo "<div><input type='submit' value='{$submit}'></div>";
echo "</form>";
?>
PS: Avoid all those echoes, put just plain html and use php where it needed, it's better. TD's outside tables are utterly wrong, remove then, use divs instead and organize them with CSS.

Related

HTML button running out of alignment

My idea is very simple, I will have a search box and a submit button.
When user key in the keyword and click on the submit button, results will be shown below with an additional button. Now my problem is I have no idea on how to make the button to be located at bottom right of the table populated.
Please consider the below code for my situation:
<input type="text" name="criteriaInput" style="width: 300px;"> <input type="submit" name="submit" value="GO" />
<?php
if (isset($_POST['submit'])) {
if(isset($_POST['inquiryMethod'])){
error_reporting(0);
$sql = 'SELECT
*
FROM
table
WHERE
fullname REGEXP \''.$_POST['criteriaInput'].'\'' ;
$server = mysql_connect("localhost","root", "");
$db = mysql_select_db("mysql",$server);
$query = mysql_query($sql);
echo "<table class=\"striped\">";
echo "<tr class=\"header\">";
echo "<td>Full Name</td>";
echo "<td>ID</td>";
echo "<td>ID Type</td>";
echo "<td>Issuance Country</td>";
echo "<td>Class</td>";
echo "</tr>";
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>".$row[fullname]."</td>";
echo "<td>".$row[id]."</td>";
echo "<td>".$row[id_type]."</td>";
echo "<td>".$row[issuance_country]."</td>";
echo "<td>{$row['class']}</td>";
echo "</tr>";
}
echo "<form method=\"post\" action=\"CIF_InquiryAction.php\">";
echo "<input type=\"submit\" name=\"create\" value=\"Create\" />";
echo "</form>";
echo "</table>";
}else{
echo "Please select one of the criteria!";
}
}
?>
The submit button with value "Create" did successfully created on existence of data, however it's aligned on top left of the table.
Kindly advice Thank you.
You need to put your button into a table row and cell.
echo "<tr>";
echo "<td colspan=\"5\">"
echo "<form method=\"post\" action=\"CIF_InquiryAction.php\">";
echo "<input type=\"submit\" name=\"create\" value=\"Create\" />";
echo "</form>";
echo "</td>"
echo "</tr>";
Also, your form should probably move to be outside your table.
Editing to show input outside of table:
echo "</table>";
echo "<input type=\"submit\" name=\"create\" value=\"Create\" />";

another simple PHP page

I'm trying to create a simple PHP page that will Display a number starting with 0. It will then ask for a number input, then add the inputted number to the beginning number. and keep adding the number that is input in the text box. Here's what I have so far... I can get the first variable initialized and get the input from a form box. I just can't seem to add them together and keep a running total. Thanks in advance.
<?php
$_POST['number1'];
echo "Current number is ".$_POST['number1'];
echo "<br>";
echo "Enter your next number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='number' name='number2'>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "<br>";
echo "Your entered number is ".$_POST['number2']."<br>";
$sumtotal = $_POST['number1'] + $_POST['$number2'];
echo "Your new total is ".$sumtotal;
$_POST['number1'] == $_POST['number2'];
?>
Using input type HIDDEN:
<?php
if(isset($_POST['submit'])){
$total=$_POST['number1']+$_POST['number2'];
echo "Your entered number is ".$_POST['number2']."<br>";
}
else {
$total=0;
}
echo "Current total number is ".$total;
echo "<br>";
echo "Enter your number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='hidden' name='number1' value='$total'>";
echo "<input type='number' name='number2'>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form><br>";
echo "Your new total is ".$total;
?>
Using SESSION:
<?php
session_start();
if(empty($_SESSION["total"])){
$_SESSION["total"]=0;
}
if(isset($_POST['submit'])){
$total=$_SESSION["total"];
$total=$total+$_POST['number2'];
$_SESSION["total"]=$total;
echo "Your entered number is ".$_POST['number2']."<br>";
}
echo "Current total number is ".$_SESSION['total'];
echo "<br>";
echo "Enter your number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='number' name='number2'>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form><br>";
echo "Your new total is ".$_SESSION['total'];
?>
Both works and have the same output, but with different approach from one another.
You could add another hidden input field that keeps the last number that was posted by you.
<input style="display: none" name="tmp_number" value="<?php echo $tmp_number; ?>">
Try (edited for all details and errors)
<?php
$input = $sum = 0;
if (isset($_POST)) {
$input = $_POST['number1'];
$prev_sum = $_POST['prev'];
$sum = $input + $prev_sum;
}
echo "Current number is ".$input;
echo "<br>";
echo "Enter your next number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='number' name='number1'>";
echo "<input type='hidden' name='prev' value='$sum' >";
echo "<input type='submit' name='submit' value='Submit'>";
echo "<br>";
echo "Your entered number is ".$_POST['number1']."<br>";
echo "Your new total is ".$sum;
?>

get a variable row from a while mysql_fetch array that isnt the last result

Hey guys I'm having a hard time sending a session variable of the $row['projectnaam']
that belongs to the one that just has been clicked.
At the moment the Session always takes the last $row['projectnaam']
from the while loop and I'm wondering how I can send the right
variable with a session that belongs to the row that just has been clicked.
Thank you in advance.
Here's my syntax:
<?php
include "config.php"
$bedrijfsnaam = $_SESSION['gebruikerbedrijf'];
$result= mysql_query("SELECT * FROM projecten WHERE bedrijfsnaam='$bedrijfsnaam' ")or die(mysql_error());
while($row = mysql_fetch_array($result)){
if (isset($_POST['submit'])){
$_SESSION['projectnaam'] = $projectnaam;
header('Location: viewprojectsbedrijf.php');
}
echo "<div class='project'>";
echo "<div class='projectdetails'>";
echo "<p class='projectnaam'>";
echo $row['projectnaam'];
echo "</p>";
echo "<hr class='paars'>";
echo "<p class='datum'>";
echo $row['datum'];
echo "|";
echo $row['Tijd'];
echo "</p>";
echo "<hr class='paars'>";
echo "<p class='bedrijfsnaam'>";
echo $row['bedrijfsnaam'];
echo "</p>";
echo "</div>";
echo "<div class='view'>";
echo "<form method=\"POST\" action=\"\">";
echo "<input type='submit' value='View' name='submit' class='viewbutton'></input>";
echo "</form>";
echo "</div>";
}
?>
Mede Nederlander ;)
I think you mean to do something like this:
include("config.php");
if(isset($_POST['submit'])){
$_SESSION['projectnaam'] = $_POST['projectnaam'];
header('Location: viewprojectsbedrijf.php');
}
$bedrijfsnaam = $_SESSION['gebruikersbedrijf'];
$result= mysql_query("SELECT * FROM projecten WHERE bedrijfsnaam=".$bedrijfsnaam)or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<div class='project'>
<div class='projectdetails'>
<p class='projectnaam'>
<form method=post action=''>
<input type=text value=".$row['projectnaam']." name=projectnaam>
</p>
</div>
</div>
<input type='submit' value='submit' name='submit'>
</form>";
}
In your own script only your submit button is in the form, so that will be the only value posted. In that case you won't get the POST value of 'projectnaam'.
Also, you try to make a SESSION value of a variable which isn't declared in your posted code. In my code it makes a Session value of the POSTED code.

previously submitted form data disappears when new form is submitted

I have multiple forms on a single page and all gets redirected to same page when form is submitted but the previously submitted form values disappears when new form is submitted.
I tried using sessions didn't worked for me what else? please help
<script language="javascript">
function row(x,y){
//var len=document.forms[x].name.value;
if(x.value.length >=3)
{
//alert("Message form no> "+y+"will be submited");
document.forms[y].submit();
}
}
</script>
</head>
<body >
<center>
<h2>Database App</h2>
<table>
<tr>
<th><lable>Name :</label></th>
<th><label>E_Id :</label></th>
<th><label>Email :</label></th>
<th><label>Other Info :</label></th></tr>
<tr>
<?php
error_reporting(E_ALL ^ E_NOTICE);
// code check for name in database and if exists,displays in table row
//for($i=0;$i<150;$i++)
//{
//$E_id=array();
if($_POST){
$i = $_GET["uid"];
//echo "fhwefwej==".$i;
$x='name'.$i;
// echo 'dasvds'.$x;
if($_POST[$x])
{
$name = strtolower($_POST[$x]);
$E_id[$i] = "";
$Email[$i] = "";
$Otherinfo[$i] = "";
$con = mysql_connect('localhost','root','') or die("npt");
$db = mysql_select_db("trainee")or die("nptdff");
$query = "Select * from reguser where fname like '".$_POST[$x]."%'";
$result = mysql_query($query);
mysql_num_rows($result);
if(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_array($result))
{
$str=$row['fname'];
$initials = strtolower(substr($str,0,3));
if($name == $initials)
{
//echo "exist"."<br>";
$E_id[$i]= $row['fname'];
$Email[$i]=$row['lastname'];
$Otherinfo[$i]=$row['address'];
break;
}
}
}
else
{
$msg[$i] = "no user with these initials";
}
mysql_close($con);
}
}
for($i=0;$i<150;$i++)
{
//session_start();
//echo session_name($i)."<br>";
echo "<form name='form$i' action='new2.php?uid=$i' method='post'>";
echo "<td><input type='text' name='name$i' id='name$i' onkeyup='row(this,$i);' /><br />";
echo "<span id='availability_status' >";
if($_POST[$x]){echo $msg[$i];}
echo "</span> </td>";
echo "<td><input type='text' name='E_id' id='E_id' value='";
if(isset($_POST[$x])){ echo $E_id[$i];}
echo "' disabled='disabled' />";
echo "</td>";
echo "<td><input type='text' name='email' id='email' value='$Email[$i]' disabled='disabled' />";
echo "</td>";
echo "<td><input type='text' name='otherinfo' id='otherinfo' value='$Otherinfo[$i]' disabled='disabled' />";
echo "</td></tr>";
echo " </form>";
}
//echo '<script language="javascript">document.getElementById(\'name0\').focus();</script>';
?>
</table>
</center>
</body>
</html>
Why don't you Use AJAX. It will help to keep you posed different form information in back-end
you can either store those information in database or in file.
It will be the best way.

Link (on a while loop on a search query) inside a search form

echo "<center><form name = 'searching' method='POST' action='cpanel.php?manage=".$useraccounts."&rcad=".$viewcustomer."'><table border = 1>";
echo "<select name = 'filter'>";
echo "<option value ='username'>Username</option>";
echo "<option value ='username'>Username</option>";
echo "</select>";
echo " <input name='search' type='text' > ";
echo "<input type='submit' name='submit' value='Search'> ";
echo "<input type='submit' name='back' value='Back'><br><br>";
echo "<tr>";
echo "<td>User ID</td>";
echo "<td>Username</td>";
echo "<td>Last Name</td>";
echo "<td>First Name</td>";
echo "<td>Middle Initial</td>";
echo "<td>Address</td>";
echo "<td>Contact Number</td>";
echo "<td>Birthday</td>";
echo "<td>Date Registered</td>";
echo "<td> </td>";
echo "</tr>";
$submit = $_POST['submit'];
if(isset($submit)) {
$search = $_POST['search'];
$filter = $_POST['filter'];
include "dbconnect.php";
if ($search != NULL) {
$searchquery = mysql_query("SELECT * FROM register WHERE $filter LIKE '%$search%'");
while($fetchres = mysql_fetch_array($searchquery)) { //show search results
$userid = $fetchres['userid'];
$username = $fetchres['username'];
$lname = $fetchres['lname'];
$fname = $fetchres['fname'];
$mi = $fetchres['mi'];
$address = $fetchres['address'];
$contact = $fetchres['contact'];
$month = $fetchres['month'];
$day = $fetchres['day'];
$year = $fetchres['year'];
$dateregistered = $fetchres['date'];
$sendmessage = "<a href = 'cpanel.php?manage=".$useraccounts."&rcad=".$viewcustomer."&user=".$username."'>Send message</a>";
echo "<tr>";
echo "<td>$userid</td>";
echo "<td>$username</td>";
echo "<td>$lname</td>";
echo "<td>$fname</td>";
echo "<td>$mi</td>";
echo "<td>$address</td>";
echo "<td>$contact</td>";
echo "<td>$month $day, $year</td>";
echo "<td>$dateregistered</td>";
echo "<td>$sendmessage</td>";
echo "</tr>";
echo "$table";
if (isset($sendmessage)) {
$getuser = $_GET['user'];
if ($getuser == $username) {
//start send message
$touser = $username;
$fromuser = $adminsess;
$subject = $_POST['subject'];
$message = $_POST['message'];
$submit = $_POST['submit'];
$date = date("Y-m-d");
$rand = rand(98765432,23456789);
$table = '<center><script type="text/javascript" src="/js/sendmessage.js"></script>
<form action="cpanel.php?manage='.$useraccounts.'&rcad='.$viewcustomer.'&user='.$username.'&send='.$one.'" method="post" name="sendpm" onsubmit="return valid()">
<table>
<tr>
<td>
To:
</td>
<td>
'.$touser.'
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<input type="text" name="subject" id="subj1" />
</td>
</tr>
<tr>
<td>
Message:
</td>
<td>
<textarea name="message" cols="60" rows="10" id="mes1"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name = "submit" value="Submit" />
</td>
</tr>
</table>
</form></center>';
if (isset($submit))
{
$send = $_GET['send'];
if ($send == $one) {
include "maildbconnect.php";
$query = mysql_query("INSERT INTO mailtbl_admin VALUES ('', '$touser', '$fromuser', '$subject',
'$message', '0', '0', '1', '$date', '$rand')");
echo "Message successfully sent.";
}
}
}//end send message
}//end $getuser
}
}
The form for sending a message ($table) doesn't appear after clicking the link ($sendmessage) on a certain search result. After clicking the link, i'm prompted to an empty table (table not inside the while loop) but the $_GET function is working. Can anyone tell me how to fix this? thanks a lot
$submit wont be set when following the link. So any code after:
if(isset($submit)) {
Will not fire.
You've got a lot wrong with this form. First off, you forgot a <tr> and <td> after your <table> tag.
echo "<center><form name = 'searching' method='POST' action='cpanel.php?manage=".$useraccounts."&rcad=".$viewcustomer."'><table border = 1><tr><td colspan='10'>";
echo "<select name = 'filter'>";
echo "<option value ='username'>Username</option>";
echo "<option value ='username'>Username</option>";
echo "</select>";
echo " <input name='search' type='text' > ";
echo "<input type='submit' name='submit' value='Search'> ";
echo "<input type='submit' name='back' value='Back'><br><br>";
echo "</td></tr>";
Alternatively you could move the opening <table> tag below the <select> tag.
After you fix that, try moving the logic where you insert the values into mailtbl_admin outside of the loop. I'm guessing you're only going to want to send the message to one user, so it only makes sense to move it out of the loop.

Categories