Page 1 of 1

Extracting a form from SQL to HTML text fields

Posted: Sun Mar 07, 2004 12:32 pm
by Shujaa
Got a MySQL database storing user submitted application forms, storing basically Name, Address etc etc. Now I want users to be able to check on their data and be able to edit it again after submitting.

I can easily pull out the data from MySQL using a query and put it in an array variable. I can then split the relevant array elements into simple string variables for each column of data. From that I can put the string variables into text input boxes as default values, so the user can see their data and edit it directly and resubmit. However, I don't know how to do this for Drop Down Lists, or for Radio Buttons. Can anyone help on this? Sample code/tutorials would be much appreciated,

thanks

Posted: Sun Mar 07, 2004 1:27 pm
by JAM
Not sure if this is what you are looking for, but... Play around abit with it.

Code: Select all

<pre>
<?php
    print_r($_POST);
?>
<form method="post">
  <select name="foo">
    <option>1</option>
    <option>2</option>
    <option>3</option>
  </select>
  <input type="radio" name="bar" value="foo" />
  <input type="radio" name="bar" value="kitty" />
  <input type="radio" name="bar" value="beer" />
  <input type="submit">
</form>
...and also this link might be of interest.

Posted: Sun Mar 07, 2004 1:33 pm
by Shujaa
Ahaaaaaa! That looks useful, still not 100% sure but I bet I can figure it out playing with that code...

Posted: Sun Mar 07, 2004 2:39 pm
by Shujaa
Related question: how do I pass a variable (say, a string) between PHP pages? I checked the documentation for variable scopes and can't see any way to increase the scope beyond one page.

Posted: Sun Mar 07, 2004 4:42 pm
by AVATAr
using forms: $_POST, $_GET
using urls: $_GET
using sessions: $_SESSION
check http://www.php.net/manual/en/language.v ... efined.php

Posted: Mon Mar 08, 2004 2:48 am
by Shujaa
I find it difficult understanding the documentation for POST and GET, could someone please take me through the basics of it? I know how to use $_POST to get data that has been submitted through an HTML form, but I can't seem to get it to do anything else. How do I tell a script to "pass" the variables along into the $_POST variable? And how do I tell a script to take variables back out from $_POST?

I appreciate your help, thanks.

Posted: Mon Mar 08, 2004 3:26 am
by Phirus

Posted: Mon Mar 08, 2004 5:47 am
by Shujaa
Hmm I'm starting to understand, but still have a major question:

Is it possible to pass variables from one PHP script to the next WITHOUT using html forms? Because as far as I can tell, using GET and POST rely on the user pressing a Submit button of some kind...

Posted: Mon Mar 08, 2004 5:53 am
by twigletmac
Yes - use sessions. E.g:

page1.php

Code: Select all

<?php
session_start();

$_SESSION['var1'] = 'foo';
$_SESSION['var2'] = 'bar';

?>
<a href="page2.php">Go to page 2...</a>
page2.php

Code: Select all

<?php
session_start();

echo 'The value of $_SESSION[var1] is: '.$_SESSION['var1'];
echo '<br />';
echo 'The value of $_SESSION[var2] is: '.$_SESSION['var2'];
?>
Mac

Posted: Mon Mar 08, 2004 5:59 am
by AVATAr
(Using twigletmac code) And - use GET without form. E.g:

page1.php

Code: Select all

<a href="page2.php?sVar1=foo&sVar2=bar">Go to page 2...</a>
page2.php

Code: Select all

<?php

echo 'The value of $_GETsVar1] is: '.$_GET['sVar1'];
echo '<br />';
echo 'The value of $_GET[sVar2] is: '.$_GET['sVar2'];
?>
You have to validate those variables, because the user an change them into mallicious code.

Posted: Mon Mar 08, 2004 9:07 am
by Shujaa
$_SESSION looks like what I need. I'd rather not use GET method at all if I can help it, because of the security/validation issues that can arise. Thanks a lot :D