By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,029 Members | 1,349 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,029 IT Pros & Developers. It's quick & easy.

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

P: 82
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
Jan 16 '09 #1
Share this Question
Share on Google+
13 Replies


Dormilich
Expert Mod 5K+
P: 8,639
@sarega
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).
Jan 16 '09 #2

P: 82
I want that value to be used for further processing. So how do i retrieve the value of value??
Jan 16 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
Javascript processing or PHP processing?
Jan 16 '09 #4

P: 82
I have to do php processing
Jan 16 '09 #5

Dormilich
Expert Mod 5K+
P: 8,639
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.
Jan 16 '09 #6

P: 82
Actually the form is not complete with this textbox, there are more checkboxes in the form. After that i have a submit button
Jan 16 '09 #7

Dormilich
Expert Mod 5K+
P: 8,639
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. }
Jan 16 '09 #8

P: 82
I did not understand how to proceed?
Jan 19 '09 #9

P: 82
Cant we use textbox and checkboxes for window.location property??
Jan 19 '09 #10

Dormilich
Expert Mod 5K+
P: 8,639
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
Jan 19 '09 #11

Dormilich
Expert Mod 5K+
P: 8,639
@sarega
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())
Jan 19 '09 #12

P: 82
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?
Jan 19 '09 #13

Dormilich
Expert Mod 5K+
P: 8,639
maybe a demonstrative html sample can clarify the matter for me.... I'm a bit lost now.
Jan 19 '09 #14

Post your reply

Sign in to post your reply or Sign up for a free account.