javascript form validation using php - 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

Related

On Checkbox Changed Copy Values to textbox jquery

On Checkbox Changed event I am copying hidden values to my textbox,
now when I try to add value using jquery its gives me [Object object].
HTML Code
My hidden variables values
<div class="reserve-form">
<span>Name: </span><span><?php echo $user_data[0]->first_name.' '.$user_data[0]->last_name; ?>
<input type="hidden" id="name_account" value="<?php echo $user_data[0]->first_name.' '.$user_data[0]->last_name; ?>" />
</span>
<span>Email Address: </span><span><?php echo $user_data[0]->email_id; ?>
<input type="hidden" id="email_account" value="<?php echo $user_data[0]->email_id; ?>" />
</span>
<span>Phone Number: </span><span><?php echo $user_data[0]->phone_no; ?>
<input type="hidden" id="phoneno_account" value="<?php echo $user_data[0]->phone_no; ?>">
</span>
<div class="passenger-checkbox">
<input type="checkbox" name="duplicate_info" id="duplicate_info" class="css-checkbox" />
<label for="duplicate_info" class="css-label">Is passenger info same as account holder</label>
</div>
</div>
**Where I need to append it**
<div id="passanger_info">
<input type="text" class="form-control" id="name[]" name="name[]" placeholder="Name">
<input type="text" class="form-control" id="email_address[]" name="email_address[]" placeholder="Email Address">
<input type="text" class="form-control" id="phone_number[]" name="phone_number[]" placeholder="Phone Number">
<button type="button" class="removebtn"><i class="fa fa-times"></i></button>
</div>
$("#duplicate_info").change(function() {
if(this.checked)
{
isChecked();
}
else
{
alert("Unchecked")
}
});
function isChecked()
{
//hidden variables values
var name_account = $('#name_account').val();
var email_account = $('#email_account').val();
var phoneno_account = $('#phoneno_account').val();
alert(name_account);
var name = $('#name').val(name_account);
alert(name);
var email_address = $('#email_address').val(email_account);
var phone_number = $('#phone_number').val(phoneno_account);
}
It's like when I copy name_account to name and try to alert the name it gives me Object Object instead of values. What I am missing?
You have to set the value, before you take from it.
setting value = $('#name').val(name_account)
taking the value = var name = $('#name').val();
The problem in your code is [] in your id
$("#duplicate_info").change(function() {
if (this.checked) {
isChecked();
} else {
alert("Unchecked")
}
});
function isChecked() {
//hidden variables values
var name_account = $('#name_account').val();
console.log("input val of #name_account is : " + name_account);
$('#name').val(name_account)
var name = $('#name').val();
console.log("input val of #name is : " + name);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="reserve-form">
<span>Name: </span><span>Carsten
<input type="hidden" id="name_account" value="Carsten" />
</span>
<span>Email Address: </span><span><?php echo $user_data[0]->email_id; ?>
<input type="hidden" id="email_account" value="<?php echo $user_data[0]->email_id; ?>" />
</span>
<span>Phone Number: </span><span><?php echo $user_data[0]->phone_no; ?>
<input type="hidden" id="phoneno_account" value="<?php echo $user_data[0]->phone_no; ?>">
</span>
<div class="passenger-checkbox">
<input type="checkbox" name="duplicate_info" id="duplicate_info" class="css-checkbox" />
<label for="duplicate_info" class="css-label">Is passenger info same as account holder</label>
</div>
</div>
**Where I need to append it**
<div id="passanger_info">
<input type="text" class="form-control" id="name" name="name[]" placeholder="Name">
<input type="text" class="form-control" id="email_address[]" name="email_address[]" placeholder="Email Address">
<input type="text" class="form-control" id="phone_number[]" name="phone_number[]" placeholder="Phone Number">
<button type="button" class="removebtn"><i class="fa fa-times"></i></button>
</div>
Check below example:
Fill up the details and click on checkbox.
$("#duplicate_info").change(function() {
if(this.checked)
{
isChecked();
}
else
{
alert("Unchecked")
}
});
function isChecked()
{
//hidden variables values
var name_account = $('#name_account').val();
var email_account = $('#email_account').val();
var phoneno_account = $('#phoneno_account').val();
alert(name_account);
$('#name').val(name_account);
var name = $('#name').val();
alert(name);
var email_address = $('#email_address').val(email_account);
var phone_number = $('#phone_number').val(phoneno_account);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
My hidden variables values
<div class="reserve-form">
<span>Name: </span><span>
<input type="hidden" id="name" value="" />
</span>
<span>Email Address: </span><span>
<input type="hidden" id="email_account" value="" />
</span>
<span>Phone Number: </span><span>
<input type="hidden" id="phoneno_account" value="">
</span>
<div class="passenger-checkbox">
<input type="checkbox" name="duplicate_info" id="duplicate_info" class="css-checkbox" />
<label for="duplicate_info" class="css-label">Is passenger info same as account holder</label>
</div>
</div>
**Where I need to append it**
<div id="passanger_info">
<input type="text" class="form-control" id="name_account" name="name_account[]" placeholder="Name">
<input type="text" class="form-control" id="email_address" name="email_address[]" placeholder="Email Address">
<input type="text" class="form-control" id="phone_number" name="phone_number[]" placeholder="Phone Number">
<button type="button" class="removebtn"><i class="fa fa-times"></i></button>
</div>

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

Textarea-N with recount and disable button function

JS
$(document).ready(function(){
$('.comment').bind("blur focus keydown keypress keyup", function(){recountss();});
$('input.comment_button').attr('disabled','disabled');
$('#form').submit(function(e){
tweet();
});
});
function recountss()
{
var maxlen=280;
var current = maxlen-$('.comment').val().length;
$('.counters').html(current);
if(current<0 || current==maxlen)
{
$('.counters').css('color','#D40D12');
$('input.comment_button').attr('disabled','disabled').addClass('inact');
}
else if (!$.trim($(".comment").val()))
{
$('input.comment_button').attr('disabled','disabled').addClass('inact');
}
else
$('input.comment_button').removeAttr('disabled').removeClass('inact');
if(current<10)
{
$('.counters').css('color','#D40D12');
}
else if(current<20)
{
$('.counters').css('color','#5C0002');
}
else
{
$('.counters').css('color','#C0C0C0');
}
HTML
<form method="post" action="" id="form">
<textarea name="comment" class="comment" id="ctextarea<?php echo $msg_id;?>" ></textarea>
<input type="hidden" name="uid" id="uid" value="<?php echo $uid; ?>"/>
<div class="p"></div>
<input type="submit" value="Comment" id="<?php echo $msg_id;?>" class="comment_button"/>
<span class="counters">
280
</span>
</form>
I using jquery.
I have more than 1 textarea, if I type in textarea-1, it work, button disabled if no any text and recount working good.
But if I type in textarea-2, textarea-3, textarea-N, it can't work.
So how can I do to make all works ?
Thank you.
If each <textarea> has its own maxlen=280, it would be better to change from using class - $('.comment') to id - $('#ctextarea-'+i) on all elements - <textarea>, <button>, <span>, etc.
This is untested, but should help point you in the right direction.
$(document).ready(function(){
var i=1;
var numtextarea = 1;
$('#form').find('textarea').each(function(){numtextarea++;} //find number of <textarea>'s
while(i<numtextarea){ // loop through each <textarea>
$('#ctextarea-'+i).bind("blur focus keydown keypress keyup", function(){recountss(i);});
$('#comment_button-'i).attr('disabled','disabled');
}
});
$('#form').submit(function(e){
tweet();
});
});
function recountss(i)
{
var maxlen=280;
var current = maxlen-$('#ctextarea-'+i).val().length;
$('#counters-'+i).html(current);
if(current<0 || current==maxlen)
{
$('#counters-'+i).css('color','#D40D12');
$('#comment_button-'+i).attr('disabled','disabled').addClass('inact');
}
else if (!$.trim($("#ctextarea-'+i).val())) {
$('#comment_button-'+i).attr('disabled','disabled').addClass('inact');
}
else
$('#comment_button-'+i).removeAttr('disabled').removeClass('inact');
if(current<10)
$('#counters-'+i).css('color','#D40D12');
else if(current<20)
$('#counters-'+i).css('color','#5C0002');
else
$('#counters-'+i).css('color','#C0C0C0');
}
And your form should look like -
<form method="post" action="" id="form">
<textarea name="comment" class="comment" id="ctextarea-1" ></textarea>
<input type="hidden" name="uid" id="uid" value="1"/>
<div class="p"></div>
<input type="submit" value="Comment" id="comment_button-1" class="comment_button"/>
<span id="counters-1">
280
</span>
<textarea name="comment" class="comment" id="ctextarea-2" ></textarea>
<input type="hidden" name="uid" id="uid" value="2"/>
<div class="p"></div>
<input type="submit" value="Comment" id="comment_button-2" class="comment_button"/>
<span id="counters-2">
280
</span>
...
<textarea name="comment" class="comment" id="ctextarea-N" ></textarea>
<input type="hidden" name="uid" id="uid" value="N"/>
<div class="p"></div>
<input type="submit" value="Comment" id="comment_button-N" class="comment_button"/>
<span id="counters-N">
280
</span>
</form>

Categories