Wondering if someone can point me in the right direction.
This is the main page of the project (index.php). The script below is receiving values from fetch.php which displays properly. All I need is to autocomplete the 2 input boxes client_id and title. I know the problem is with the script, but can't figure it out.
<input type="text" id="client_id" name="client_id" placeholder="ID" />
<input type="text" id="status" name="status" placeholder="Status" autocomplete="off" />
<input type="text" id="title" name="title" placeholder="Client" autocomplete="off" />
function setText(obj){
var val = obj.value;
console.log(val);
function concat(){
var x = <?php echo $client_id; ?>
var y = <?php echo $first_name; ?>
document.getElementById("client_id").value = x;
document.getElementById("title").value = y;
}
concat();
}
If was was to have the following script, title would fill in automagically.
function setText(obj){
var val = obj.value;
console.log(val);
document.getElementById('title').value = val;
}
You're mixing php in with javascript in a way that doesn't make sense, since you say the values are coming in from a fetch
If it really is PHP, then (obviously) you could set the inputs directly
<input type="text" id="client_id" name="client_id" placeholder="ID" value="<?php echo $client_id; ?>" />
<input type="text" id="status" name="status" placeholder="Status" autocomplete="off" />
<input type="text" id="title" name="title" placeholder="Client" autocomplete="off" value=" <?php echo $first_name; ?>" />
If it's coming in through fetch, you could do it this way:
let ref = 12; // the reference to get the data
fetch(`http://example.com/getClientData.php?ref=${ref}`)
.then(response => response.json())
.then(data => {
document.getElementById("client_id").value = data.client_id;
document.getElementById("title").value = data.client_title;
console.log(data)
});
Related
Currently I'm working on a large form with dynamic number of input fields, it can get extremely large (above 3k variables or more) and it causes problems with 'max_input_vars' option.
I'm looking for a workaround for this issue (I'm not interested in increasing the value of 'max_input_vars' option), so currently I'm trying to join all form fields values into single field (using jQuery serialize() method) and then recreate the variables on the server side.
This is what I have so far:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Workaround for max_input_vars</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<?php
if($_SERVER["REQUEST_METHOD"]==="POST" && !empty($_POST["data"])) {
$vars = explode("&", $_POST["data"]);
$data = array();
foreach($vars as $var) {
parse_str($var, $variable);
assign_var($_POST, $variable);
}
echo "<pre>";
var_dump($_POST);
echo "</pre>";
}
function assign_var(&$target, $var) {
$key = key($var);
if(is_array($var[$key]))
assign_var($target[$key], $var[$key]);
else {
if($key==0)
$target[] = $var[$key];
else
$target[$key] = $var[$key];
}
}
?>
<form id="myForm" method="POST">
<input type="text" name="var_1" value="<?php echo htmlentities("double quote: \"hello\"");?>"/><br/>
<input type="text" name="var_2" value="<?php echo htmlentities("single quote: 'hello'");?>"/><br/>
<input type="text" name="var_3" value="<?php echo htmlentities("text with & ampersant");?>"/><br/>
<input type="text" name="var_4" value="<?php echo htmlentities("animals");?>"/><br/>
<input type="text" name="matrix[]" value="1"/><br/>
<input type="text" name="matrix[]" value="2"/><br/>
<input type="text" name="matrix[]" value="3"/><br/>
<input type="text" name="matrix[]" value="4"/><br/>
<input type="text" name="matrix[]" value="5"/><br/>
<input type="text" name="matrix[]" value="6"/><br/>
<input type="text" name="matrix[]" value="7"/><br/>
<input type="text" name="var_5" value="abc"/><br/>
<input type="text" name="var_6" value="bcd"/><br/>
<input type="text" name="var_7" value="cde"/><br/>
<input type="text" name="var_8" value="def"/><br/>
<input type="text" name="multi_matrix[colors][]" value="red"/><br/>
<input type="text" name="multi_matrix[colors][]" value="blue"/><br/>
<input type="text" name="multi_matrix[colors][]" value="green"/><br/>
<input type="text" name="multi_matrix[weight][]" value="75"/><br/>
<input type="text" name="multi_matrix[weight][]" value="83"/><br/>
<input type="text" name="multi_matrix[weight][]" value="62"/><br/>
<input type="text" name="multi_matrix[height][]" value="170"/><br/>
<input type="submit" value="Send">
</form>
<style>
input {
margin: 5px 0;
}
</style>
<script type="text/javascript">
(function(){
$(function(){
$("#myForm").submit(function(event) {
var $this = $(this);
var data = $this.serialize();
$this.find("input, textarea, select, button").remove();
$this.append("<input type='hidden' class='data' name='data'/>");
$this.find("input.data").val(data);
});
});
})(jQuery);
</script>
</body>
</html>
As you can see, I'm intercepting the submit event and replace all form fields with single 'data' field. On PHP side I'm using explode() and pars_str() methods to recreate the variable, lastly I want to put the variable into $_POST array, so I created simple recursive function that can handle simple values and multidimensional associative arrays.
The result is a $_POST array filled with form values, as if the form was submitted normally.
What do you think about above method ? Maybe there's a much simpler solution to the problem that I hadn't noticed earlier ? If not, are there any options to improve the above code ? Are there any negative side effects of the above solution ?
Thanks in advance !
I have to parse this jQuery output <span id="chargetotal" class="chargetotal">Amount</strong></span> to a PSP shopping card page.
From this page i try to parse the 'Total amount' (Quantity * Value), and the rest of the form variables to the PSP shopping card page (https://test.ipg-online.com/connect/gateway/processing).
On the voucher page i include ipg-util.php which look like this:
<?php
$dateTime = date("Y:m:d-H:i:s");
function getDateTime() {
global $dateTime;
return $dateTime;
}
function createHash($chargetotal, $currency) {
$storeId = "XXX storeId XXX";
$sharedSecret = "XXX sharedSecret XXX";
$stringToHash = $storeId . getDateTime() . $chargetotal .
$currency . $sharedSecret;
$ascii = bin2hex($stringToHash);
return sha1($ascii);
}
?>
In this instruction i setup 3.3 PHP Example
The form look like this:
<form method="post" action="https://test.ipgonline.com/connect/gateway/processing">
<input type="hidden" name="txntype" value="sale">
<input type="hidden" name="timezone" value="GMT"/>
<input type="hidden" name="txndatetime" value="<?php echo getDateTime() ?>"/>
<input type="hidden" name="hash" value="<?php echo createHash( "13.00","978" ) ?>"/>
<input type="hidden" name="storename" value="XXX My store id XXX"/>
<input type="hidden" name="mode" value="fullpay"/>
<input type="text" name="chargetotal" value="13.00"/>
<input type="hidden" name="currency" value="978"/>
<input type="hidden" name="responseSuccessURL" value="http://www.abbeyglen.ie/abbeyglen-castle-hotel/voucher-succes.html"/>
<input type="hidden" name="responseFailURL" value="http://www.abbeyglen.ie/abbeyglen-castle-hotel/voucher-failure.html"/>
<input type="submit" value="Submit">
</form>
My problem is that in the above form example they use a strict value of 13.00 for <input type="text" name="chargetotal" value="13.00"/> to create the hash 'form variable'.
But in my case chargetotal is the 'Total amount' of the jQuery function in this HTML <span id="chargetotal" class="chargetotal">Amount</strong></span> .
See below, the jQuery script to get the 'Total amount'.
<script type="text/javascript">
$(document).ready(function(){
update_amounts();
$('.qty').change(function() {
update_amounts();
});
});
function update_amounts()
{
var chargetotal = 0;
var sum = 0.0;
$(function () {
var qty = $(this).find('.qty').val();
var price = $(this).find('.price').val();
var chargetotal = (qty*price)
sum+=chargetotal;
$(this).find('.chargetotal').text(''+chargetotal);
});
}
</script>
How do i translate the jQuery 'Total amount' to a <input type="text" name="chargetotal">?
Thanks!
Add
<input type="hidden" name="chargetotal" id="chargetotal" value=""/>
to your form.
Remove id="chargetotal" from
<span class="chargetotal" id="chargetotal"></span>
Add
$('#chargetotal').val(chargetotal);
after
$(this).find('.chargetotal').text(''+chargetotal);
I have a form, with the 3 input type text.
<input type="text" id="one" onkeyup="multiply()" name="one">
<input type="text" id="two" name="two">
<input type="text" id="three" name="three">
<script>
function multiply(){
one = document.getElementById('one').value;
two = document.getElementById('two').value;
document.getElementById('three').value = one * two
}
</script>
now i don't have value in three, but it is a dynamic one, when i submit forum to (forumSubmit.php)then i get error of
undefiend index three
I searched & found this can be done with ajax, but i don't want to use ajax, i want to make a page refresh
You could do something like this instead:
Markup
<!-- Use onkeyup on both inputs -->
<input type="text" id="one" onkeyup="multiply()" name="one">
<input type="text" id="two" onkeyup="multiply()" name="two">
<input type="text" id="three" name="three">
JavaScript
function multiply() {
// Parse the values, and count it as 0 if the input is not a number
// I also made the variables private to this function using the var keyword
// There is no need to have them in the global namespace
var one = parseInt(document.getElementById('one').value, 10) || 0;
var two = parseInt(document.getElementById('two').value, 10) || 0;
document.getElementById('three').value= one * two;
}
Working example
Put together a demo: http://jsfiddle.net/DjQNx/
<input type="text" id="one" onkeyup="multiply()" name="one" />
<input type="text" id="two" onkeyup="multiply()" name="two" />
<input type="text" id="three" name="three" />
<script type="text/javascript">
function multiply() {
one = document.getElementById('one').value;
two = document.getElementById('two').value;
document.getElementById('three').value = one * two;
}
</script>
I know this seems like I should have immediately found the one example out there that simply explained this concept, and I can't find the right one. The examples in tutorials out there use flickr as the request url, and I get it up to a point. I'm trying to handle a very complex form with four different buckets of info under one larger bucket of info. I though json would be a good choice. So I found an example that shows me how to serialize the data like this:
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
Which is great! Via an alert, I can see the json structure and my form elements. So I thought the rest would be totally simple. And here I am banging my head because I can't find one simple example of HOW to SEND or REQUEST this serialized data from my processing page which needs to print, organize and calculate the values of what was sent.
So what I did next (and I'm sure it's wrong, but perhaps this will illustrate how new I am to it and where I need to start in my understanding of it) was to write the serialized data out to a hidden field like so:
$('form').submit(function() {
var field = '<input type="hidden" name="jsonval" value="' + $.toJSON($('form').serializeObject()) + '">';
// alert($.toJSON($('form').serializeObject()));
alert(field);
document.write(field);
return true;
});
I'm using the GET method in the form and when it pops the alert it looks fine, then it goes to the next page. Only that hidden var I wrote out isn't there. The rest of the form values are. I just WANT to see the darn json somehow so I can then use json.parse or whatever I have to use in PHP to decode it.
Here's the complete code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title>Attendees</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://jquery-json.googlecode.com/files/jquery.json-1.3.min.js"></script>
</head>
<body>
<form action="receive.php" method="get">
<p><br/>
First Name:<input type="text" name="Fname" maxlength="12" size="12"/> <br/>
Last Name:<input type="text" name="Lname" maxlength="36" size="12"/> <br/>
Preconference:<br/>
A:<input type="radio" name="gender" value="A"/><br/>
B:<input type="radio" name="gender" value="B"/><br/>
What days:<br/>
ConfA:
<input type="checkbox" name="conf[]" value="ConfA"/><br/>
ConfB:
<input type="checkbox" name="conf[]" value="ConfB"/><br/>
ConfC:
<input type="checkbox" name="conf[]" value="ConfC"/>
</p>
<p><br/>
<input name="quote" type="text" value="Enter your meal preference" size="20">
</p>
<p><br/>
Select a role:<br/>
<select name="education">
<option value="decision">Decision Maker</option>
<option value="person">Person</option>
<option value="fcg">Family</option>
</select>
</p>
<p><br/>
Select your educational credits:<br/>
<select size="3" name="edu">
<option value="nursing">Nursing</option>
<option value="welding">Welding</option>
<option value="case">Case Managers</option>
</select>
</p>
<hr>
First Name:<input type="text" name="Fname2" maxlength="12" size="12"/> <br/>
Last Name:<input type="text" name="Lname2" maxlength="36" size="12"/> <br/>
Preconference:<br/>
A:<input type="radio" name="gender" value="A2"/><br/>
B:<input type="radio" name="gender" value="B2"/><br/>
What days:<br/>
ConfA:
<input type="checkbox" name="conf[]" value="ConfA2"/><br/>
ConfB:
<input type="checkbox" name="conf[]" value="ConfB2"/><br/>
ConfC:
<input type="checkbox" name="conf[]" value="ConfC2"/>
</p>
<p><br/>
<input name="quote2" type="text" value="Enter your meal preference" size="20">
</p>
<p><br/>
Select a role:<br/>
<select name="education2">
<option value="decision2">Decision Maker</option>
<option value="person2">Person</option>
<option value="fcg2">Family</option>
</select>
</p>
<p><br/>
Select your educational credits:<br/>
<select size="3" name="edu2">
<option value="nursing2">Nursing</option>
<option value="weld2">Welding</option>
<option value="case2">Case Managers</option>
</select>
</p>
<script type="text/javascript">
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
$('form').submit(function() {
var field = '<input type="hidden" name="jsonval" value="' + $.toJSON($('form').serializeObject()) + '">';
// alert($.toJSON($('form').serializeObject()));
alert(field);
document.write(field);
return true;
});
</script>
<p><input type="submit" /></p>
</form>
</body>
</html>
How do I get it to just communicate in some fashion with the receive.php page? Thanks for fielding such a newbie question. I did search for awhile, but can't figure out if I need to use a framework, or if there is a nice straightforward way.... There were so many different kinds of examples that were more complex than this problem seemed to call for.
Try putting the hidden input there to begin with
<input type="hidden" name="jsonval" id="jsonval" value="" />
<!-- also make sure to end your input tags with a "/" -->
And then in your javascript just do...
var j = $.toJSON($('form').serializeObject());
//You might need this if the quotes are making things annoying
//j = encodeURIComponent(j);
document.getElementById('jsonval').value = j;
I am creating a dynamic form where the user will have the ability to add a set of inputs to the form. The html looks like this:
<form>
<input id="title1" class="title" name="title1" type="text" value="">
<input id="productionCompany1" name="productionCompany1" type="text" value="">
<input id="year1" name="year1" type="text" value="">
<input id="role1" name="role1" type="text" value="">
<div id="newCredit"> </div>
add another credit
</form>
When the user clicks the link with the id of "addCredit" the following jQuery script is called:
$(document).ready(function() {
var $ac = $('#addCredit');
$ac.click(function() {
/* the following two lines are where the problem lies? */
var $credInt = $(this).prev(".title");
$.get("addCredit.php", {num: $credInt},
function(data){
$('#newCredit').append(data);});
return false;
});
});
The jQuery function queries a php file called "addCredit.php", which looks like this:
<?php
$int = $_GET["num"];
$int = substr($int, -1);
$int++;
?>
<input id="title<?php echo $int;?>" class="title" name="title<?php echo $int;?>" type="text" value="">
<input id="productionCompany<?php echo $int;?>" name="productionCompany<?php echo $int;?>" type="text" value="">
<input id="year<?php echo $int;?>" name="year<?php echo $int;?>" type="text" value="">
<input id="role<?php echo $int;?>" name="role<?php echo $int;?>" type="text" value="">
My problem is getting the javascript variable $credInt set properly so that it can be sent to the addCredit.php page and update the form fields accordingly. I also need to be sure that every time the form is appended, the next value sent is the incremented value.
Any thoughts on how I might accomplish this? Thank you for your help.
This is the wrong way of doing it; PHP can handle array syntax in a variable name. This makes it much easier to handle. It is also unnecessary to call the server to clone the form. You should name your fields like this:
<form>
<div id="originalCredit">
<input name="title[]" type="text" value="">
<input name="productionCompany[]" type="text" value="">
<input name="year[]" type="text" value="">
<input name="role[]" type="text" value="">
</div>
add another credit
</form>
And then your Javascript can be like this:
$(function() {
$('#addCredit').click(function() {
var newCredit = $('#originalCredit').clone(); // create new set
newCredit.find('input').val(''); // empty input fields
$(this).before(newCredit); // append at the end
return false;
});
});
When the form is finally sent to the server, because the variables are in the format of name[] PHP will recognize they are an array and then you can do this:
<? foreach($_POST['title'] as $k => $v) { ?>
Title: <?=$_POST['title'][$k]?><br>
Company: <?=$_POST['productionCompany'][$k]?><br>
Year: <?=$_POST['year'][$k]?><br>
Role: <?=$_POST['role'][$k]?><br>
<? } ?>
Obviously this is just displaying it as an example, but you can then save/update/whatever with it.