Forum Moderators: open
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
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.
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.
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.
<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
- 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+'&more=1234';
}
else { final_url = 'site.com/cat.php?url='+ url +'&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>
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.