adding input box dynamically when filling the other input box - php

i have a form like this
<FORM method="post">
<TABLE>
<TR>
<TD>Username</TD>
<TD><INPUT type="text" value="" name="username" title="Enter Username"/><TD>
</TR>
<TR>
<TD>Age</TD>
<TD><INPUT type="text" value="" name="username" title="Enter Age"/><TD>
</TR>
<TR>
<TD>City</TD>
<TD><INPUT type="text" value="" name="username" title="Enter City"/><TD>
</TR>
<TR>
<TD>Comment</TD>
<TD><INPUT type="text" value="" name="username" title="Enter Comment"/><TD>
</TR>
</TABLE>
</FORM>
what i want is when user after filling the comment box ,when they press enter then dynamically another input box has to display with comment label by using javascript.
can anyone help me to write the code...

Try this my friend:
<FORM method="post">
<TABLE>
<TR>
<TD>Username</TD>
<TD><INPUT type="text" value="" name="username" title="Enter Username"/><TD>
</TR>
<TR>
<TD>Age</TD>
<TD><INPUT type="text" value="" name="age" title="Enter Age"/><TD>
</TR>
<TR>
<TD>City</TD>
<TD><INPUT type="text" value="" name="city" title="Enter City"/><TD>
</TR>
<TR>
<TD>Comment</TD>
<TD><INPUT class="comment" type="text" value="" name="comment[]" title="Enter Comment"/><TD>
</TR>
</TABLE>
</FORM>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(function(){
$('.comment').live('keydown', function(e){
$field = $(this);
// capture enter key being pressed
if (e.which == 13)
{
$html = $(this).parents('tr').clone();
$html.find('input').val('');
$html.appendTo($field.parents('table'));
}
});
});
</script>

First of all your all the input names in your form are the same (name="username"), you have to give each input a unique name.
so change comment input to
<TR id="comment">
<TD>Comment</TD>
<TD><INPUT type="text" value="" name="comment[]" title="Enter Comment"/><TD>
</TR>
note that i added id to the TR so it'll be easier to latch on to it using jquery and i also changed the input name to be comment[] since if you add a new comment to the dom you'll have a comment array.
if the new comment should have a new name like : 'comment2', you can change it to be just 'comment'
Now, using Jquery do the following:
$(document).keypress(function(e) {
//catch enter event
if(e.which == 13) {
var $comment = $('#comments);
//check that the user entered something in the comment input
if($comment.find('input').val().length > 0)
{
//create new comment element and attach it to DOM
var $newComment = $comment.clone().removeAttr('id');
$('form table').append($newComment);
}
}
});
let me know if that helped or if you need more info.

Related

I need the (Submit this form )button to be clicked automatically when the page loads (WORDPRESS)

iam customizing a page in wordpress , i dont need the user to see this page when it loads , so i need the Sumnit this form button to be clicked automatically the moment this page loads-for security reasons-...the user doesnt need to fill anything on this form.
this is the button code:
<td colspan="2" align="center"><input name="submitBtn" type="submit"value="Submit This Form" /></td>
and this is the complete code:
[insert_php]
date_default_timezone_set('Asia/Kolkata');
$dateTime= date('Y:m:d-H:i:s');
function getDateTime() {
global $dateTime;
return $dateTime;
}
function createHash($chargetotal,$Currency) {
$storeId = "330995001";
$sharedSecret = "sharedsecret";
$dateTime= date('Y:m:d-H:i:s');
$stringToHash = $storeId . $dateTime . $chargetotal . $Currency .
$sharedSecret;
$ascii = bin2hex($stringToHash);
return sha1($ascii);
}
[/insert_php]
IPG Connect Sample for PHP(hashing)
<h1>Order Form</h1>
[insert_php]
$responseSuccessURL = "http://127.0.0.1:8006/wordpress/?page_id=27";
$responseFailURL = "http://127.0.0.1:8006/wordpress/?page_id=31";
[/insert_php]
<form action="https://test.ipg-online.com/connect/gateway/processing"
method="post"><input name="timezone" type="hidden" value="IST" />
<input name="authenticateTransaction" type="hidden" value="true" />
<table>
<tbody>
<tr>
<td>Transaction Type</td>
<td><input name="txntype" size="50" type="text" value="sale" /></td>
</tr>
<tr>
<td>Transaction Date Time</td>
<td><input name="txndatetime" size="50" type="text" value="[insert_php] echo
$dateTime; [/insert_php]" /></td>
</tr>
<tr>
<td>Calculated HASH</td>
<td><input name="hash" size="50" type="text" value="[insert_php] echo
createHash("1","356"); [/insert_php]" /></td>
</tr>
<tr>
<td>Currency</td>
<td><input name="currency" size="50" type="text" value="356" /></td>
</tr>
<tr>
<td>Payment Mode</td>
<td><input name="mode" size="50" type="text" value="payonly" /></td>
</tr>
<tr>
<td>Store Id</td>
<td><input name="storename" size="50" type="text" value="330995001" /></td>
</tr>
<tr>
<td>Chargetotal</td>
<td><input name="chargetotal" size="50" type="text" value="1" /></td>
</tr>
<tr>
<td>Shared Secret</td>
<td><input name="sharedsecret" size="50" type="text" value="sharedsecret" />
</td>
</tr>
<tr>
<td>Language</td>
<td><select name="language">
<option value="de_DE">Deutsch</option>
<option selected="selected" value="en_EN">English</option>
</select></td>
</tr>
<input type="hidden" name="responseSuccessURL" value="[insert_php] echo
$responseSuccessURL; [/insert_php]"/>
<input type="hidden" name="responseFailURL" value="[insert_php] echo
$responseFailURL; [/insert_php]"/>
<tr>
<td colspan="2" align="center"><input name="submitBtn" type="submit"
value="Submit This Form" /></td>
</tr>
<input type="hidden" name="hash_algorithm" value="SHA1"/>
</tbody>
</table>
</form>
[/insert_php]
with Javascript, you can do it like
window.onload = function() {
document.getElementById("formId").submit();
};
You can do it with JQuery like this:
<script type="text/javascript">
$(document).ready(function(){
$( "#formId" ).submit(
});
</script>
I am submitting form directly, you can click on submit button as well that will do that submit operation.
You can use trigger after document is ready,
<script type="text/javascript">
$(document).ready(function(){
$( "#foo" ).trigger( "click" );
});
</script>
Just replace the foo with your submit button ID

if the field empty JQUERY

I wrote a simple page with HTML and PHP. So, before PHP I would like to check empty fields with Jquery, but I do not learn the Jquery yet, so I would appreciated if someone helped me.
<?php
if(isset($_POST[add]))
{
if(empty($_POST[name]) || empty($_POST[surname])) {echo 'All form fields are required';}
}
?>
<form action="" method=post>
<table border=0 cellspacing=10 cellpadding=5>
<tr>
<td>Name:</td>
<td><input type="text" name="name" size="10" value=""></td>
</tr>
<tr>
<td>Surname:</td>
<td><input type="text" name="surname" size="10" value=""></td>
</tr>
<tr>
<td colspan=2><input type="submit" name="add" value="Add"></td>
</tr>
</table>
</form>
You can add validation by adding jquery.
$('input[name="add"]').click(function() {
if($('input[name="name"]').val() == '' || $('input[name="surname"]').val() == '') {
$('.error').html('');
if($('input[name="name"]').val() == '') {
$('.error').append('<p>Please enter your name</p>');
}
if ($('input[name="surname"]').val() == '') {
$('.error').append('<p>Please enter your surname</p>');
}
$('.error').show();
return false;
}
return true;
})
html code
<div class="error" style="display:none"></div>
<form action="" method=post>
<table border=0 cellspacing=10 cellpadding=5>
<tr>
<td>Name:</td>
<td><input type="text" name="name" size="10" value=""></td>
</tr>
<tr>
<td>Surname:</td>
<td><input type="text" name="surname" size="10" value=""</td>
</tr>
<tr>
<td colspan=2><input type="submit" name="add" value="Add"></td>
</tr>
</table>
</form>
Add a javascript function on submit let say validate() and add this jquery code in that
function validate() {
var name = $("#name").val();
if (name == "") {
alert('Name is required');
return false;
}
}
<input class="field-name" type="text" name="name" size="10" value="">
i added a class to this input field so i can use a jquery selector
var name = $('.field-name').val();
if(name="") {
console.log('name is empty');
//your code here to do something when the field is empty
}
Following code can give you some idea. I would suggest to read tutorial on jquery at
http://www.w3schools.com/jquery/default.asp
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<script>
$(document).ready(function(){
$("#add").submit(function(){
if ($('#name').length == ""){
alert("ERROR in name!");
}
});
});
</script>
<form action="" method=post>
<table border=0 cellspacing=10 cellpadding=5>
<tr>
<td>Name:</td>
<td><input type="text" name="name" id="name" size="10" value=""></td>
</tr>
<tr>
<td>Surname:</td>
<td><input type="text" name="surname" id="surname" size="10" value=""</td>
</tr>
<tr>
<td colspan=2><input type="submit" name="add" id="add" value="Add"></td>
</tr>
</table>
</form>
</html>

Jquery Button to make visible a form

I want to ask about how to make a form invisible when a button clicked? So, if I have a button called "Hide" and a form with many button, textbox, etc.
Then when I click "Hide" button, it will hide all form and all things in form such as textbox, button, etc. I have google it but have no result. I accept answer with Jquery, JS, or php language because I'm using that language program.
Example my form is like this:
<form name="myform" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<table>
<tr>
<td>ID</td>
<td>:</td>
<td><input type="text" maxlength="15" name="clientid" /></td>
<td><input type="submit" name="cariclientid" value="Search" /></td>
<td width="50px"></td>
<td>ID</td>
<td>:</td>
<td><input type="text" maxlength="15" name="orderid" /></td>
<td><input type="submit" name="cariorderid" value="Search" /></td>
</tr>
<tr>
<td>No.</td>
<td>:</td>
<td><input type="text" maxlength="15" name="veh" /></td>
<td><input type="submit" name="carikendaraan" value="search" /></td>
<td></td>
<td>Nama Sopir</td>
<td>:</td>
<td><input type="text" maxlength="15" name="sopir" /></td>
<td><input type="submit" name="carisopir" value="Cari" /></td>
</tr>
<tr>
<td>Waktu Berangkat</td>
<td>:</td>
<td><input type="text" name="tglb" id="datetimepicker" /></td>
<td><input type="submit" name="cariberangkat" value="Cari" /></td>
<td></td>
<td>Waktu Pulang</td>
<td>:</td>
<td><input type="text" name="tglp" id="datetimepicker2" /></td>
<td><input type="submit" name="caripulang" value="Cari" /></td>
</tr>
</table>
</form>
maybe there's a way to make it invisible by a button?
You want something like this:
// code for only hide
$('#hide_button').on('click', function() {
$('form[name="myform"]').hide();
});
Demo for hide
and for toggle the form with a single button you can try:
$('#your_button').on('click', function() {
$('form[name="myform"]').toggle();
});
Demo for toggle
According to comment
To prevent the submission:
$('form[name="myform"]').submit(function(e) {
e.preventDefault();
// Your code
});
See here for .perventDefault()

Create dynamic forms

Up to this point I've avoided javascript, mostly because I'm a newbie. But I think I can't avoid it anymore?
I have a simple form in a table that I process with php.
<table id="my-table">
<thead>
<tr>
<td>Name</td>
<td>Quality</td>
<td>Status</td>
<td>User</td>
<td>Password</td>
<td>IP</td>
<td>Port</td>
<td>Options</td>
</tr>
</thead>
<form action="<?php htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
<tbody>
<tr>
<td>
<input type="text" size="18" maxlength="32" name="add_name" value="Enter name" />
</td>
<td>
<select name="add_quality">
<option value='HIGH' selected='selected'>High</option>
<option value='MEDIUM'>Medium</option>
<option value='MOBILE'>Mobile</option>
</select>
</td>
<td>
<select name="add_status">
<option value='ENABLED' selected='selected'>Enabled</option>
<option value='DISABLED'>Disabled</option>
</select>
</td>
<td>
<input type="text" size="14" maxlength="16" name="add_user" value="Enter username" />
</td>
<td>
<input type="password" size="12" maxlength="16" name="add_pass" />
</td>
<td>
<input type="text" size="10" maxlength="16" name="add_ip" value="Enter IP" />
</td>
<td>
<input type="text" size="12" maxlength="6" name="add_port" value="Enter Port #" />
</td>
<td>
<input type="submit" name="add" value="Add" />
</td>
</tr>
</tbody>
</form>
</table>
I'd like to take this table and only keep the first three input types shown (Name, Quality, Status). The rest would open up if you hit a "details" link or button. So the table would expand (or ideally go to the next line as this table is too long). That details button or link would have to toggle on and off. Any ideas would be appreciated.
Basic javascript approach:
<script type="text/javascript">
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display === 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
</script>
Then place a button or link with:
<input type="button" value="Details" onClick="toggle_visibility('detailsWrapper')">
Finally, just make a div or maybe a new tr around the fields you want to hide with the id set to "detailsWrapper" and style it with display:none.
Alternatively you can use this function:
function toggle_visibility(id) {
var control = document.getElementById(controlId);
if(control.style.visibility == "visible" || control.style.visibility == "")
control.style.visibility = "hidden";
else
control.style.visibility = "visible";
}
If I remember correctly this will reserve the needed space to display the element, so it won't mess up your layout.
For animations, jQuery is my favorite JavaScript library. It makes things quite a lot easier.
This code might work for you:
$('#my-table tr td').each(function(i) {
if (i >= 3) {
$(this).hide();
}
});
$('#details').click(function() {
$('#my-table tr td').each(function(i) {
if (i >= 3) {
$(this).slideToggle();
}
});
});

Need some resoultion. Javascript function not being called in click event in jquery

I am trying to call my function named isUrgencyTypeValid from my javascript code but it is not working. Please check what is the problem in my code.
Alert should get displayed which is not being displayed
My javascript function is not being called.
HTML Code
<td colspan="2" align="center"><input id="btnSubmit" type="submit" value="submit" runat="server"/></td></tr>
jQuery Call function
$("#btnSubmit").bind("click",function(){
alert(); // this is running
isUrgencyTypeValid();
});
javascript implemented function
function isUrgencyTypeValid()
{
alert("asd");
var i=0;
for(i=0;i<$("radio[name='urgencyType']").length;i++)
{
if($("radio[name='urgencyType']")[i].checked)
{
alert($("radio[name='urgencyType']")[i].value);
return true;
}
return false;
}
More description about my form is here
<form runat="server" name="myPage">
<table style="width: 100%;" cellpadding="5" cellspacing="5">
<caption>
Computer Support / Service Request
</caption>
<tr><td>First Name</td>
<td><input id="txtFirstName" type="text" value="First Name" runat="server"/><span class="error"></span></td></tr>
<tr>
<td>Last Name</td>
<td><input id="txtLastName" type="text" value="Last Name" runat="server"/><span class="error"></span></td></tr>
<tr>
<td>Email Address</td>
<td><input id="txtEmailAddress" type="text" value="Email Address" runat="server"/><span class="error"></span></td></tr>
<tr>
<td>Phone No</td>
<td><input id="txtPhoneNo" type="text" value="Phone No" runat="server" /><span class="error"></span></td></tr>
<tr>
<td>Do you have text messaging</td>
<td>
<span>Yes</span><input id="rdoYes" value="Yes" type="radio" runat="server"/>
<span>No</span><input id="rdoNo" value="No" type="radio" runat="server"/><span class="error"></span>
</td></tr>
<tr>
<td>Description of request*: </td>
<td><textarea id="txtDescription" cols="50" rows="10" runat="server"></textarea><span class="error"></span><span id="lengthCount"></span></td></tr>
<tr>
<td>Urgency of this support request:</td>
<td>
<input id="rdoAnyTime" name="urgencyType" value="Anytime" type="radio" runat="server"/><span>Anytime</span><br />
<input id="rdoCplDays" name="urgencyType" value="In the next couple of days" type="radio" runat="server"/><span>In the next couple of days</span><br />
<input id="rdoToday" name="urgencyType" value="Today" type="radio" runat="server"/><span>Today</span><br />
<input id="rdoUrgent" name="urgencyType" value="This is extremely urgent...I cannot wait!" type="radio" runat="server"/><span>This is extremely urgent...I cannot wait!</span><br />
<input id="rdoTalkSometime" name="urgencyType" value="Please contact me and we'll talk about it" type="radio" runat="server"/><span>Please contact me and we'll talk about it</span><br /><span class="error"></span>
</td></tr>
<tr>
<td colspan="2" align="center">Captcha To Be implemented.</td></tr>
<tr>
<td></td>
<td><input id="chkRequestCopy" type="checkbox" runat="server"/>Please send me a copy of this service request</td></tr>
<tr>
<td colspan="2" align="center"><input id="btnSubmit" type="submit" value="submit" runat="server"/></td></tr>
</table>
</form>
I copied your code and ran into the same issue. Then I looked at it closer and noticed you are missing a closing bracket in your isUrgencyTypeValid() function. The closing bracket for your for loop is missing. Firefox Error Console will also show this.
Once I added the bracket the alert worked fine.
function isUrgencyTypeValid()
{
alert("asd");
var i=0;
for(i=0;i<$("radio[name='urgencyType']").length;i++)
{
if($("radio[name='urgencyType']")[i].checked)
{
alert($("radio[name='urgencyType']")[i].value);
return true;
}
} //this was missing
return false;
}
Your radio selector is incorrect.
There's not such thing as a radio element, but there is a :radio pseudo-class selector.
Your code should read:
function isUrgencyTypeValid()
{
alert("asd");
var i=0;
for(i=0;i<$("input[name='urgencyType']:radio").length;i++)
{
if($("input[name='urgencyType']:radio")[i].checked)
{
alert($("input[name='urgencyType']:radio")[i].value);
return true;
}
return false;
}
** EDIT **
In addition to the changes above, since the alert never occurs, this indicates that isUrgencyTypeValid is not being called.
The most likely explanation is that you're not waiting until the DOM is loaded, before you bind the your handler to the click event of the button. You should have some code somewhere that looks like this:
$(function()
{
/*
* ... other code ...
*/
$("#btnSubmit").bind("click",function(){
isUrgencyTypeValid();
});
/*
* ... other code ...
*/
}

Categories