enable submit button if all fields filled [duplicate] - php

This question already has answers here:
Disabling submit button until all fields have values
(11 answers)
Enable submit button only when all fields are filled
(5 answers)
Disable Submit button until Input fields filled in
(7 answers)
Closed 5 years ago.
I have a form
<form>
Username<br />
<input type="text" id="user_input" name="username" /><br />
Password<br />
<input type="password" id="pass_input" name="password" /><br />
Confirm Password<br />
<input type="password" id="v_pass_input" name="v_password" /><br />
Email<br />
<input type="text" id="email" name="email" /><br /> <br/>
<textarea name="adress" id="adress" ></textarea>
<br>
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female<br>
<input type="submit" id="register" value="Register" disabled="disabled" />
</form>
I want to disable my submit button until all the fields have values.Now everything is okay except radio button.
JS:
<script>
(function() {
$('form > input,textarea,radio').keyup(function() {
var empty = false;
$('form > input,textarea,radio').each(function() {
if ($(this).val() == '') {
empty = true;
}
});
if (empty) {
$('#register').attr('disabled', 'disabled');
} else {
$('#register').removeAttr('disabled');
}
});
})()
</script>
I don't know whether this is good code or not.I am totally new to jquery so please correct me i had make anything wrong.
I want to disable my submit button until all the fields have values including radio button..Please help me.Thanks in advance

You can check if a radio button is checked or not using:
if (!$("input[name='gender']:checked").val()) {
alert('Nothing is checked!');
} else {
alert('One is checked');
}

You need to use prop() and check the type. If the type is radio, you can add validation checks:
required = function(fields) {
var valid = true;
fields.each(function() { // iterate all
var $this = $(this);
if ((($this.is(':text') || $this.is('textarea')) && !$this.val()) || // text and textarea
($this.is(':radio') && !$('input[name=' + $this.attr("name") + ']:checked').length)) { // radio
valid = false;
}
});
return valid;
}
validateRealTime = function() {
var fields = $("form :input");
fields.on('keyup change keypress blur', function() {
if (required(fields)) {
{
$("#register").prop('disabled', false);
}
} else {
{
$("#register").prop('disabled', true);
}
}
});
}
validateRealTime();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
Username<br />
<input type="text" id="user_input" name="username" /><br /> Password
<br />
<input type="password" id="pass_input" name="password" /><br /> Confirm Password<br />
<input type="password" id="v_pass_input" name="v_password" /><br /> Email
<br />
<input type="text" id="email" name="email" /><br /> <br/>
<textarea name="adress" id="adress"></textarea>
<br>
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female<br>
<input type="submit" id="register" value="Register" disabled="disabled" />
</form>

Related

javascript form validation using php

checkEmpty validation is not working. functions are not executing in the PHP file. I tried below code please help me to understand where I am wrong.
My Code in my index.php file.
<div class="main_wrapper">
<form name="myform" method="post" action="test1.php">
<div class="head_wrapper">
Rollno : <input type="text" name="id" onblur="checkEmpty('id')" value="<?php echo $id; ?>"/></br></br>
Name : <input type="text" name="name" onblur="checkEmpty('name')" value="
<?php echo $name; ?>"/></br></br>
Email : <input type="text" name="email" onblur="checkEmpty('email')"
value="<?php echo $email; ?>"/></br></br>
Phone : <input type="number" name="phone" onblur="checkEmpty('phone')"
value="<?php echo $phone; ?>"/></br></br>
Address : <input type="text" name="address" onblur="checkEmpty('address')"
value="<?php echo $address; ?>"/></br></br>
Dept_code: <input type="text" name="dept_code"
onblur="checkEmpty('dept_code')" value="<?php echo $dept_code; ?>"/></br>
</br>
</div>
<div class="foot_wrapper">
<input type="submit" name="insert" value="Addinfo"/>
<input type="submit" name="update" value="Modify"/>
<input type="submit" name="delete" value="Delete"/>
<input type="submit" name="search" value="Find"/>
<input type="submit" name="display" value="Display"/>
</div>
</form>
</div>
</body>
<script src="validation.js">
function checkEmpty(field)
{
var fn = document.getElementById(field).value;
if (fn == "" || fn = null)
{
alert("Fill the box");
}
}
function validEmail(email)
{
var fn = document.getElementById(email).value;
var format = /.+#.+/;
if (!fn.match(format))
{
alert("Enter valid mail");
}
}
</script>
First: you need to put id on your input tags if you gonna use the getElementById
<input type="text" name="id" onblur="checkEmpty('id')" value="<?php echo $id; ?>" id="id" /> //it is better to use id the same as the name
Second: inside the checkEmpty function you need to fix the
if(fn==""||fn=null) //change the fn=null to fn == null
{
alert("Fill the box");
}
third: I wouldn't recommend using the alert("Fill the box") because you are using the onblur method, once you try to skip the field for id/Rollno
1. The focus will be on the next input which is for the 'Name'.
2. The alert will be triggered and will say to 'fill the box' for the 'Rollno'
3. Once the alert shows, it will also trigger the onblur method for the 'Name'
4.Then it will loop showing the alert
I would recommend to put a hidden span that will act as an alert to the user, which will show if the user left the field blank
<input type="text" name="id" onblur="checkEmpty('id')" value="" id="id"/><span id="alertForId" hidden="" class="alerts">Fill the this field first.</span></br></br>
and your checkEmpty function will somehow look like:
function checkEmpty(field)
{
console.log(field);
var fn=document.getElementById(field).value;
if(fn==""||fn==null)
{
if(field == "id")
{
$("#alertForId").show();
}
}else $(".alerts").hide();
}
I put a class 'alerts' to the span to hide them in case the field is not empty, but it will hide all the spans. Just try to configure the code an hope it will help you.
Here is the code configured based from your code:
<div class="main_wrapper">
<form name="myform" method="post" action="index.php">
<div class="head_wrapper">
Rollno : <input type="text" name="id" id="id" onblur="checkEmpty('id')" value=""/><span id="alertForId" class="alerts" hidden="">Fill the this field first.</span></br></br>
Name : <input type="text" name="name" id="name" onblur="checkEmpty('name')" value=""/><span id="alertForName" class="alerts" hidden="">Fill the this field first.</span></br></br>
Email : <input type="text" name="email" id="email" onblur="checkEmpty('email')" value=""/><span id="alertForEmail" class="alerts" hidden="">Fill the this field first.</span></br></br>
Phone : <input type="number" name="phone" id="phone" onblur="checkEmpty('phone')" value=""/><span id="alertForPhone" class="alerts" hidden="">Fill the this field first.</span></br></br>
Address : <input type="text" name="address" id="address" onblur="checkEmpty('address')" value=""/><span id="alertForAddress" class="alerts" hidden="">Fill the this field first.</span></br></br>
Dept_code: <input type="text" name="dept_code" id="dept_code" onblur="checkEmpty('dept_code')" value=""/><span id="alertForIdDeptCode" class="alerts" hidden="">Fill the this field first.</span></br>
</br>
</div>
<div class="foot_wrapper">
<input type="submit" name="insert" value="Addinfo"/>
<input type="submit" name="update" value="Modify"/>
<input type="submit" name="delete" value="Delete"/>
<input type="submit" name="search" value="Find"/>
<input type="submit" name="display" value="Display"/>
</div>
</form>
</div>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
// validation.js is here
//validation
function checkEmpty(field)
{
var fn=document.getElementById(field).value;
if(fn==""||fn==null)
{
if(field == "id")
$("#alertForId").show();
if(field == "name")
$("#alertForName").show();
if(field == "email")
$("#alertForEmail").show();
if(field == "phone")
$("#alertForPhone").show();
if(field == "address")
$("#alertForAddress").show();
if(field == "dept_code")
$("#alertForIdDeptCode").show();
} else{
if(field == "id")
$("#alertForId").hide();
if(field == "name")
$("#alertForName").hide();
if(field == "email")
$("#alertForEmail").hide();
if(field == "phone")
$("#alertForPhone").hide();
if(field == "address")
$("#alertForAddress").hide();
if(field == "dept_code")
$("#alertForIdDeptCode").hide();
}
}
function validEmail(email)
{
var fn=document.getElementById(email).value;
var format=/.+#.+/;
if(!fn.match(format))
{
alert("Enter valid mail");
}
}
</script>
</body>'
changes I made:
I just put your javascript inside the same page
Just link a jquery.js

HTML form submit button check

I have created an HTML form that stores data in a database and later on I take them, and today I wanted to make it more advanced.
I have a form that looks like this (not for login, just for example)
<form method="post" action="process.php" autocomplete="off">
Name: <input type="text" name="name"/>
Surname: <input type="text" name="surname" />
Phone: <input type="text" name="phone" />
<input type="submit" />
In process.php, it stores data in the database so later I can use them.
I want to make it so if one or more of them are empty, when you press submit button it shows error like "Must fill all fields", but if you have done everything, it just submits and stores data in database.
I thought I can make with this kind of code:
<?php
$required = array('name', 'surname', 'phone');
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
Write an error message and don't react to submit
}
else {
If everything is done, allow to submit
}
?>
How can I make it work, so that you can't press Submit while you haven't finished all fields?
You're wanting something client side to validate the fields before you're able to submit to the next page. Look up validation in jQuery and maybe include the "required" attribute that's been added into HTML5.
Add name attribute to button set required to all required fields
<input name="submit" type="submit" />
in php
<?php
if(isset($_POST['submit'])){
$required = array('name', 'surname', 'phone');
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
write and error message and don't react to submit
} else {
if everything is done, allow to submit
}
}
?>
Try the below code
<?php
if(isset($_POST['submit'])){
$required = array('name', 'surname', 'phone');
$error = false;
$message='';
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
$message = 'All fields required';
}else{
//Submit process here
}
}
?>
<?php echo $message; ?>
<form method="post" action="process.php" autocomplete="off">
Name: <input type="text" name="name"/>
Surname: <input type="text" name="surname" />
Phone: <input type="text" name="phone" />
<input type="submit" name="submit" value="Submit" />
Easiest and quickest way, You can use HTML5 required.
<form method="post" action="process.php" autocomplete="off">
Name: <input type="text" name="name" required />
<BR>
Surname: <input type="text" name="surname" required />
<BR>
Phone: <input type="text" name="phone" required />
<BR>
Gender: Male <input type="radio" name="gender" value="1" required />
Female <input type="radio" name="gender" value="2" required />
<BR>
<input type="submit" />
</form>

Getting a value from dynamically created textbox through php

I have n number of text box (n may be any number) with same name. And
I want to access the value of all the text box of that name.
Ex-:
<form method="post" id="create">
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="button" id="newFieldBtn"/>
<input type="submit" name="save" value="save"/>
</form>
jQuery
<script>
jQuery(document).ready(function($) {
$('#newFieldBtn').click(function(){
var code='<input type="text" name="user[]" />';
jQuery('#create').append(code);
</script>
or is there any other way to access the value of the text box. Either
by class or any other property..
<script>
jQuery(document).ready(function($) {
$('#newFieldBtn').click(function(){
var count = document.getElementById('count').value;
count++;
var code = '<input type="text" name="user'+count+'" />';
jQuery('#create').append(code);
document.getElementById('count').value = count;
</script>
and your html like...
<form method="post" id="create">
<input type="hidden" id="count" value="0" name="count">
<input type="button" id="newFieldBtn"/>
<input type="submit" name="save" value="save"/>
</form>
and in your php code...
<?php
if(isset($_POST))
{
$count = $_POST['count'];
for($i=1;$i<=$count;$i++)
{
$user.$i = $_POST['user'.$i];
}
}
?>
Try this one, it will show you the values :
<form action="#" method="post">
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="submit" value="submit" >
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
foreach($_POST['user'] as $key => $value)
{
echo $key." has the value = ". $value."<br>";
}
}
?>
See this in action: http://wistudat.be/try/array.php

how to display contact address as the permanent address by clicking the checkbox

I want to display contact address and permanent address same by clicking the checkbox.
For eg : if i type the contact address and click the checkbox then the same address should be displayed in permanent address field. How to do it using jquery?
Thanks in advance..
I have done it via java script. May be this is helpful for all.
Check box code:
<input type="checkbox" name="present" onclick="permanent(this.form)">
Java Script code:
<script language="JavaScript">
function permanent(p) {
if(p.present.checked == true) {
p.per_add.value = p.temp_address.value;
p.per_city.value = p.temp_city.value;
p.per_state.value = p.temp_state.value;
p.per_country.value = p.temp_country.value;
p.per_pin.value = p.temp_pin.value;
}
}
</script>
Contact Address
<input type="text" id="contact">
<input type="checkbox" id="check">
Contact Address <input type="text" id="permanent">
jquery is
$('#check').click(function() {
var c=$('#contact').val();
$("#permanent").val(c);
});
<script language="JavaScript">
$(document).ready(function(e) {
$('#check').click(function() {
var c=$('#contact').val();
$("#permanent").val(c);
});
});
</script>
</head>
<body>
<form>
<input type="text" id="contact">
<input type="checkbox" id="check">
Contact Address <input type="text" id="permanent">
</form>
function filladd()
{
if(filltoo.checked == true)
{
var tal11 =document.getElementById("tal").value;
var dist11 =document.getElementById("dist").value;
var pin11 =document.getElementById("pin").value;
var copytal =tal11 ;
var copydist =dist11 ;
var copypin =pin11 ;
document.getElementById("tal1").value = copytal;
document.getElementById("dist1").value = copydist;
document.getElementById("pin1").value = copypin;
}
else if(filltoo.checked == false)
{
document.getElementById("tal1").value='';
document.getElementById("dist1").value='';
document.getElementById("pin1").value='';
}
}
<label>Tal</label>
<br/>
<input type="Text" id="tal" name="Taluka1" class="" placeholder="" >
<br/>
<label>Distic</label>
<br/>
<input type="Text" id="dist" name="Distric1" placeholder="">
<br/>
<label>Pincodes</label>
<br/>
<input type="Text" id="pin" name="pincode1" placeholder="">
<br/>
<input type="checkbox" value="" name="filltoo" id="filltoo" onclick="filladd()" />Permanent Address same as Current Address <br/>
<label>Tal</label>
<br/>
<input type="Text" id="tal1" name="Taluka1" placeholder="" >
<br/>
<label>Distic</label>
<br/>
<input type="Text" id="dist1" name="Distric1" placeholder="">
<br/>
<label>Pincodes</label>
<br/>
<input type="Text" id="pin1" name="pincode1" placeholder="">
<br/>

Run function when pressing button and insert into form

So I have this form, where I want to add a button which makes a function run.
This is how my form looks like:
<form action="" method="POST">
CVR-nummer:<br>
<input name="cvr" id="cvr" type="text" value="" size="30"/> <button onclick="<?php getinfo(); ?>">Run the function</button><br>
Navn:<br>
<input name="navn" type="text" value="<?php if(isset($navn)) {echo $navn;} ?>" size="30"/><br>
Adresse:<br>
<input name="adresse" type="text" value="<?php if(isset($adresse)) {echo $adresse;} ?>" size="30"/><br>
Postnummer:<br>
<input name="postnr" type="text" value="<?php if(isset($postnr)) {echo $postnr;} ?>" size="30"/><br>
By:<br>
<input name="by" type="text" value="<?php if(isset($by)) {echo $by;} ?>" size="30"/><br>
<input type="submit" value="Registrer"/>
</form>
And this is my function which uses an API
<?php
function getinfo() {
$cvr = "12345678";
$api = json_decode(file_get_contents("http://cvrapi.dk/{$cvr}/"),true);
$navn = $api['navn'];
$adresse = $api['adresse'];
$postnr = $api['postnr'];
$by = $api['by'];
}
?>
When I click the button I want it to:
1. Get value of the cvr-field.
2. Validate cvr-field so it has exactly 8 numbers.
3. If validation OK it must set the variables.
4. The variables must be shown in the form.
Any thoughts on how to do this?
UPDATE
<script type="text/javascript">
function checkCVR()
{
var cvr = document.getElementById("cvr").value;
var reg = new RegExp("^[0-9]{8}$");
if(!reg.test(cvr))
{
document.getElementById("cvr_error").style.display = 'block';
document.getElementById("cvr_error").innerHTML = "CVR Error!";
}
else
{
document.getElementById("cvr_error").style.display = 'none';
// var url = "http://cvrapi.dk/"+cvr+"/";
// var url = "http://haxify.org/result.php";
var url = "json.txt";
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
var json = JSON.parse(xmlhttp.responseText);
document.getElementById("navn").value = json["navn"];
document.getElementById("adresse").value = json["adresse"];
document.getElementById("postnr").value = json["postnr"];
document.getElementById("by").value = json["by"];
}
};
xmlhttp.send(null);
}
}
</script>
<form action="" method="POST">
CVR-nummer:<br>
<input name="cvr" id="cvr" type="text" value="" size="30"/>
<input type="button" onClick="checkCVR()" value="Run the function" /><br />
<span id="cvr_error" style="color:red;display:none;"></span>
Navn:<br />
<input name="navn" id="navn" type="text" value="" size="30"/><br />
Adresse:<br />
<input name="adresse" id="adresse" type="text" value="" size="30"/><br />
Postnummer:<br />
<input name="postnr" id="postnr" type="text" value="" size="30"/><br />
By:<br />
<input name="by" id="by" type="text" value="" size="30"/><br />
<input type="submit" value="Registrer"/>
</form>
This one actually works on one website but does not work on my own site. Is there any requirements for any software or something, or what can be the reaseon for this to not work on my specific website?
Test:
http://haxify.org/form_v4.php

Categories