6

I wonder whether someone could help me please.

I've been looking through this, and many other sites and tutorials to find out how to add a button to a form which opens a PHP file, in this case, a pop up form that allows a user to upload a file to a mySQL database.

In addition to the opening of the file, I'd like to carry over the 'id' field value from the main form to the pop 'File Upload' form.

From the research I've carried out there seems to be a number of ways to do this, but from a beginners perspective I'm not sure what is the best way to do this.

Could someone perhaps please advise on what is the best way to go about this.

Many thanks and kind regards

IRHM
  • 1,326
  • 11
  • 77
  • 130
  • Setting up an asynchronous upload is not really beginner territory. I'd suggest setting up a local WAMP server and going through the tutorials and learn it offline first. – Jared Farrish Dec 18 '11 at 16:08
  • When you say "pop up" do you mean a new browser window (with it's own title bar, etc.) or a light-box effect? – Brigand Dec 18 '11 at 16:10
  • Hi, yes it's a new browser window, rather than the light-box effect. Kinds regards. – IRHM Dec 18 '11 at 16:13

2 Answers2

4

To pass values between pages:

Main form:

<form action="myuploadform.php" method="get">
ID: <input type="text" name="id">
<input type="submit" value="Open Form">
</form>

The value of the ID text box will be accessible as $_GET['id'] in myuploadform.php.

Using GET parameters is the simplest way of passing values. Another way to pass in this GET value would be in the URL:

.../myuploadform.php?id=35 where the ID then becomes 35.

Tim
  • 14,447
  • 6
  • 40
  • 63
0

Here's a sample from my site. All it does is allow the uploading of files to the server. It should serve as a tutorial.

<html>
<head>
<script type="text/javascript">
var form_object = null;
var button_object = null;

function submit_form(obj)
         {
         form_object = obj.parentNode;
         form_object.submit();
         form_object.disabled = true;

         button_object = obj;
         button_object.disabled = true;
         }

function enable_form()
         {
         form_object.disabled = false;
         button_object.disabled = false;
         }

function Add_HTML(html)
         {
         if(navigator.appName == 'Microsoft Internet Explorer')
           {
           document.body.insertAdjacentHTML('beforeEnd', html);
           }

           //Firefox uses the Netscape engine (the Netscape version that really sucked)
           if(navigator.appName == 'Netscape' && parseInt(navigator.appVersion) == 5)
             {
             var freaky_object = document.createRange();
             freaky_object.setStartBefore(document.body);
             html = freaky_object.createContextualFragment(html);
             document.body.appendChild(html);
             }
         }
</script>
</head>

<body>

    <form action="upload.php" method="post" enctype="multipart/form-data" target="upload">
          <label>File:</label> <input type="file" name="file" />
          <br />
          <label>File:</label> <input type="file" name="swntic" />
          <br />
          <input type="button" value="SUBMIT"
                 onclick="submit_form(this);" />
    </form>

    <iframe src="about:blank" style="display:none;" id="upload" name="upload"></iframe>

</body>
</html>

server side code:

<?
$confirmation = "";

while(list($name) = each($HTTP_POST_FILES)) {
?>

<? if(is_uploaded_file($HTTP_POST_FILES[$name]["tmp_name"])) { ?>
<?= $HTTP_POST_FILES[$name]["name"] ?>
<br />
<?= $HTTP_POST_FILES[$name]["type"] ?>
<br />
<?= $HTTP_POST_FILES[$name]["tmp_name"] ?>
<br />
<?= $HTTP_POST_FILES[$name]["error"] ?>
<br />
<?= $HTTP_POST_FILES[$name]["size"] ?>
<br /><br />
<? } ?>

<?
if(is_uploaded_file($HTTP_POST_FILES[$name]["tmp_name"]))
  {
  move_uploaded_file($HTTP_POST_FILES[$name]["tmp_name"], "./uploads/" . $HTTP_POST_FILES[$name]["name"]);
  chmod("./uploads/" . $HTTP_POST_FILES[$name]["name"], 0644);

  $confirmation .= "<a href=\"./uploads/" . $HTTP_POST_FILES[$name]["name"] . "\">" .
                   $HTTP_POST_FILES[$name]["tmp_name"] . "</a> " . $HTTP_POST_FILES[$name]["type"] . ", " . $HTTP_POST_FILES[$name]["size"] . " bytes<br />";
  }
}
?>

<html>

<script>
var confirmation = '<?= $confirmation ?>';
</script>

<body onload="parent.enable_form(); parent.Add_HTML(confirmation);">
</body>
</html>

It's not perfect, but can be used as a learning tool.

Frank
  • 459
  • 3
  • 9
  • This isn't actually related to the question but seems like a good example for webpage to server interaction, helpful to newbs like me :) – D J Aug 20 '22 at 13:29