Jump to content

passing variables from one form to another


digitalgod

Recommended Posts

Hey guys,

I have 2 forms, one that let's you choose a user to edit and the other is for the actual editing.

What I'm trying to do is when you select a user to edit and click on Edit it will take you to the 2nd form with all the input boxes filled with the correct info from the db.

Should be very simple but I'm using a template system that doesn't seem to allow me to pass information like that.

here's an example

[code]
case "edituser":
      $process=$_POST['process'];
        if ($process == "yes") {
        $process_b=$_POST['process_b'];
        $username=$_POST['username'];
        
          if ($process_b == "yes") {
          $new_uname=$_POST['new_uname'];
          $new_email=$_POST['new_email'];
          $new_fname=$_POST['new_fname'];
          $new_lname=$_POST['new_lname'];
          $new_active=$_POST['new_active'];
            if (strlen($new_uname) < 5 || strlen($new_email) < 6 || strlen($new_name) < 3 || strlen($new_active) < 2) {
            $tmpl->add_template("edituser_tooshort");
            }
            else {
            mysql_query("UPDATE ".$prefix."users SET username='$new_uname',email='$new_email',fname='$new_fname',lname='$new_lname',active='$new_active' WHERE username='$username'") or die(query_error());
            $tmpl->add_template("edituser_success");
            }
          }
          else {
          $result=mysql_query("SELECT * FROM ".$prefix."users WHERE username='$username'") or die(query_error());
          $row=mysql_fetch_array($result);
            if ($row['id'] != "") {
            $tmpl->add_template("edituser_form2");
            }
            else {
            $tmpl->add_template("username_no");
            }
          }
        }
        else {
        $tmpl->add_template("edituser_form1");
        }      
      break;
[/code]

and here's edituser_form1

[code]
<div id="article">
<form action="admin.php?a=edituser" method="post">
<input type="hidden" name="process" value="yes" />
<table border="0">
<tr>
<td>Username:</td>
<td><select name="username" id="username">
  <option>Choose one</option>
  <?php
$sql='SELECT * FROM mtl_users';
$req=mysql_query($sql) or die(query_error());
while($data = mysql_fetch_assoc($req)) {
    echo '<option value="'.$data['username'].'">'.$data['username'].'</option>';
}
?>
</select></td>
</tr>
</table>
<br />
<input type="submit" value="Edit User" />
</form>
</div>[/code]

so when that form is submitted admin.php collects the username $username=$_POST['username']; but if I echo $username in edituser_form2 it won't display anything so I can't really query the db to fill up the input boxes...

is there any way I can make this work without having to put the 2nd form in admin.php?? I prefer having the forms in seperate files just so that everything stays clean. Hope I made myself clear.

would really appreciate any help I can get.
Link to comment
Share on other sites

[!--quoteo(post=378547:date=May 30 2006, 08:32 PM:name=legohead6)--][div class=\'quotetop\']QUOTE(legohead6 @ May 30 2006, 08:32 PM) [snapback]378547[/snapback][/div][div class=\'quotemain\'][!--quotec--]
use GET method to send the user id to the edit page then go $id=$_GET['field of id']; connect to db and put WHERE id=$id
[/quote]


can you please elaborate? The form itself submits to admin.php not to the second form
Link to comment
Share on other sites

Form1.php

[code]
<?
         $sql     = "SELECT * FROM users WHERE user_id = 1";
         $result = mysql_query($sql) or die (mysql_error());

?>
[/code]
[code]
<html>


<? if (mysql_num_row($result) > 0)
    {
?>
      <table>
                   <? while($row = mysql_fetch_object($result))
                        {  ?>
        <tr>
              <td><? echo $row->FirstName ?> </td><td><a href =edit.php?[b]id=<? echo $row->id?>[/b]>
              </td>
       </tr>
       <? } ?>
     </table>
     <? } ?>
</html>
[/code]

you have notice why i've bold the link so you can easily identify what i'm pointing out here :) let's continue


Form2.php
[code]
<? if((!isset($_GET['id']) || trim($_GET['id']) == ''))
        {
            die('Missing record ID!');
        }

    $id     = $_GET['id'];
    $mysql  = "SELECT * FROM user WHERE user_id = '$id'";
    $result = mysql_query($mysql) or die(mysql_error());
    $row    = mysql_fetch_object($result);



echo <? $row->LastName;
?>
[/code]
// here i outputted the last name of the user from form1 by using [b]$_GET['id'][/b]

the "id" inside the $_GET[' '] was declared from form.1:

<a href =edit.php?[b]id[/b]=<? echo $row->id?>>



hope this helps :)

Link to comment
Share on other sites

There are two main ways of passing data from one page to another and that is decided in the followng code:[code]<form method="???">[/code]Where ??? is either "get" or "post".

When using POST the data contained within the form is sent to the page in packets of data. If using GET the data is appended onto the end of the URL. For example, if you have the following form:[code]<form action="test.php" action="get">
<input type="text" name="name">
<input type="text" name="age">
<input type="submit" name="subsend">
</form>[/code]If you typed in "rupert" as the name and "34" as the age the URL would be something like this:
test.php?name=rupert&age=34

Then you can use $_GET to retrieve the information from the URL. When using method="post" you'll need to use $_POST to retrieve the data. I always prefer to use the POST method because its all too easy for someone to edit the URL and send their own data but then again, its up to you to verify any data received before using it. The POST method does not affect the URL.
Link to comment
Share on other sites

thanks guys but I think you're missing my point...

the first form is named edituser_form1.php and the second is named edituser_form2.php

edituser_form1.php submits to admin.php which controls everything, so admin.php does the mysql query to check if the user exists, if so it will include edituser_form2 if not it will include username_no

so I can't really use GET... is there another way I can do this? admin.php stores the variables and I don't understand why edituser_form2 doesn't want to see those varibables...

any ideas?

edit

oh and I can't add a varible to the link from admin.php (ie edituser_form2.php?u=user1) because the way I made the template system is that I just type the name of the file and it adds the .php by itself.... changing that now would be a really big hassle...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.