Get form hidden variables to a function - php

I know that function will need to call out by using onclick or any events. But is it possible to get a hidden value without user handling.
Sample Form:
$ID = 3;
$Report1 = "Test1.docx";
<form id = "Test" action="Nextpage.php">
//Not sure how to call out my function over here...
<input type="hidden" id="ID" name="ID" value="<?php echo $ID ?>"/>
<input type="hidden" id="ReportPath" name="ReportPath" value="<?php echo $ReportPath ?>"/>
//Once user click, function of RemoveDoc() will handle it.
<input type="submit" id="Remove" name="<?php echo $Report1?>" value="Remove" title="Remove report1" onclick="return RemoveDoc(this.name, this.getAttribute('Report1'));" />
My function:
function Remove(ID, ReportPath, Report1)
{
xmlhttp1=new XMLHttpRequest();
xmlhttp1.open("GET","functions/call.php?ID="+ID+"&ReportPath="+ReportPath+"&Report1="+Report1,true);
xmlhttp1.onreadystatechange=function()
{
if (xmlhttp1.readyState==4 && xmlhttp1.status==200)
{
document.getElementById("msg").innerHTML= xmlhttp1.responseText;
}
}
xmlhttp1.send();
return false;
}
So how can i pass in the hidden value of the input into my function Remove(ID,ReportPath...) since it's now hidden and no action taken. Kindly advise.

You can just get
var id = document.getElementById('ID').value;
var reportpath = document.getElementById('ReportPath').value;
var report1 = document.getElementById('Report1').value;
Call the function:
Remove(id, reportpath, report1);
To make things easier for you. You can use jquery. Just include it in your page:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<form id="Test" action="Nextpage.php">
<input type="hidden" id="ID" name="ID" value="<?php echo $ID ?>"/>
<input type="hidden" id="ReportPath" name="ReportPath" value="<?php echo $ReportPath ?>"/>
<input type="submit" id="remove" name="<?php echo $Report1?>" value="Remove"/>
</form>
<script>
$('#remove').click(function(){
var id = $('#ID').val();
var reportpath = $('#ReportPath').val();
var report1 = $('#Report1').val(); //check this as I dont see it in your form
//call your function from here
Remove(id, reportpath, report1);
});
//function definition here
function Remove(id, reportpath, report1)...
</script>

Related

After showing output using jQuery ajax the value doesn't pass using PHP post method

Hi I try to sum two data using AJAX and display it into hidden text input after that, I will pass the variable using PHP post method.
There are no problems in my jQuery AJAX code but the problem is after I submit the button, the value I retrieve from textbox total_rec[] is blank.
Here's my code below.
HTML:
<form method="post" action="<?php echo base_url() ?>user/recitem_insert">
<table>
<thead>
<tr><th>Test</th></tr></thead>
<tbody>
<tr><td>
<input type="hidden" name="last_item_rec[]" value="<?php echo $row->rec_qty; ?>">
<input type="text" name="item_rec[]" id="txt" disabled="">
<input type="hidden" name="total_rec[]" value="">
</td><tr>
</tbody>
</table>
</form>
JQUERY AJAX:
<script>
$(document).ready(function(){
$("input[name=item_rec\\[\\]]").on('keyup',function(){
var one = $(this).parents('tr').find('input[name=last_item_rec\\[\\]]').val();
var two = $(this).parents('tr').find('input[name=item_rec\\[\\]]').val();
sum = parseInt(one) + parseInt(two);
$(this).parents('tr').find('input[name=total_rec\\[\\]]').val(sum);
});
});
<script>
PHP CODE: (recitem_insert.php)
$total_rec = $_POST['total_rec'];
for($i=0;$i<sizeof($check);$i++){
for($j=0;$j<sizeof($total_rec);$j++){
$query=mysqli_query($con,"UPDATE tblstock
SET
rec_qty='$total_rec[$j]'
WHERE id = '$check[$i]'
")or die(mysqli_error($con));
}
}
As you told that the item contain 2 or more value. So you can use class instead of name.
HTML
<input type="hidden" class="last_item_rec" name="last_item_rec[]" value="23" />
<input type="text" class="item_rec" name="item_rec[]" id="txt" />
<input type="hidden" class="total_rec" name="total_rec[]" value="" />
jQuery
$(function(){
$(".item_rec").on('keyup',function(){
var one = $(this).parents('tr').find('.last_item_rec').val();
var two = $(this).parents('tr').find('.item_rec').val();
sum = parseInt(one) + parseInt(two);
console.log(sum);
$(this).parents('tr').find('.total_rec').val(sum);
});
});

PHP How to prevent $_POST from HTML form with given id of the section

I have this piece of code:
<form action = "save.php" method = "post" onsubmit = "return submitForm()">
<tbody id = "foo">
<input type="text" name="inputField" id="inputField" value = "<?php echo $someValue ?>">
</tbody>
<input type="submit" value="Save configuration" />
</form>
My question is: How to prevent submitting variable $someValue to save.php page?
I tried with JS:
document.getElementById("inputField").readOnly = true;
document.getElementById("inputField").disabled = true;
Which disables the field, yet does not prevent from submitting content of the input field.
Remove all name attribute from your foo section.
By example
<tbody id="foo">
<input type="text" id="thisInputWillNotBeSubmited1" value = "<?php echo $someValue ?>">
<input type="text" id="thisInputWillNotBeSubmited2" value = "<?php echo $someOtherValue ?>">
</tbody>
Or if you prefer jQuery (after your DOM is loaded)
$("#foo > input").each(function() {
$(this).removeAttr( "name" );
});
$("form").submit(function() {
$("input[name='inputField']").remove();
});

Parse jQuery variable to PHP form to PSP shopping card

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);

How to send the value in the slider made by javascript with a php

I use the jquery code made a slider in my website and i want to sent the value I get from the slider to my database with php. The jquery code sites address is right here. http://jqueryui.com/slider/#steps
My html form code:
<form action="like.php" method="post" id = "button">
<input type="hidden" name="likefemale" value = "<?php echo $rand_num_rows;?>"/>
<input type="hidden" name="likemale" value = "<?php echo $rand_num_rows1;?>"/>
<input type = "hidden" name = "slider_val" id = "slider_val"/>
<input name="like" type="submit" value="like" />
</form>
<form action="unlike.php" method="post" id = "button1">
<input type="hidden" name="unlikefemale" value = "<?php echo $rand_num_rows; ?>"/>
<input type="hidden" name="unlikemale" value = "<?php echo $rand_num_rows1; ?>"/>
<input type = "hidden" name = "slider_val" id = "slider_val"/>
<input name="unlike" type="submit" value="unlike" />
</form>
I want the value sent by this two forms.
On your jqueryUI slider code, use slide event to get it's value. For example:
$("#yourSlider_1").slider({
[...]
slide: function( event, ui ) {
$( "#slider_val_1" ).val( ui.value );
}
});
Also, use a different ID for the hidden inputs "slider_val" of your forms.

Display message including the session name

Is there a way, when clicking on a button, using HTML and PHP, to display an alert message containing the name of the session started, a radio button checked and the date and time choose? The button is included in a form, so I am looking if there is way to get these things done...
Here's my code:
<?php
if (!isset($_SESSION)) {
session_start();
}
echo $_SESSION["uname"]; ?>
</div>
<br>
<form id="form1" action="welcome.php" method="post" >
Blood Type Needed :
<br>
<br>
<input id="input1" type="Radio" value="A+" name="bn"/> A
<input id="input1" type="Radio" value="B+" name="bn"/> B
<input type="hidden" id="sessionId" value="<?php echo $_SESSION['uname']; ?>" />
$('input[#name="bn"]:checked').val();
var radios = document.getElementsByName('bn');
if (radios.checked) {
function showSession(){
var x=document.getElementById("sessionId");
var y=new Date();
alert(x.value+" wants "+radios.value+y);
}
}
</script>
<input id="done" class="button1" type="button" onClick="showSession();" value=" DONE ">
</form>
I will use a INPUT hidden and a javascript function, after attack it to onClick event on your button, but suggest you use Jquery to make all cross-browser:
HTML input
<input type="hidden" id="sessionId" value="<?php echo $_SESSION['uname']; ?>" />
JAVASCRIPT
function showSession(){
var x=document.getElementById("sessionId");
alert(x.value);
}
HTML BUTTON
<input type="button" value="Click me" onClick="showSession();" />
FULL SCRIPT
<?php
if (!isset($_SESSION)) session_start();
echo "Session ID: ".$_SESSION["uname"];
?>
</div><br/>
<script>
function showData(){
var sessionId = document.getElementById("sessionId").value;
var today = new Date();
var x = document.getElementsByName('bn')
var selected = false;
// Check the selected value
for(var k=0;k<x.length;k++)
if(x[k].checked){
alert(' '+ sessionId + " wants " + x[k].value + " " +today);
selected = true;
}
if (!selected) alert('Select something please...');
}
</script>
<form id="form1" action="welcome.php" method="post" ><br/>
Blood Type Needed :<br/><br/>
<input id="AP" type="Radio" value="A+" name="bn"/><label for="AP"> A</label><br/>
<input id="BP" type="Radio" value="B+" name="bn"/><label for="BP"> B</label><br/><br/>
<input id="done" type="button" onClick="showData();" value=" DONE " class="button1">
<input type="hidden" id="sessionId" value="<?php echo $_SESSION['uname']; ?>" />
</form>
You can do this by using simple Javascript
<script>
function showValue()
{
var sessionVal=<?php echo $_SESSION['uname']; ?>;
var selectedDate=documnet.form1.dt.value;
var selectedRadio=document.form1.bn.value;
alert(sessionVal+" "+selectedRadio+" "+selectedDate); //what ever format you want to show.
document.form1.actiom='welcome.php';
document.form1.submit();
}
</script>
In your html add onsubmit function
<form id="form1" action="" onsubmit="return showValue();" method="post" >

Categories