Connecting Tech Pros Worldwide Forums | Help | Site Map

While using window.location the value entered in text box is not retained

Member
 
Join Date: Oct 2008
Posts: 82
#1: Jan 16 '09
Hi,

Here am trying to populate the page with new url, with the value that is being entered in the text box.
this is the php code called example.php
Expand|Select|Wrap|Line Numbers
  1. <?
  2. echo "<input type='text' name='text1' id='text1' />\n";
  3. echo "<input type='button' name='button' onclick=\"return reload_page_with_value()\" />\n";
  4. ?>
  5.  
the javascript code:
Expand|Select|Wrap|Line Numbers
  1. function reload_page_with_value() {
  2.         var value=document.GetElementById('text1').value;
  3.         var url="example.php";
  4.         url=url+"?value=+"value;
  5.        window.location=url;
  6. }
  7.  
The problem here is, after the page is being reloaded with the value, the text entered in the textbox will not be available. Can somebody tell me the reason why this is happening and give the solution??

thanks

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,658
#2: Jan 16 '09

re: While using window.location the value entered in text box is not retained


Quote:

Originally Posted by sarega View Post

Can somebody tell me the reason why this is happening and give the solution??

getElementById() is written with a small "g", but why so complicated? just echo out the value in your server script using the default value attribute for <input> (<input ... value="default"/>). when you have a form, you don't need to reload the page with location.href, that's what the submit button is for (except for some special cases maybe).
Member
 
Join Date: Oct 2008
Posts: 82
#3: Jan 16 '09

re: While using window.location the value entered in text box is not retained


I want that value to be used for further processing. So how do i retrieve the value of value??
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,658
#4: Jan 16 '09

re: While using window.location the value entered in text box is not retained


Javascript processing or PHP processing?
Member
 
Join Date: Oct 2008
Posts: 82
#5: Jan 16 '09

re: While using window.location the value entered in text box is not retained


I have to do php processing
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,658
#6: Jan 16 '09

re: While using window.location the value entered in text box is not retained


submit the form. depending upon which submit button you use (you can have more than one submit type button in a form), the script can do different tasks with it.
Member
 
Join Date: Oct 2008
Posts: 82
#7: Jan 16 '09

re: While using window.location the value entered in text box is not retained


Actually the form is not complete with this textbox, there are more checkboxes in the form. After that i have a submit button
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,658
#8: Jan 16 '09

re: While using window.location the value entered in text box is not retained


that doesn't matter, if you name this submit button (not the final one), say, "refresh" you can test for this value in the script and print the page with all the currently submitted values (and maybe some according processing))
like:
Expand|Select|Wrap|Line Numbers
  1. if (isset($_POST['refresh']))
  2. {
  3.     // do some refreshing
  4.     // compute some other stuff
  5. }
Member
 
Join Date: Oct 2008
Posts: 82
#9: Jan 19 '09

re: While using window.location the value entered in text box is not retained


I did not understand how to proceed?
Member
 
Join Date: Oct 2008
Posts: 82
#10: Jan 19 '09

re: While using window.location the value entered in text box is not retained


Cant we use textbox and checkboxes for window.location property??
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,658
#11: Jan 19 '09

re: While using window.location the value entered in text box is not retained


Quote:

Originally Posted by Mozilla Developer Center

Returns a Location object, which contains information about the URL of the document and provides methods for changing that URL. You can also assign to this property to load another URL.

window.location seems not the right place to me to handle form elements..... unless I've totally misunderstood you
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,658
#12: Jan 19 '09

re: While using window.location the value entered in text box is not retained


Quote:

Originally Posted by sarega View Post

I did not understand how to proceed?

you can have multiple submit buttons in a form, so you can assign in your script a different function/processing to each of the buttons. the buttons can be distinguished by their name attributes. so if you put one submit button named "process" (just for example) and another one "refresh", then you can write (there's always only one submit button's value sent):
Expand|Select|Wrap|Line Numbers
  1. // HTML
  2. <form action="...php" method="post" ...>
  3. <!-- your form elements go here -->
  4.      <input type="submit" name="refresh" value="preview"/>
  5.      <input type="submit" name="process" value="send"/>
  6. </form>
  7.  
  8. // PHP
  9. if (isset($_POST['refresh']))
  10. {
  11.     // preview and its processing code comes here
  12. }
  13. if (isset($_POST['process']))
  14. {
  15.      // final processing code comes here
  16. }
form elements which were not filled are either not present in $_POST or have an empty value (which you can check for with empty())
Member
 
Join Date: Oct 2008
Posts: 82
#13: Jan 19 '09

re: While using window.location the value entered in text box is not retained


There exists already another form which is pointing to another php file, in this case can i still use another form which points to a diff php file?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,658
#14: Jan 19 '09

re: While using window.location the value entered in text box is not retained


maybe a demonstrative html sample can clarify the matter for me.... I'm a bit lost now.
Reply