Jump to content


Photo

passing variables from one form to another


  • Please log in to reply
6 replies to this topic

#1 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 30 May 2006 - 11:32 PM

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

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;

and here's edituser_form1

<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>

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.

#2 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 31 May 2006 - 12:32 AM

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
"Syntax error" .. WHERE THE **** IS IT?!

#3 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 31 May 2006 - 01:20 AM

[!--quoteo(post=378547:date=May 30 2006, 08:32 PM:name=legohead6)--][div class=\'quotetop\']QUOTE(legohead6 @ May 30 2006, 08:32 PM) View Post[/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

#4 massive

massive
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 31 May 2006 - 08:16 AM

Form1.php

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

?>
<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>

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
<? 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;
?>
// here i outputted the last name of the user from form1 by using $_GET['id']

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

<a href =edit.php?id=<? echo $row->id?>>



hope this helps :)



#5 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 31 May 2006 - 09:10 AM

There are two main ways of passing data from one page to another and that is decided in the followng code:
<form method="???">
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:
<form action="test.php" action="get">
<input type="text" name="name">
<input type="text" name="age">
<input type="submit" name="subsend">
</form>
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.
Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them

#6 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 31 May 2006 - 02:17 PM

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

#7 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 31 May 2006 - 11:56 PM

anyone...?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users