[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 721
  • Last Modified:

Passing value form html to php

Hi experts,

I’m uploading files from computer to server using this method
http://code.google.com/p/upload-at-click/

Right now the php code is :
<?php
$tmp_file_name = $_FILES['Filedata']['tmp_name'];
$target_path = "/TARGET_PATH/";
$target_path = $target_path . basename($_FILES['Filedata']['name']);
$ok = move_uploaded_file($tmp_file_name, $target_path);

// This message will be passed to 'oncomplete' function
echo $ok ? "OK" : "KO";

However, I would like to modify a little bit the php code in order to move files to different target paths.
Eg
$target_path = "/TARGET_PATH/VARIABLE/";

The “VARIABLE” value comes from a hidden input text file in the form

<input name="field" type="hidden" id="field" size="13" value="VARIABLE" />

I don’t know how to pass a value form an html field to php.

Thank you for your advice,

Regards,
0
alcani
Asked:
alcani
  • 2
  • 2
  • 2
  • +3
1 Solution
 
ChrisCommented:
If you had the following form
<form action="page.php" method="post" >
      <input name="field" type="hidden" id="field" size="13" value="VARIABLE" /></input>
</form>

Open in new window

You'd retrieve the value like so.
<?php
      $target_path = $_POST['field'];
?>

Open in new window

The "name" attribute is what you need to use to retrieve the value.
0
 
ChrisCommented:
This is also worth a look:

http://www.tizag.com/phpT/postget.php
0
 
leakim971PluritechnicianCommented:
<form method="POST"...
<input name="fieldname" type="hidden" id="field" size="13" value="VARIABLE" />

$v = $_POST["fieldname"]
$target_path = "/TARGET_PATH/" . $v . "/";

Open in new window

;

http://matthom.com/archive/2005/02/19/php-passing-variables-across-pages
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Ray PaseurCommented:
Start your learning here:
http://us2.php.net/tut.php

As a practical matter, you would never want to rely on unfiltered external input to decide a file path on your server.  Make sure that your code checks the "VARIABLE" carefully!
0
 
Hugh McCurdyCommented:
Here's a live example with source code.

http://hugh.tekcities.com/hugh/program/web/phpFormSimple.php
0
 
hieloCommented:
>> I don’t know how to pass a value form an html field to php.
>> <input name="field" type="hidden" id="field" size="13" value="VARIABLE" />

Try supplying "action_params" -ex:

<script type="text/javascript">

   var uploader = document.getElementById('uploader');

   upclick(
     {
      element: uploader,
      action: '/path_to/you_server_script.php',
      action_params:{'field':'VARIABLE'},
      onstart:
        function(filename)
        {
          alert('Start upload: '+filename);
        },
      oncomplete:
        function(response_data)
        {
          alert(response_data);
        }
     });

   </script>
0
 
alcaniAuthor Commented:
Thank you for your comments,

In fact I’m not using “POST” that’s why it seems to be more difficult to pass the value.

Looking to hielo answer, I would like to pass “value” instead of VARIABLE because it’s not a fixed value.

Something like that
upclick(
     {
      element: uploader,
      action: '/path_to/you_server_script.php',
      action_params:{'field':'value'},
      onstart:
        function(filename)
        {
          alert('Start upload: '+filename);
        },
      oncomplete:
        function(response_data)
        {
          alert(response_data);
        }
     });

   </script>

Next step is to catch this value in php, how can I do it?

I was thinking something like that??

<?php
$value = $_FILES['field']['value'];

Thks,
0
 
hieloCommented:
In fact I’m not using “POST” that’s why it seems to be more difficult to pass the value.
You are using this via a browser aren't you?  That being the case, in order to upload a file, your browser will do so using a POST method only.  So that script must be doing a POST under the hood (without you knowing of course).

Looking to hielo answer, I would like to pass “value” instead of VARIABLE because it’s not a fixed value.
I highlighted the word VARIABLE because in your initial (sample) <input> field you have '...value="VARIABLE"...'.  I thought it would be clear that whatever you assign to the value attribute is what is actually passed.  To clarify, if you had:
<input type="hidden" name="id" value="23"/>

then the following should pass 23 to the server.  You should be able to retrieve it from the $_POST array (NOT from $_FILES):

<html>
<head>
<script type="text/javascript" src="/path_to/upclick-min.js"></script>
</head>
<body>
<input type="hidden" name="id" id="clientID" value="23"/>
 <input type="button" id="uploader" value="Upload">
<script type="text/javascript">
upclick(
     {
      element: uploader,
      action: '/path_to/you_server_script.php', 
      action_params:{'id': document.getElementById("clientID").value },
      onstart:
        function(filename)
        {
          alert('Start upload: '+filename);
        },
      oncomplete:
        function(response_data) 
        {
          alert(response_data);
        }
     });
</script>
</body>
</html>

Open in new window


Next step is to catch this value in php
Like I said before, if you are successfully uploading a file, then the action_params will be in $_POST. So on the example above you can verify the value 23 on the server via:
<?php
echo $_POST['id'];
?>
0
 
alcaniAuthor Commented:
Thanks a lot hielo!
Great answer!!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now