Current location: Hot Scripts Forums » General Web Coding » JavaScript » Submit a Javascript button and execute PHP


Submit a Javascript button and execute PHP

Reply
  #1 (permalink)  
Old 09-26-06, 12:17 AM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
Submit a Javascript button and execute PHP

Hi
I have the following JavaScript function:

Code:
<script type="text/javascript">
function Attach()
{
  window.location = "test.php?do=upload";
}
</script>
and the following HTML form:

Code:
<form name="foo" action="test.php" method="post" enctype="multipart/form-data">
<input type="file" name="userfile" size="20" onkeypress="KeyPress()"  >
<input type="button" name="button" onclick="Attach();">
I select a file for upload and I click on button. Button tell JavaScript function to execute the code and redirect the page to : test.php?do=upload

Now I have this code for PHP:

Code:
if ($_GET['do'] == 'upload')
{
	print $_FILES['userfile']['name'];  // No output, why ? because the file is selected in HTML form
}
But i don't see an output for print. Why ?

Thanks !
Reply With Quote
  #2 (permalink)  
Old 09-26-06, 02:32 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
You have to submit the form. What your code does it just go to that page, but no data is sent to the server. You can point the form to test.php?do=upload in the action attribute.
Code:
<form name="foo" action="test.php?do=upload"
But you still have to submit the form. Change the button to "submit" and it will work.

Code:
<form name="foo" action="test.php?do=upload" method="post" enctype="multipart/form-data">
<input type="file" name="userfile" size="20" onkeypress="KeyPress()"  >
<input type="submit" name="button" value="Upload">
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
execute php into javascript function Hagoromo PHP 3 05-23-07 02:19 AM


All times are GMT -5. The time now is 11:19 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.