Disable zend form element Enter keypress event - php

Does anyone know how to disable the Enter keypress event of a zend element text input control? When a user presses Enter on a form element it tries to post, which I want to disable.

This is something you'll need to do client side (i.e. with Javascript). With Dojo it would be something like:
dojo.connect(dojo.byId('FIELDID'), 'onkeydown', function(event){
if (event.keyCode == dojo.keys.ENTER) {
dojo.stopEvent(event);
}
});
replace 'FIELDID' with the ID of the text field you want to hook this into. You could combine this with a dojo.query call if you want to apply it to everything in the form.
jQuery and the other JS frameworks will have an equivalent.

I think this Should solve your problem
$textField = new Zend_Form_Element_Text("text");
$textField->setAttrib('onkeypress', 'nullifyEnterKey();');
function nullifyEnterKey(e)
{
var key;
if(window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
return (key != 13);
}

Related

prevent characters in text box in php using jquery

I want to prevent user to input few characters like ; and \ and /. I now it need jquery to do, but i cant find one
The jquery will used in name. zip code input to prevent such characters etc.
please show me an example as i new to jquery and php
You can use event.which to getting code of pressed key and use return false to preventing enter of character.
$("input").keydown(function(e) {
if (e.which === 186 || e.which === 191 || e.which === 220){
console.log("This character can't entered");
return false;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input />
This should do the job. I hope you can read this simple code:
// select css classes 'input' and assign onKeyDown event to them
$(".input").keydown(function(event) {
// check what key is pressed. dot is not allowed in this case
if ( event.keyCode === 190) {
// prevent event to happen
event.preventDefault();
}
});
On the bottom of this page there is a small table for some codes and also an input field which shows you key code for particular key. Just as a helper, to let you find codes for wished key faster.

how to use the enter button(key board) in php

For Example :
in a webpage if the user press Enter key from the keyboard a certain web button will be pressed.
Suppose there are 3 web button in a web layout namely Next,Previous,reset.
I want if the user press enter key the Next button action will be done.
if press the backspace then previous web button action will be done.
Please I want to implement this in PHP.
Full code and description.
This is no posible with PHP. PHP is server side langue, this means the code only runs on the server not the client side. To do this you will something like javascript and jQuery.
Example in jQuery:
$(document).keydown(function(e) {
if(e.which == 13) {
// enter pressed
}
});
Here's a list of key values
$(document).keydown(function(e) {
var keyPressed = e.which || evt.keyCode;
if (keyPressed==13)
{
console.log(keyPressed)
alert("your next acction")
}
if(keyPressed==8)
{
console.log(keyPressed)
alert("Your previous action");
}
})
This can be done by checking the keypress event in javascript.
$(document).on('keypress',fieldId, function(evt){
var keyPressed = evt.which || evt.keyCode;
if (keyPressed == ascii value of the keyboard button) {
// do something
}
});
The enter button value is 13.
Newer browsers support an autofocus attribute.
<form action=http://google.com>
<input value=yes type=submit autofocus>
</form>
This is just HTML, no PHP required, but you could echo this out.

How to assign value to .click event

I have many input field that post data when enter key is press i have done one thing that i find id to of selected button and want to pas with click event bt it gives error here is code
$(".text").bind("keydown", function(event) {
// track enter key
var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
if (keycode == 13) {
var id=$(this).parent().find("input[type=submit]").attr('id');
(id).click(); // this one give error
return false;
} else {
return true;
}
});
(id).click(); this line gives error that id.click is not a function.what i m missing.
I think you are looking for:
$(this).closest("form").find("input[type=submit]").trigger("click");
There is no need to try to find the id and create a different JQuery object. You will have a reference to what you are looking for with find("input[type=submit]").
In order to know for sure, we would need to see your markup in order to find the most effective way to find what element you are looking for,
id is a string.
Strings don't have a click method.
Instead, you should submit the form directly:
$(this).closest('form').submit();
You need to use the following:
$("#"+id).trigger("click");

If form option value select equals then redirect PHP/jQuery

I have a form on my page with a dropdown of counties. I want to somehow, with jQuery or PHP say that if a certain county is selected then redirect otherwise process the form.
I've created a fiddle to try and explain...
http://jsfiddle.net/KVjAc/
Just submit the form and use a php header command for the values you want to redirect. That would be the first thing to check in the form processor script.
No need for javascript unless you are using ajax to refresh parts of the page based on the selection.
I hope this helps(if you need a javascript/jquery solution),
$('#selectbox_ID').change( function() {
if($(this).val() == "your country"){
}
});
Do something like:
$(document).ready(function () {
$('#county').on('change', function () {
var value = $(this).val();
if (value == 'cheshire' || value == 'city of london' || value == 'durham') {
window.location.href = 'http://www.google.com/';
}
})
});
EDIT: Also, you need to add values to your option elements: <option value="cheshire">Cheshire</option>

Check in ajax/jquery a form beform submit ( check the value whether it is in database)

ajax is not yet sothin i master.
I have two forms field
code :
name :
and the submit button like :
<form><input type=text name=code><input type =text name=name/></form>
I would like in php/jquery to check if the code the user fill exist in a table of my db.
If it does not exits, when the user leave the textfield to fill the next one, i would like to print a message like: this code is not in the db and then clean the fied. Until the user provide a valide code.
If your php service returns true or false for validation.
and the placeholder for the error is a label called
then an example (in jQuery) would be
$(document).ready(function() {
$("form").submit(function(e) {
var code = $("input[name='code']");
var error = $("#error");
e.preventDefault();
var form = this;
$.getJSON('urlToPhp',
{ code: code.val() },
function(valid) {
if (!valid) {
error.text(code.val() + ' is not found try another code...');
code.val('');
} else {
form.submit();
}
}
);
});
});
I've created a simple example at http://jsfiddle.net/nickywaites/e4rhf/ that will show you have to create a jQuery ajax post request.
I'm not too familiar with php so that part of it I'll have to leave aside although you can use something along the lines of $_POST["Name"].
Here is php example that I googled http://php4every1.com/tutorials/jquery-ajax-tutorial/ that might be better for you.

Categories