How to set textfield value in JSP action - forms

I am designing a JSP page with form action.
<form action="" method="post">
Username: <input type="textfield" name="username" />
Country : <input type="textfield" name="country" />
Url : <input type="textfield" name="Url" />
<input type="submit" value="Submit" />
</form>
I want to set the user given URL value in form action when I click the Submit button and redirect to that URL. How can I achieve this?

This can work pretty well
<form action="" method="post" id="myForm">
Username: <input type="text" name="username" />
Country : <input type="text" name="country" />
Url : <input id="url" type="text" name="Url" />
<button onclick="doStuff();">Submit</button>
</form>
<script>
function doStuff(){
$('#myForm').attr('action', $('#url').val());
$('#myForm').submit();
}
</script>
Don't forget to include the JQuery api's in your code:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

Related

How prevent form submit in google apps script

I am importing a form written in GoogleApps Script into an iframe on a page built with Squarespace but for the life of me cannot prevent the form from submitting. I am using:
window.addEventListener( 'load', preventFormSubmit );
as suggested in GAS documentation but this does not seem to be triggering the preventFormSubmit function. Instead, when the submit button is clicked the form submits and goes to a blank google page. Also the alerts in the preventFormSubmit function (now commented out) never display which suggests that the form is never called.
I have spent days on this, cannot find an answer anywhere and can no longer see the woods for the trees. Any clues as to what I am doing wrong?
Squarespace is a website builder which enables one to embed code, in this case as an iframe.
My code:
js.html:
<script>
function preventFormSubmit() {
//alert( "prevent form submit triggered" );
var forms = document.querySelectorAll('form');
for (var i = 0; i < forms.length; i++) {
forms[i].addEventListener('submit', function(event) {
event.preventDefault();
});
}
//alert( "forms prevented from submitting: " = forms.length );
}
window.addEventListener( "ready", preventFormSubmit );
function handleFormSubmit(formObject) {
google.script.run
.withSuccessHandler( showSuccess )
.withFailureHandler( showError )
.processForm_1( formObject );
}
</script>
html:
<!DOCTYPE html >
<head>
<base target="_top" >
<?!= include('css'); ?>
<?!= include('js'); ?>
</head>
<body>
<div id="formDiv" class="card" >
<h2 id="title" >Alternative Booking form</h2>
<form id="alternative-booking-form-1" onsubmit="handleFormSubmit(this)" >
<fieldset>
<legend>About You</legend>
<p>Please tell us a bit about yourself.
</p>
<input type="text" id="firstName" name="firstName" form="alternative-booking-form-1"
placeholder="your first name" value="" required
/><br />
<input type="text" id="lastName" name="lastName" form="alternative-booking-form-1"
placeholder="your last name" value="" required
/><br />
<input type="text" id="title" name="title" form="alternative-booking-form-1"
placeholder="your title, eg: mr, mrs, ms etc" value="" /><br>
</fieldset>
<fieldset>
<legend>Your Contact Details</legend>
<p>We will only use your contact details in case we need to contact you with regard to
this booking, unless you consent
to further communications, as offered later in this booking process.</p>
<input type="email" id="email" name="email" form="alternative-booking-form-1"
placeholder="your email address" value=""
required /><br />
<input type="tel" id="phone" name="phone" form="alternative-booking-form-1"
placeholder="phone" value="" required /><br />
</fieldset>
<fieldset>
<input type="hidden" id="form" name="form" form="alternative-booking-form-1" value="1" />
<br />
<input type="submit" id="submit" name="submit" form="alternative-booking-form-1"
class="red" value="Next →" />
<br />
<br />
</fieldset>
</form>
</div>
<div id="output" name="output" ></div>
</body>
<!DOCTYPE html >
<head>
<base target="_top" >
<?!= include('css'); ?>
<?!= include('js'); ?>
</head>
<body>
<div id="formDiv" class="card" >
<h2 id="title" >Alternative Booking form</h2>
<form id="alternative-booking-form-1" >
<fieldset>
<legend>About You</legend>
<p>Please tell us a bit about yourself.
</p>
<input type="text" id="firstName" name="firstName" form="alternative-booking-form-1"
placeholder="your first name" value="" required
/><br />
<input type="text" id="lastName" name="lastName" form="alternative-booking-form-1"
placeholder="your last name" value="" required
/><br />
<input type="text" id="title" name="title" form="alternative-booking-form-1"
placeholder="your title, eg: mr, mrs, ms etc" value="" /><br>
</fieldset>
<fieldset>
<legend>Your Contact Details</legend>
<p>We will only use your contact details in case we need to contact you with regard to
this booking, unless you consent
to further communications, as offered later in this booking process.</p>
<input type="email" id="email" name="email" form="alternative-booking-form-1"
placeholder="your email address" value=""
required /><br />
<input type="tel" id="phone" name="phone" form="alternative-booking-form-1"
placeholder="phone" value="" required /><br />
</fieldset>
<fieldset>
<input type="hidden" id="form" name="form" form="alternative-booking-form-1" value="1" />
<br />
<input type="button" id="submit" name="submit" form="alternative-booking-form-1"
class="red" value="Next →" />
<br />
<br />
</fieldset>
</form>
</div>
<div id="output" name="output" ></div>
<script>
window.onload = function() {
document.getElementById("alternative-booking-form-1").addEventListener( "ready", handleFormSubmit );
}
function handleFormSubmit(formObject) {
google.script.run
.withSuccessHandler( showSuccess )
.withFailureHandler( showError )
.processForm_1( formObject );
}
</script>
</body>

send parameters of form via URL

I have a form like this:
<form rel="search" action="/archives">
<input name="keyword" type="text" value="search..." />
<input type="submit" value=submit" name="submit" />
</form>
When a User click on submit...I want sent parameters by URL...
exact this URL: http://examole.com/archives/keyword
Or any alternative way...
.
Is it possible?
Edit
I try change above code to this:
<form rel="search" action="/archives" method="get">
<input name="keyword" type="text" value="search..." />
<input type="submit" value="submit" />
</form>
The result:
http://127.0.0.1/archives?keyword=search...
How can I change The URL to this:
http://127.0.0.1/archives/search...
Yes, you can. Just use method="get"
<form method="get" rel="search" action="/archives">
<input name="keyword" type="text" value="search..." />
<input type="submit" value=submit" name="submit" />
</form>

Neither ng-submit or ng-click responding to submit button in a form

I'm trying to make a very simple login form in angularjs, but it seems that neither the ng-submit() nor the ng-click directive seems to be working.
I've created a very basic plunker example here: http://plnkr.co/edit/BrLIxSZggZofCBoZpjT4?p=preview
In which either/both the ng-click or the ng-submit should open a simple alert window with an 'a' letter. However nothing happens when I click on the sign in button. What's more interesting, that if I change ng-app to ng-app="test" then the form get submitted but the alert doesn't get called either.
What am I doing wrong?
The basic example:
<head>
<script data-require="angular.js#1.2.9" data-semver="1.2.9" src="http://code.angularjs.org/1.2.9/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-app>
<!-- Login -->
<form data-title="Sign in" data-value="login" class="tab-pane" name="login"
ng-submit="alert('a')" ng-controller="Ctrl">
<input type="text" name="username" value="" placeholder="Username" ng-model="model.username" ng-minlength="3" required autocapitalize="false" />
<input type="password" name="password" value="" placeholder="Password" ng-model="model.password" required autocapitalize="false" autocorrect="false" />
<input type="submit" ng-click="alert('a')" name="login" value="Sign in" />
</form>
<!-- / Login -->
</body>
</html>
ng-submit directive invokes a function which should be in the controller.
The ng-submit directive will be triggered provided the form fields are valid
Form/HTML
<form data-title="Sign in" data-value="login" class="tab-pane" name="login"
ng-submit="submit()" ng-controller="Ctrl">
<input type="text" name="username" value="" placeholder="Username" ng-model="model.username" ng-minlength="3" required autocapitalize="false" />
<input type="password" name="password" value="" placeholder="Password" ng-model="model.password" required autocapitalize="false" autocorrect="false" />
<input type="submit" name="login" value="Sign in" />
</form>
Controller
function Ctrl($scope) {
$scope.model = {};
$scope.submit = function(){
alert('a');
}
}
Plunkr

On CodeIgniter, how to repopulate a form only with correct values?

Imagine a form like this:
<form action="test.php" method="post">
<input type="text" name="firstname" value="<?=set_value('firstname')?>" />
<input type="text" name="lastname" value="<?=set_value('lastname')?>" />
<input type="text" name="email" value="<?=set_value('email')?>" />
<input type="submit" name="submit_form" value="OK" />
</form>
If I submit an incorrect email, the CodeIgniter function will write "the field is not valid" and populate the invalid field with the wrong value.
I would like to keep the error message but not the wrong value (I prefer having an empty value). But I also want to keep the re-populating function for correct values.
Here is what I get:
Here is what I want:
[EDIT] SOLUTION FOUND (thanks to Herr Kaleun and Matt Moore)
Example with the email field:
<input type="text" name="email" id="email" value="<?=!form_error('email')?set_value('email'):''?>" />
You can check the fields individually and have a logic like this.
If the error for a specific field is present, you can build a logic on top of that.
if ( form_error('email') != '' )
{
$data['email_value'] = '';
}
else
{
$data['email_value'] = set_value('email');
}
<form action="test.php" method="post">
<input type="text" name="firstname" value="<?=set_value('firstname')?>" />
<input type="text" name="lastname" value="<?=set_value('lastname')?>" />
<input type="text" name="email" value="<?= $email_value; ?>" />
<input type="submit" name="submit_form" value="OK" />
</form>
The Form_error is set when a form value errors out based on the validation. Judging by your use of the set_value function I assume you're using the built in form validation. You can check to see if the message for that field is set if(form_error('fieldname') !=NULL) and set the value based on that.
You may have to setup error messages for each field, which you should be doing anyway. Here is the guide that covers it: http://codeigniter.com/user_guide/libraries/form_validation.html#repopulatingform
<form action="test.php" method="post">
<input type="text" name="firstname" value="<?if(form_error('firstname') != NULL){echo set_value('firstname');}?>" />
<input type="text" name="lastname" value="<?if(form_error('lastname') != NULL){ echo set_value('lastname');}?>" />
<input type="text" name="email" value="<?if(form_error('email') !=NULL){ echo set_value('email');}?>" />
<input type="submit" name="submit_form" value="OK" />
</form>
*NOTE THIS CODE HAS NOT BEEN TESTED *

Select input value from two different forms in one page using mootools

I have two forms with different values in one page.
I want get input value of the form that user clicks on it's submit button, using mootools code.
Mootools code most work for two forms, as same:
<form class="cart_form" >
<input type="hidden" name="order_code" value="KWL-hgy" />
<input type="hidden" name="price" value="40000" />
<input type="hidden" name="name" value="modeling" />
<label>KWL-hgy: <input class="center" type="text" name="qty" value="2" size="3" ></label>
<input type="submit" name="submit" value="Add to cart" />
</form>
<form class="cart_form" >
<input type="hidden" name="order_code" value="KWL-JFE" />
<input type="hidden" name="price" value="12000" />
<input type="hidden" name="name" value="php" />
<label>KWL-JFE: <input class="center" type="text" name="qty" value="1" size="3" ></label>
<input type="submit" name="submit" value="Add to cart" />
</form>
you could do it somehow like this:
$$('form.cart_form').addEvent('submit', function(e){
this.getChildren().each(function(el){
alert(el.get('name') + ': ' + el.get('value'));
});
});
this will fire an event as soon as a form with the class chart_form is submited and loop through all the form's children.