Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing form values and moving to another page

I am beginner in web development and doing HTML with PHP and MySQL. I am trying to read data from a user using a form, and on the click "save", I need to submit the form values to the same file and move to another page.

The values are used by PHP object in the same file in order to invoke a method that handles these values.

Unfortunately, the submitted values are lost and seem to go nowhere. Further, I am not moving to the desired (another) page.

Here's my form. ( the current file is named "Insert.php" and I am showing one element only).

<form method="post" action= "Insert.php">
<fieldset>
<legend>Insert New Data </legend>
<p> Service Name : 
<select name="service_name">
<option value=""> </option>

<?php

$result = $db_link->query("SELECT * FROM servicetype_lookup "); 

while($row= $result->fetch_row()) {
$id = $row[0];
$value = $row[1];
echo "<option value='$id'>$value</option>";
}
    ?>
    </select>

.........
.........

In the same file "Index.php", I am sending the values to a function in another file that contains a class called "AccountHolder" as the following:

<?php


if(isset ($_POST['service_name'] ) )
{

$account_holder= new AccountHolder; 
$result= $account_holder->insert($_POST['service_name'] , 
                                 $_POST['reference'],
                                 $_POST['title'],
                                 $_POST['risk_rating'],
                                 $_POST['root_cause'],
                                 $_POST['impact'],
                                 $_POST['likelihood'],
                                 $_POST['efforts'],       
                                 $_POST['finding'],
                                 $_POST['implication'],
                                 $_POST['recommendation'] );  


}

?>

and finally I have this submit button that should move to another page called "Database.php" (This is not the page that contains the class AccountHolder):

   <br/><input type="submit" value=" Save " onclick="window.location.href='Database.php'" />

Can someone help? Thank you.

like image 820
Traveling Salesman Avatar asked Dec 18 '25 03:12

Traveling Salesman


1 Answers

Change the submit button to this:

<input type="submit" value=" Save " />

When clicking "Save" it will take you to Insert.php Dou your stuff (inserting) there.

When done inserting, before echoing anything do

Header( 'Location: Database.php');

or any other location that the user must see after the insert.

You should change your test to see if something has submitted from

if(isset ($_POST['service_name'] ) ) {

into

if( ($_SERVER['REQUEST_METHOD'] == 'POST') ) {
like image 154
JvdBerg Avatar answered Dec 19 '25 19:12

JvdBerg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!