tln6482 Posted July 29, 2010 Share Posted July 29, 2010 Is it possible to allow a JS function to resubmit a previous form with added variables in the url? Here is what I have so far: window.location = 'http://mydomain.com/form.php?var1=true'; It reloads but obviously the form that the user filled out in my index.html does not get resubmitted with this.... Ideas? Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 29, 2010 Share Posted July 29, 2010 I do not believe JS has "access" to POSTed values on the receiving page. So, I see two options: 1. Have the values from the form sent via GET and JS can read the values from the query string. I believe I have a function that will do that. But, obviously, this isn't the most aesthetically pleasing option since the query string will be long and ugly. 2. If you still want to have the form data submitted via POST then the server-side code will need to populate the form values in a way that JS can work with them. You could store the values as JS variables or recreate the original form with the values populated. You could even make all the fields hidden so the form is invisible to the user. Then use JS to resubmit. However, I really can't think of any situation where this would make sense as there would be better solutions using server-side code. For example, on the receiving page if you are initially saving the data to a database you could write the unique ID for that record to the page and use that in the URL. When passed the processing page can reference back to the original POST data. That's just one example. If you provide more information of what you are really trying to achieve a better solution might be found. Quote Link to comment Share on other sites More sharing options...
tln6482 Posted July 29, 2010 Author Share Posted July 29, 2010 Well, I have a php upload form setup. I am trying to give the user the option to overwrite if a file already exists, or not to overwrite if they don't care to. I'm using JS to popup a confirm box then based on whats returned, I have it calling the reload, inwhich I add the var value to the end of the URL. Based on that var, I'll have php determine whether to overwrite file or not........is that better? Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 29, 2010 Share Posted July 29, 2010 I would suggest a PHP only solution. This *may* work, I've explain why it may not and an alternative solution below. 1. User submits form 2. PHP processing page determines that the file is a duplicate 3. The processing page recreates the form as hidden fields giving the user options to "Overwrite" (submits again w/ confirmation) or "Cancel" (goes back to the orginal form for user to make a different submission) However, I see one big potential problem (which would be the case for a JS solution as well). I doubt if repopulating a file input field (especially with it hidden) will work. That would seem like a huge security problem. So, another alternative is if the file is a duplicate then the processing page can save the data to a temp table and the file to a temporary location. Then give the user the option to overwrite or not. If the user chooses to overwrite then move the data and file from the temp locations to the normal ones. If the user cancels, then delete the data. That way the data is already saved and there are no security issues. Quote Link to comment Share on other sites More sharing options...
tln6482 Posted July 29, 2010 Author Share Posted July 29, 2010 I'm having some syntax problems I guess when trying to do the hidden input fields.......is anything standing out to you? //Does this file already exist? if (file_exists($upfile_flv)) { echo "<h1>" . $exists . "</h1>"; echo "<input type='hidden' name='flv' value=$_FILES['flv']>"; echo "<input type='hidden' name='avi' value=$_FILES['avi']>"; echo "<input type='hidden' name='course' value=$course>"; echo "<input type='hidden' name='finit' value=$finit>"; echo "<input type='hidden' name='lname' value=$lname>"; echo "<input type='hidden' name='date' value=$date>"; echo "<input type='hidden' name='title' value=$title>"; echo "<input type='hidden' name='description' value=$description>"; echo "<input type='button' name='overwrite' value='Overwrite'>"; echo "<input type='button' name='cancel' value='Cancel'>"; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 29, 2010 Share Posted July 29, 2010 Well, it would be helpful if you provided the error! I typically enclose variable in curly braces when using then inside a double quoted string. //Does this file already exist? if (file_exists($upfile_flv)) { echo "<h1>{$exists}</h1>\n"; echo "<input type=\"hidden\" name=\"flv\" value=\"{$_FILES['flv']}\" />\n"; echo "<input type=\"hidden\" name=\"avi\" value=\"{$_FILES['avi']}\" />\n"; echo "<input type=\"hidden\" name=\"course\" value=\"{$course}\" />\n"; echo "<input type=\"hidden\" name=\"finit\" value=\"{$finit}\" />\n"; echo "<input type=\"hidden\" name=\"lname\" value=\"{$lname}\" />\n"; echo "<input type=\"hidden\" name=\"date\" value=\"{$date}\" />\n"; echo "<input type=\"hidden\" name=\"title\" value=\"{$title}\" />\n"; echo "<input type=\"hidden\" name=\"description\" value=\"{$description}\" />\n"; echo "<input type=\"button\" name=\"overwrite\" value=\"Overwrite\"}\" />\n"; echo "<input type=\"button\" name=\"cancel\" value=\"Cancel\" />\n"; } Quote Link to comment Share on other sites More sharing options...
tln6482 Posted July 29, 2010 Author Share Posted July 29, 2010 Thank you mucho! That helped clear up the syntax errors. I am now able to load the page the hidden fields remain hidden and the buttons come up. I modified it so that when I submit the form, it loads the page with the added variable in it set to true. It loads the page but does not seem to submit the files, if not the rest of the hidden fields as well..........When i submit it I get my error I have setup if there are missing or wrong filetypes. The cancel button takes you back the index.html form just fine. if (file_exists($upfile_flv)) { echo "<h1>{$exists}</h1>"; echo "<form method=\"post\" action=\"upload.php?var1=true\">"; echo "<input type=\"hidden\" name=\"flv\" value=\"{$_FILES['flv']}\">"; echo "<input type=\"hidden\" name=\"avi\" value=\"{$_FILES['avi']}\">"; echo "<input type=\"hidden\" name=\"course\" value=\"{$course}\">"; echo "<input type=\"hidden\" name=\"finit\" value=\"{$finit}\">"; echo "<input type=\"hidden\" name=\"lname\" value=\"{$lname}\">"; echo "<input type=\"hidden\" name=\"date\" value=\"{$date}\">"; echo "<input type=\"hidden\" name=\"title\" value=\"{$title}\">"; echo "<input type=\"hidden\" name=\"description\" value=\"{$description}\">"; echo "<input type=\"submit\" name=\"overwrite\" value=\"Overwrite\">"; echo "</form>"; echo "<form method=\"post\" action=\"http://mydomain.com/.upload/\">"; echo "<input type=\"submit\" name=\"cancel\" value=\"Cancel\">"; echo "</form>"; The only thing not noticeably working is that the form values do not get resubmitted...... Quote Link to comment Share on other sites More sharing options...
tln6482 Posted August 2, 2010 Author Share Posted August 2, 2010 I got it working. After spending a few hours getting the syntax right, I finally got it to work. //Do we have an overwrite?? if ($overwrite == true){ rename('/usr/local/apache2/htdocs/.upload/store/' . $tmp1, '/usr/local/apache2/htdocs/core/core' . $course . "/" . $flv_name); rename('/usr/local/apache2/htdocs/.upload/store/' . $tmp2, '/usr/local/apache2/htdocs/core/core' . $course . "/" . $avi_name); echo "<h1>Overwrite Complete</h1>"; echo "<h2><a href='http://mediacentral.saintjoe.edu/.upload'/>Click Here</a> to go back to Upload page</h2><br />"; echo "<h2><a href='http://mediacentral.saintjoe.edu/video'/>Click Here</a> to go to watch/download page</h2><br />"; exit; } That is my condition at the beginning of the processing page. $overwrite is a var I pass to the page when resubmitting if it needed to be done. Here is one last look at the resubmit area as well: //Does this file already exist? if (file_exists($upfile_flv)) { echo "<h4>{$exists}</h4>"; rename($_FILES['flv']['tmp_name'], '/usr/local/apache2/htdocs/store/flash.flv'); rename($_FILES['avi']['tmp_name'], '/usr/local/apache2/htdocs/store/movie.avi'); echo "<form method=\"post\" enctype=\"multipart/form-data\" action=\"upload.php?overwrite=true&&tmp1=flash.flv&&tmp2=movie.avi\">"; echo "<input type=\"hidden\" name=\"course\" value=\"{$course}\">"; echo "<input type=\"hidden\" name=\"finit\" value=\"{$finit}\">"; echo "<input type=\"hidden\" name=\"lname\" value=\"{$lname}\">"; echo "<input type=\"hidden\" name=\"date\" value=\"{$date}\">"; echo "<input type=\"hidden\" name=\"title\" value=\"{$title}\">"; echo "<input type=\"hidden\" name=\"description\" value=\"{$description}\">"; echo "<input type=\"submit\" name=\"overwrite\" value=\"Overwrite\">"; echo "</form>"; echo "<form method=\"post\" action=\"http://mediacentral.saintjoe.edu/.upload/\">"; echo "<input type=\"submit\" name=\"cancel\" value=\"Cancel\">"; echo "</form>"; exit; } Thanks again for all the help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.