最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Passing form values and moving to another page - Stack Overflow

programmeradmin1浏览0评论

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['remendation'] );  


}

?>

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.

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['remendation'] );  


}

?>

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.

Share Improve this question asked Sep 3, 2012 at 11:41 Travelling SalesmanTravelling Salesman 2,28112 gold badges50 silver badges88 bronze badges 2
  • What errors are you getting (if any)? – user399666 Commented Sep 3, 2012 at 11:45
  • no errors, the page just refreshes on "save" click – Travelling Salesman Commented Sep 3, 2012 at 11:46
Add a ment  | 

3 Answers 3

Reset to default 1

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') ) {

you can send you data from one php page to another page

index.php submit.php

$username=$_post['name'];
$Password=$_post['pass'];

on submit.php

$x=$username;
$y=$password;

Change the submit button to this:

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

Then in the below code do edits as shown below:

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

$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['remendation'] );  
                             RedirectToURL("DESIRED PAGE");

}

?>

Note the line RedirectToURL("table.php"); in the above code. This is the function that redirects to a new page after saving the values . How to create Redirect to url function is shown below:

 function RedirectToURL($url)
 {
    header("Location: $url");
    exit;
 }
发布评论

评论列表(0)

  1. 暂无评论