Forum Moderators: open

Message Too Old, No Replies

Submit URL by taking values from a Textbox

         

blackcode

9:42 pm on Dec 6, 2009 (gmt 0)

10+ Year Member



Greetings everyone!

I have 1 textbox and I would like to have 2 buttons (ButtonA , ButtonB).
and when the

Textbox contains a value and hits on [ButtonA] i would like to pass a URL redirecting

site.com/script.php?='value in the text'more?parameters
where site.com and end part would be provided by me and static

If clicked on the second button it also takes values from the same textbox and go to a URL of site2.com?script.php?='value in textbox'?more=parameters

It would be great help for me if you could provide a simple script to do this in a safe way. I would like it to be independent to all browsers, because and i am guessing if java is used, users without java may not be able to do it.

Thank you in advance!
John. S

rocknbil

5:27 pm on Dec 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard blackcode, there are some issues with your plan.

If someone types in the text field and presses enter instead of clicking one of the buttons, what then? No button was clicked, so it's impossible to determine server side what happened.

You'll need some server side logic to manage this part. That is, if the text field is blank AND no button was clicked you need an error handler to inform the user what they need to do. If the text field is populated and no button was clicked, you can assume they meant "custom url" and assemble the URL server side. If the "custom url" buton is clicked but there's no text value, again, error handler.

If they use the form as expected, pretty straightforward. I'd probably just post the info to a handler script, assemble the appropriate URL, then do a location header to the desired URL from there.

Once that's working, you can go back and add Javascript to support a direct post the the location you want. This way it will work with and without JS.

Demaestro

6:34 pm on Dec 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



"""If someone types in the text field and presses enter instead of clicking one of the buttons, what then?"""

Since he is talking about a textarea he shouldn't have to worry about this as clicking enter in a textarea gives a new line in the area rather than submitted the form.

It might be easiest though to have them both buttons submit to the same URL and then that page does a check to see what button was pushed and redirects to the correct page and builds a query string to pass values to the right page.

blackcode

9:00 pm on Dec 7, 2009 (gmt 0)

10+ Year Member



Thank you for ur help guys!

I would like always hitting ENTER is equal to [Button-A] - the other button needs to be clicked using mouse.

What could I do? I am not a coder. it would be a great help if i get a code for this function. i would really appreciate.
thank you

Regards
John

rocknbil

8:08 pm on Dec 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since he is talking about a textarea...

Was wondering who'd dunk me on that. :-P Even though it says "textarea" if you're creating a URL, you wouldn't use textarea, you would use input type="text" to avoid the issues that will arise with newlines in the submitted data, so I went on that assumption. Extend the argument, then, to accessibility, in which case some users may not even be able to "click" but they can still submit (yes I know this is a minority, yes it is a thin argument.)

What could I do? I am not a coder.

I think you'll need to hire one, this is a relatively easy thing to do as described, insuring it works with and without JS, but it's not something that can easily be addressed in a code-drop. It has to integrate into whatever else you are doing.

blackcode

7:11 am on Dec 9, 2009 (gmt 0)

10+ Year Member



I am sorry, Textarea was a mistake - i actually reffered to "TextBox"
and I am using currently this snippet of Java script:

<script>
function convertURL() {
var url = document.getElementsByName( "url")[0];
var fullurl = document.getElementsByName( "fullurl")[0];
fullurl.value = "site.com/cat.php?="+ url.value +"more.php?=options";
}
</script>

and this form

form name= "Search" onsubmit='convertURL();location.href=fullurl.value;return false'>
<p>
<input name='url' type='text' size="60" />
<input name='fullurl' type='hidden' />

but it has 1 button. Thats my problem. I would like to have:
1 - TextBox
Button-A and Button-B which both if clicked takes values from TextBox but Button-A takes to URL-A and Button-B takes to URL-B
If hit ENTER - its always Button-A

Thank you for ur suggestion
Regards
John

rocknbil

8:37 pm on Dec 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, if you

- refuse to take the advice to hire someone who knows what they are doing,
- don't care that this is Javascript dependent,

This appears to work.


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<script type="text/javascript">
var whichClick;
window.onload=function() { attachBehaviors(); }
function attachBehaviors() {
var arr = new Array ('submitOne','submitTwo');
for (i=0;i<arr.length;i++) {
var but_id=arr[i];
if (document.getElementById(but_id)) {
document.getElementById(but_id).onclick=function() { whichClick=this.id; }
}
}
}
function convertURL(form) {
if (document.getElementById('url')) {
var url = document.getElementById('url').value;
if (url=='') { alert('Enter a url.'); return false; }
var final_url;
if (whichClick=='submitTwo') {
final_url = 'site2.com?script.php?url='+url+'&amp;more=1234';
}
else { final_url = 'site.com/cat.php?url='+ url +'&amp;more=1234'; }
// Uncomment the window.location, comment out the alert
//window.location=final_url;
alert(final_url);
}
else { alert('no url field'); }
return false;
}
</script>
</head>
<body>
<!-- do not name objects reserved words: search, submit, reset, etc. -->
<form name="SearchForm" action="" onsubmit="return convertURL(this);">
<input name="url" id="url" type="text" size="60">
<input name="submitOne" id="submitOne" type="submit" value="Custom URL (a)">
<input name="submitTwo" id="submitTwo" type="submit" value="Other URL (b)">
</form>
</body>
</html>

blackcode

10:49 pm on Dec 9, 2009 (gmt 0)

10+ Year Member



Worked like charm! Perfect! Thank you very much for your time and support! I really appreciate it!

blackcode

7:30 am on Dec 10, 2009 (gmt 0)

10+ Year Member



Hi, just out of curiosity, i wud like to ask you, would brower-dependent issue be solved if this function is done by perl/cgi script? :)

mack

9:52 am on Dec 10, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



It would solve most issues, because your form could be simple html, and all validation could be done server side. if validation fails "please go back and fix errors"

The problem with client side scripting is if the user has it disabled, the page simply won't work.

Mack.

rocknbil

7:14 pm on Dec 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



would brower-dependent issue be solved if this function is done by perl/cgi script?

(As mack said) correct, see post #2 and Demaestro's post #3, but perl/cgi, PHP, or ASP would all work to do this so it is not Javascript dependent.

One could possibly even do some wizardry with mod_rewrite but simplest would be to do "exactly what that Javascript is doing" but server-side.

blackcode

10:10 am on Dec 17, 2009 (gmt 0)

10+ Year Member



Thank you all for great help!

rocknbil Thank you very much for the script provided. I have a little question about it. How could i define which URL to be taken when a user hits [ENTER] key?

Regards
John

blackcode

9:56 pm on Dec 17, 2009 (gmt 0)

10+ Year Member



Figured out! Switched the links! :) Either way, thank you so much for the helps! :)