My json object should look like below, which is generating on submiting the form:
{
"range":{
"l1_price":
{"from":"0", "to":"260k", "price": "Rs .02"},
"l2_price":
{"from":"260k", "to":"500k", "price": "Rs .04"},
"l3_price":
{"from":"500k", "to":"1MM", "price": "Rs .007"},
"l4_price":
{"from":"1MM", "to":"2MM", "price": "Rs .003"}
}
}
Now the form is like:
<form class="json_out">
<p>
l1_price:
<input type="text" name="from_l1" placeholder="from range">
<input type="text" name="to_l1" placeholder="to range">
<input type="text" name="price_l1" placeholder="price">
</p>
<p>
l2_price:
<input type="text" name="from_l2" placeholder="from range">
<input type="text" name="to_l2" placeholder="to range">
<input type="text" name="price_l2" placeholder="price">
</p>
<p>
l3_price:
<input type="text" name="from_l3" placeholder="from range">
<input type="text" name="to_l3" placeholder="to range">
<input type="text" name="price_l3" placeholder="price">
</p>
<p>
l4_price:
<input type="text" name="from_l4" placeholder="from range">
<input type="text" name="to_l4" placeholder="to range">
<input type="text" name="price_l4" placeholder="price">
</p>
<p>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</p>
</form>
Finally i want to send the data to data.json file on submitting the form.
JavaScript code:
<script>
$('form.json_out').submit(function(e) {
var values = {};
var range = {};
var rates = [];
var thresholds = [];
var result = {};
e.preventDefault();
$.each($('form.json_out').serializeArray(), function(i, field) {
if(field.name){
temp = {};
name_split = field.name.split('_');
price_lev = name_split[1] + '_' + 'price';
val_str = name_split[0];
val = field.value;
temp[val_str] = val
if (val_str == 'price'){rates.push(val)};
if (val_str == 'to'){thresholds.push(val)};
if (range[price_lev]){
res = range[price_lev]
res[val_str] = val
range[price_lev] = res
}
else{
range[price_lev] = temp;
}
}
});
result['range'] = range
result['rates'] = rates
result['thresholds'] = thresholds
});
</script>
Related
I'm doing a HTML form, it's meant to create step to pass on a game and I want to be able to enter a number and when for example I enter 5 it instantly show me 5 form to fill and create new step but when I try it doesn't appear how can I do please?
<h3>Insertion des étape :</h3>
<form action="InsertionEtape.php" method="post">
<input type="number" name="quantity" min="1" max="5">
<br>
<?php for ($i=0; $i < $_GET['quantity']; $i++) {
?>
<h5>Nom de l'étape</h5>
<input type="text" name="NomEtape" size="40" maxlength="40">
<br>
<h5> Description de l'étape </h5>
<textarea name="DescriptionEtape" rows="8" cols="80"></textarea>
<br>
<br>
<input type="submit" value="Valider">
<?php
} ?>
</form>
<h3>Insertion des étape :</h3>
<form action="index.php" method="post">
<input type="number" name="quantity" id="quantity" min="1" max="5">
<div id="formcontainer">
</div>
<input type="submit" value="Valider">
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
jQuery(function($) {
$("#quantity").bind('keyup mouseup', function () {
var count = $('#quantity').val();
var htmlf = '';
for (i = 0; i < count; i++) {
htmlf += "<h5>Nom de l'étape</h5><br/>";
htmlf += '<input type="text" name="NomEtape_"'+i+' size="40" maxlength="40">';
htmlf += "<h5>Description de l'étape</h5><br/>";
htmlf += '<textarea name="DescriptionEtape_"'+i+' rows="8" cols="80"></textarea>';
htmlf += "<br/>"
}
$("#formcontainer").html(htmlf);
});
});
</script>
Simple Just run the code and you can find yourself easy.
Right so it sounds like you want this form to update on page. For this you'll need to use JavaScript. PHP runs server side only, you want this to update on the client side.
Here is a quick example to get you on the right path. I am using jQuery which is a popular Javascript library.
var $group = $('form .group').clone();
$('[name="quantity"]').on('change input keyup cut paste', function() {
var quantity = parseInt($(this).val());
$('form .group').remove();
for (var i = 0; i < quantity; i++) {
$group.clone().insertBefore('form [type="submit"]');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h3>Insertion des étape :</h3>
<form action="InsertionEtape.php" method="post">
<input type="number" name="quantity" value="1" min="1" max="5">
<br>
<div class="group">
<h5>Nom de l'étape</h5>
<input type="text" name="NomEtape[]" size="40" maxlength="40">
<br>
<h5> Description de l'étape </h5>
<textarea name="DescriptionEtape[]" rows="8" cols="80"></textarea>
<br>
</div>
<input type="submit" value="Valider">
</form>
okey I changed to this but an error appeared,
<form action="InsertionEtape.php" method="post">
<input type="number" name="quantity" min="1" max="5">
<br>
<?php for ($i=0; $i < $_GET['quantity']; $i++) {
echo "<h5>Nom de l'étape</h5>";
echo "<input type=\"text\" name=\"NomEtape_".$i."\" size=\"40\" maxlength=\"40\"> <br>";
?>
<h5> Description de l'étape </h5>
<textarea name="DescriptionEtape" rows="8" cols="80"></textarea>
<br>
<br>
<input type="submit" value="Valider">
<?php
} ?>
</form>
Notice: Undefined index: quantity in /***/**********/*****/******/WWW/Page_Administrateur/FormulaireInsertion.php on line 106
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>
i have multiple rows insertion, so, i want to insert them into database.
i am trying to use loop function to send them to a table; but not success. Please anyone help with this?
//Adding New Line Item Function
var i = 1;
function AddNew(){
if (i <=3){ //if you don't want limit, you remove IF condition
i++;
var div= document.createElement('div');
div.innerHTML = '<input type="text" value="'+i+'" name="lineitem_'+i+'" maxlength="2" size="2"> <input type="text" name="materialcode_'+i+'" placeholder="Material Code" maxlength="18" size="18"> <input type="text" name="qty_'+i+'" placeholder="Quantity" maxlength="10" size="10"> <input type="text" name="price_'+i+'" placeholder="Price" maxlength="10" size="10"> <input type="button" onclick="removeItem(this)" value="-">';
document.getElementById('addingitem').appendChild(div);
}
}
function removeItem(div){
//document.getElementById('addingitem').removeChild(div.parentNode);
i--;
var addingitem = document.getElementById('addingitem');
addingitem.removeChild(div.parentNode);
var inputs = addingitem.querySelectorAll('[name^="lineitem_"]');
for (var r = 0; r < inputs.length; r++)
{
var item = inputs[r];
item.value = r + 1;
}
inputs = null;
}
//Validate Number Key
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : evt.keyCode
return !(charCode > 31 && (charCode < 48 || charCode > 57));
}
<form method="post" action="make_order.php">
<!--Adding item-->
Item Detail:
<br />
<div id="addingitem">
<input type="text" name="lineitem_1" id="item" value="1" maxlength="2" size="2">
<input type="text" name="materialcode_1" placeholder="Material Code" maxlength="18" size="18">
<input type="text" name="qty_1" placeholder="Quantity" maxlength="10" size="10">
<input type="text" name="price_1" placeholder="Price" maxlength="10" size="10">
<input type="button" name="addnew" id="Add_New()" onClick="AddNew()" value="New Item">
</div>
<br />
<input type="submit" name="makeorder" value="Make Order">
</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
hello guys? can you help me with this? i want to get the id of the first input and the text value of the second input in a specific p tag
<p class="receiveitem">
<label for="mat_id">Material</label>
<input type="text" id="4"value="GI Coulping" disabled/>
<label for="rec_qty">Quantity</label>
<input type="text" class="rec_qty" />
<input type="button" class="receive" value=Receive />
</p>
<p class="receiveitem">
<label for="mat_id">Material</label>
<input type="text" id="5"value="Vulca Seal" disabled/>
<label for="rec_qty">Quantity</label>
<input type="text" class="rec_qty" />
<input type="button" class="receive" value=Receive />
</p>
<p class="receiveitem">
<label for="mat_id">Material</label>
<input type="text" id="6"value="teflon tape" disabled/>
<label for="rec_qty">Quantity</label>
<input type="text" class="rec_qty" />
<input type="button" class="receive" value=Receive />
</p>
so when i click the receive button that's in the p tag.
it will output someting like
ID: 4 and Value: 10
already have a js.but i can't get it done.
$(document).ready(function(){
$('input#receive').click(function(){
var mat = $('input[type=text]:first').attr('id');
var qty = $('input[type=text]:last').val;
alert(mat + qty);
});
});
and i dont know where to put the this in my variables mat and qty.please help?
Here is a modified approach using the more semantically correct data attributes (which are broadly supported) (building on #Chase's answer) (and a demo)
<p class="receiveitem">
<label for="mat_id">Material</label>
<input name="mat_id" type="text" data-id="4" value="GI Coulping" disabled="disabled"/>
<label for="rec_qty">Quantity</label>
<input type="text" name="rec_qty" />
<input name="rec_qty" type="button" class="receive" value="Receive" />
</p>
<p class="receiveitem">
<label for="mat_id">Material</label>
<input name="mat_id" type="text" data-id="5" value="Vulca Seal" disabled="disabled"/>
<label for="rec_qty">Quantity</label>
<input type="text" name="rec_qty" />
<input type="button" class="receive" value="Receive" />
</p>
<p class="receiveitem">
<label for="mat_id">Material</label>
<input name="mat_id" type="text" data-id="6" value="teflon tape" disabled="disabled"/>
<label for="rec_qty">Quantity</label>
<input type="text" name="rec_qty" />
<input type="button" class="receive" value="Receive" />
</p>
With this JavaScript
$(document).on("click", "input.receive", function () {
var mat = $(this).siblings("[name='mat_id']").data("id");
var qty = $(this).siblings("[name='rec_qty']").val();
alert("mat id: " + mat + " qty val: " + qty);
});
This approach won't abuse the id attribute which is designed for node identification and not data storage.
$(document).on('click', 'input#receive', function(e){
e.preventDefault();
var qty = $(this).prev().prev('input');
alert(qty);
});
You really need to change your markup to use data or unique IDs though.