passing variables through session - php - php

I'm new to php and I'm trying to pass a the value of a submit button through a session.
my code so far looks like this:
main_page.php
session_start();
echo '<form method="post" action="details_page.php">';
echo '<li><a href="ifconfig.php> <input type="submit" name=submit value='."$ip_address".' /></a></li>';
echo '</form>';
$value_to_pass = $_POST[submit];
echo $value_to_pass;
$_SESSION['value'] = $value_to_pass;
}
details_page.php
session_start();
echo $value_to_pass = $_SESSION['value'];
echo $value_to_pass;
I need it to print $value_to_pass in the details_page.php

This is highly confusing
session_start();
echo '<form method="post" action="details_page.php">';
echo '<li><a href="ifconfig.php> <input type="submit" name=submit value='."$ip_address".' /></a></li>';
echo '</form>';
$value_to_pass = $_POST[submit];
echo $value_to_pass;
$_SESSION['value'] = $value_to_pass;
Consider changing it to this so that your POST code only gets executed when the form is actually submitted. Without this check your SESSION will be assigned a blank value when the form is not submitted which may be giving you odd results.
session_start();
echo '<form method="post" action="details_page.php">';
echo '<li><a href="ifconfig.php> <input type="submit" name=submit value='."$ip_address".' /></a></li>';
echo '</form>';
// Note also you need single quotes in the $_POST array around 'submit'
if(isset($_POST['submit']))
{
$value_to_pass = $_POST[submit];
echo $value_to_pass;
$_SESSION['value'] = $value_to_pass;
}
And change details_page.php to
session_start();
// Do not echo this line, as you are echoing the assignment operation.
$value_to_pass = $_SESSION['value'];
var_dump($value_to_pass);

first You change your below code
echo '<form method="post" action="details_page.php">';
echo '<li><a href="ifconfig.php> <input type="submit" name=submit value='."$ip_address".' /></a></li>';
echo '</form>';
with the below
echo '<form method="post" action="details_page.php">';
echo '<input type="hidden" name="ip_address" id="ip_address" value="'.$ip_address.'">';
echo '<li><a href="ifconfig.php> <input type="submit" name=submit /></a></li>';
echo '</form>';
this is the best practice actually. I also followed this. So try to avoid passing values in "submit" button. pass it to "hidden" field.
and then get the value like below:-
$value_to_pass = $_POST["ip_address"];
I think it will help You. Thanks.

Related

How can I pass variables from hybrid .php/HTML file to another .php file? -- I've tried all the methods I've found on-line [duplicate]

I have the following code:
<?php
$student_no = $_GET['student_no'];
echo '<form name="student" action="PROCESS_FEE007.php" method="POST">';
echo '</br><table>';
echo '**<input name="student_no" type="hidden" value="$student_no" />**';
echo '<td>Amount: </td><td>'.'<input name="amount" type="text" /></td></tr>';
echo '<tr> <td>Remarks: </td><td>'.'<input name="remarks" type="text" /> </td>';
echo '<tr> <td>';
echo '<td>'.'<input type="submit" value="Save"/></td></tr>';
echo '</table>';
echo '</form>';
?>
On the next page PROCESS_FEE007.PHP the value is not received.
Variables are not parsed by interpreter inside single quotes. You should use double quotes or explicit string concatenation.
In your example the value of $_POST['student_no'] will be string '$student_no', not the value of the $student_no variable.
Besides if you're using method="POST" in your form, you can only get the inputs value through the $_POST array.
<?php
$student_no = $_POST['student_no'];
echo '<form name="student" action="PROCESS_FEE007.php" method="POST">';
echo '</br><table>';
echo '**<input name="student_no" type="hidden" value="'.$student_no.'" />**';
echo '<td>Amount: </td><td>'.'<input name="amount" type="text" /></td></tr>';
echo '<tr> <td>Remarks: </td><td>'.'<input name="remarks" type="text" /> </td>';
echo '<tr> <td>';
echo '<td>'.'<input type="submit" value="Save"/></td></tr>';
echo '</table>';
echo '</form>';
?>
parse student_no in form as
<?php
$student_no = $_GET['student_no'];
echo '<form name="student" action="PROCESS_FEE007.php" method="POST">';
echo '</br><table>';
echo '**<input name="student_no" type="hidden" value="'.$student_no.'" />**';
echo '<td>Amount: </td><td>'.'<input name="amount" type="text" /></td></tr>';
echo '<tr> <td>Remarks: </td><td>'.'<input name="remarks" type="text" /> </td>';
echo '<tr> <td>';
echo '<td>'.'<input type="submit" name="submit_save" value="Save"/></td></tr>';
echo '</table>';
echo '</form>';
?>
and on the PROCESS_FEE007.php page use
<?php
if ($_POST['submit_save']){
var_dump($_POST);die();
}
?>
check the attribute "VALUE" of hidden input field. The value is not put in the field.
First make the input field a text box and after fixing the bug make it a hidden field.
may be useful. (I forgot cuz I am out of PHP long time).
try using $_REQUEST instead of get example $student_no = $_REQUEST['student_no'];

html+php+database (very beginner)

I'd like to ask for help about my code. I'm still beginning to find way with html, php, databases and all that. This is something I already did some times, but somehow I got stuck at this point.
The connection is fine, so I omitted it.
The thing is that my buttons don't do anything when I click them, it's like there's no action to be taken. At this point I can't notice my mistake, but I'm sure it's very simple. :(
I've translated some things from the code from my native language to English, so if you find it inconsistent in that regard, I'm sorry. *
I'd guess it's something I've messed up on the table structure
Thank you!
<?php
echo "<th>City |</th>";
echo "<th>Update |</th>";
echo "<th>Delete |</th>";
echo "</tr>";
$query = "SELECT name, id_city as id FROM city";
$result = pg_query($conn, $query);
if($result) {
while($row = pg_fetch_assoc($result)) {
echo '<tr>';
echo '<td>';
echo $row['name'];
echo '</td>';
echo '<td>';
echo '<form method="post" action="./updatecity.php">';
echo '<input type="hidden" name=id_city value="'.$row['id'].'">';
echo '<input type="submit" name="submit" value="Update">';
echo '</form>';
echo '</td>';
echo '<td>';
echo '<form method="post" action="./deletecity.php">';
echo '<input type="hidden" name=id_city value="'.$row['id'].'">';
echo '<input type="submit" name="submit" value="Delete">';
echo '</form>';
echo '</td>';
echo '</tr>';
}
}
pg_close($conn);
?>
Your approach to making form is completely wrong, what you are doing in simply echoing texts, No form is getting generated. Hence the button is not working because there is no button.
What you need to do it something like this :
<?php
#Write your php related code here
#Like connecting to database
?>
#All your html related content goes here like making tables
<form method="post" action="./updatecity.php">
<input type="hidden" name=id_city value="<?php echo $row['id'] ?>">
<input type="submit" name="submit" value="Update">
</form>
<?php
//Write your php related code here
?>

PHP form submission with echo html post's empty array

I'm using php to dynamically create multiple buttons on my web page that have increasing values 01 to 09. When I create the forms as below the buttons are created as expected with the correct values but when I submit the form an empty array is posted.
The code here is inside a for loop that increments $i:
echo '<form action="test.php" method="post">';
echo '<input type="hidden" name="RunSmokeTest" value="0'.$i.'">';
echo '<input type="submit" name="submit" value="Run Test">';
echo '</form>';
Here is the HTML that is created:
However, when I change the code to take out the $i variable, the form will be submitted as expected but all the buttons will have the same value which I can't have.
Here is the start of test.php that prints out array(0) { } when I click the submit button and an alert comes up with message 'empty'.
var_dump($_POST);
if(empty($_POST['RunSmokeTest']))
{
echo '<script language="javascript">';
echo 'alert("empty")';
echo '</script>';
}
else{
...
}
Try this .
<?php
if(isset($_POST['submit'])) {
echo $_POST['RunSmokeTest'];
}
for ($i=0; $i < 10; $i++) {
echo '<form action="test.php" method="post">';
echo '<input type="hidden" name="RunSmokeTest" value="0'.$i.'">';
echo '<input type="submit" name="submit" value="Run Test">';
echo '</form>';
}
?>
Demo is here

how to use two form's post value together

I have a form on a page like:
<form action='search.php' method='POST'>
<input type='text' name='specialist' />
<input type='submit' name='submit' />
</form>
on search page there is another form like
<form action='' method='POST'>
<input type='submit' name='anygender' />
</form>
then i am using
if(isset($_POST['anygender'])){
$speciality = $_POST['speciality'];
echo $speciality;
$a = mysql_query("SELECT * FROM find_doctor WHERE doctor_type LIKE '%$speciality%'");
while ($b = mysql_fetch_array($a)){
echo "<img src='$b[image]' height='150px' width='300px'>"."</br>";
echo $b['name']."</br>";
echo $b['doctor_type']."</br>";
echo $b['location']."</br>";
echo $b['insurance']."</br>";
echo $b['comments']."</br>";
echo $b['address']."</br>";
}
}
then $specialist is showing blank and sql query not working..I want to use both form's post value together.Please tell me how to use first form post value in this. Thanks in advance
Why you need two forms?
<form action='index.php' method='POST'>
<input type='text' name='specialist' />
<input type='submit' name='anygender' />
<input type='submit' name='submit' />
</form>
Maybe you can use one form and check buttom?
Use this code. Check $_POST['submit'] in if condition.
if(isset($_POST['submit'])){
$speciality = $_POST['speciality'];
echo $speciality;
$a = mysql_query("SELECT * FROM find_doctor WHERE doctor_type LIKE '%$speciality%'");
while ($b = mysql_fetch_array($a)){
echo "<img src='$b[image]' height='150px' width='300px'>"."</br>";
echo $b['name']."</br>";
echo $b['doctor_type']."</br>";
echo $b['location']."</br>";
echo $b['insurance']."</br>";
echo $b['comments']."</br>";
echo $b['address']."</br>";
}
}

A form (action) error

I have a question, I have a page and a form that I direct it to itself to edit data to database.
I need a way to use the form and redirect itself with action=editcinemas.php?cinema_id=THE ID OF CINEMA
<html>
<body>
<form name="form3" method="post" action="editcinemas.php"> //MY PROBLEM IS HERE I GUESS
<td>
<table>
<?php
require('../classes/cinema_class.php');
$cinema=new cinema($_GET["cinema_id"]);
$cinemas = $cinema->get_all_cinemas();
foreach ($cinemas as $movie)
{
$cinema_id = $movie['cinema_id'];
$cinema_name = $movie['cinema_name'];
$cinema_location = $movie['cinema_location'];
}
echo "<input name='cinemaid' type='hidden' id='cinemaid' value=" . $cinema_id . '><br/>';
echo "Cinema Name :";
echo "<input name='cinemaname' type='text' id='cinemaname' value=" . $cinema_name . '><br/>';
echo "Cinema Location :";
echo "<input name='cinemalocation' type='text' id='cinemalocation' value=" . $cinema_location . '><br/>';
?>
</table>
<input type="submit" name="Edit" value="Edit"> <input type="reset" name="Reset" value="Reset"/><INPUT Type="button" VALUE="Back" onClick="history.go(-1);return true;">
</form>
</body></html>
<?php
$cinemaid=$_POST['cinemaid'];
$cinemaname=$_POST['cinemaname'];
$cinemalocation=$_POST['cinemalocation'];
if ($_POST) {
if(empty($cinemaname)){
echo "Cinema Name is a must"; }
else{
$update_movie=mysql_query("UPDATE `memoire`.`cinema`
SET `cinema_name`= '$cinemaname', `cinema_location`= '$cinemalocation'
WHERE `cinema_id`='$cinemaid'");
if($update_movie)
{ header("Refresh: 0;url=../listallcinemas.php");}
else
{ echo "error in registration".mysql_error(); }
}
?>
Just simply append it, like this:
<form name="form3" method="post" action="editcinemas.php?cinema_id=<?php echo $_GET["cinema_id"]; ?>">

Categories