SESSION variable slow to update textfield in php - javascript

I have a form, contained 3 fields, which when the submit button in submitted,
it will changes the session variable values according to the fields, in this case there are 3 variables. then i echo back the variable onto the fields.
For the first time submit, it stores the value beautifully and display in the fields correctly, the problem is that, when i submit the second time, the values are still the same. after i refresh the page, then the values in the fields are changed.
this is partially the codes i'm using now.
<?php
session_start();?>
?>
<form name="form1" id="form1" action="">
<input type="text" name="acc1" value="<?php echo $_SESSION['acc_main']" />
<input type="text" name="acc2" value="<?php echo $_SESSION['acc_id']" />
<input type="text" name="acc3" value="<?php echo $_SESSION['acc_cat']" />
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']) != '')
{
$_SESSION['acc_main'] = $_POST['acc1'];
$_SESSION['acc_id'] = $_POST['acc2'];
$_SESSION['acc_cat'] = $_POST['acc3'];
}
?>
After i refresh(F5), then the value changed. i want it to be, when i clicked the submit button, it will change to the new value.

PHP Code:
<?php
if(isset($_POST['submit']) != '')
{
$_SESSION['acc_main'] = $_POST['acc1'];
$_SESSION['acc_id'] = $_POST['acc2'];
$_SESSION['acc_cat'] = $_POST['acc3'];
echo '<script type="text/javascript">'
, 'jsfunctionToPrintUpdatedValues();'
, '</script>'
;
}
?>
Javascript Sample Code
function jsfunctionToPrintUpdatedValues()
{
/* retrieve the updated session variables in javascript variables */
var acc_main_js = <?php echo $_SESSION['acc_main']?>
var acc_id_js = <?php echo $_SESSION['acc_id']?>
var acc_cat_js = <?php echo $_SESSION['acc_cat']?>
document.getElementById("main").value=acc_main_js;
document.getElementById("id").value=acc_main_js;
document.getElementById("cat").value=acc_main_js;
}

in the input fields you have to write like this
`
Below
`
<input type="text" name="acc1" value="<?php if(isset($_SESSION['acc_main'])) echo $_SESSION['acc_main']" />

Use if(isset($_POST['submit']) != '') before the form. Change your code to this:
<?php
session_start();
if(isset($_POST['submit']) != '')
{
$_SESSION['acc_main'] = $_POST['acc1'];
$_SESSION['acc_id'] = $_POST['acc2'];
$_SESSION['acc_cat'] = $_POST['acc3'];
?>
<form name="form1" id="form1" action="">
<input type="text" name="acc1" value="<?php echo $_SESSION['acc_main']" />
<input type="text" name="acc2" value="<?php echo $_SESSION['acc_id']" />
<input type="text" name="acc3" value="<?php echo $_SESSION['acc_cat']" />
<input type="submit" name="submit">
</form>
<?php
}else{
?>
<form name="form1" id="form1" action="">
<input type="text" name="acc1" value="<?php echo $_SESSION['acc_main']" />
<input type="text" name="acc2" value="<?php echo $_SESSION['acc_id']" />
<input type="text" name="acc3" value="<?php echo $_SESSION['acc_cat']" />
<input type="submit" name="submit">
<?php } ?>

Related

can we send the id to the file itself with submit form

I tried to post id to file itself in php. But it post nothing. This code I tried
//if can it should echo 1 and point 232
<?php
if ($_POST['submit'] == 'winners') {
$a = $_GET['A'];
echo $a;
$point = $_GET['POIN'];
echo $point;
}
?>
<form enctype="multipart/form-data" method="post" style="width:auto;">
<div class="box">
<span class='odometer' id="timespan" name="timespan">232</span>
</div>
<input class="process" name="submit" type="submit" id="submit" value="winners" onclick="location.href='reward-pollingx.php?A=1&POIN='+document.getElementById('timespan').innerHTML'">
</form>
You want to GET value inside span in PHP using JavaScript. Try something like this:
<?php
$a = isset( $_GET['A'] ) ? $_GET['A'] : '';
echo $a;
$point = isset( $_GET['POIN'] ) ? $_GET['POIN'] : '';
echo $point;
?>
<form enctype="multipart/form-data" method="post" style="width:auto;">
<div class="box">
<span class='odometer' id="timespan" name="timespan">232</span>
</div>
<input
class="process"
name="submit"
type="submit"
id="submit"
value="winners"
onclick="window.location='reward-pollingx.php?A=1&POIN='+document.getElementById('timespan').innerHTML;return false;"
>
</form>
If you try to pass parameters like a query string you do not need to use a form.
Because you configure your form to send a post request, you can pass the parameters of the query string to input values of hidden type. Test as follows
<form action="reward-pollingx.php" enctype="multipart/form-data" method="post" style="width:auto;">
<input type="hidden" name="A" value="1">
<input type="hidden" name="POIN" value="">
<input type="hidden" name="submit" value="winners">
<div class="box">
<span class='odometer' id="timespan" name="timespan">232</span>
</div>
<input class="process" name="submit" type="submit" id="submit" value="winners">
</form>
<script>
document.querySelector('input[name=POIN]').value = document.getElementById('timespan').innerHTML;
</script>
Your php code should change to use $_POST instead of $_GET
<?php
if ($_POST['submit'] === 'winners') {
$a = $_POST['A'];
echo $a;
$point = $_POST['POIN'];
echo $point;
}
Or use query string to pass parameters to the PHP script
Submit
And the php
if ($_GET['submit'] === 'winners') {
$a = $_GET['A'];
echo $a;
$point = $_GET['POIN'];
echo $point;
}
To build the query string because you need to get information from the view you probably need to build it dynamically
If you need to submit the form to itself, you can use
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" ....
or you can use:
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST" ....

Javascript - change input value on click not working

Here's a div which represents cart item qty field:
<div class="cart_quantity_div">
<form action="cart.php" method="post">
<button class="cart_qty_controls" onclick="minusOne(qty_field_<?php echo $item_id; ?>)">-</button>
<input type="text" name="cart_item_qty" value="<?php echo $each_item['quantity']; ?>" size="1" maxlength="3" id="qty_field_<?php echo $item_id; ?>"/>
<input type="submit" name="qty_adjust_cart<?php echo $item_id; ?>" value="change" class="cart_qty_adjust_btn"/>
<input type="hidden" name="cart_item_to_adjust_qty" value="<?php echo $item_id; ?>"/>
</form>
</div>
It is a draft version so don't pay attention to submit and hidden inputs
And JS code:
function minusOne (input_id){
var subtracted_qty = document.getElementById(qty_field_id).value = document.getElementById(qty_field_id).value - 1;
document.getElementById(qty_field_id).value = subtracted_qty;
}
I want to change value of input text field by clicking minus button by -1. But for some reason it's not working.
Can you, please, review my code and find whats wrong with it. Or maybe there is some better ways to do such qty change..
Your code does not set the variable qty_field_id, nor does it use the variable input_id. If input_id is referring to the same value as PHP’s $item_id, then try adding the following line to the top of the JavaScript function:
var qty_field_id = "qty_field_" + input_id;
You shouldn't use inline javascript and you aren't referencing the correct element. Try this instead:
HTML:
<div class="cart_quantity_div">
<form action="cart.php" method="post">
<button class="cart_qty_controls" data-productId="<?php echo $item_id; ?>">-</button>
<input type="text" name="cart_item_qty" value="<?php echo $each_item['quantity']; ?>" size="1" maxlength="3" id="qty_field_<?php echo $item_id; ?>"/>
<input type="submit" name="qty_adjust_cart<?php echo $item_id; ?>" value="change" class="cart_qty_adjust_btn"/>
<input type="hidden" name="cart_item_to_adjust_qty" value="<?php echo $item_id; ?>"/>
</form>
</div>
JS:
[].slice.call(document.getElementsByClassName('cart_qty_controls')).forEach(function(el){
el.addEventListener('click', function(e) {
var id = e.target.getAttribute('data-productId');
document.getElementById('qty_field_' + id).value -= 1;
})
})

one form two actions with javascript

Trying to first populate fields with one js button and then submit that collect data with a second js button
I can get the fields to poulate, when I click the "Populate" button, but when I try to do the submit none of the values show on the ordertest.php
Thanks
<form action="#" name="data" id="data">
<input type='button' value='Populate' onclick='popContact()' /><BR>
<input type="submit" onclick="document.getElementById('data').action = 'ordertest.php'" value="Payment Submit" /><BR>
Contact Info:<BR>
First Name: <input type='text' readonly="readonly" name='cfname' /><BR>
Last Name: <input type='text' readonly="readonly" name='clname' /><BR>
Email:<input type='text' readonly="readonly" name='cemail' /><BR>
</form>
<script type="text/javascript">
function popContact()
{
var c = window.external.Contact;
data.cfname.value = c.FirstName;
data.clname.value = c.LastName;
data.cemail.value = c.EmailAddr;
}
</script>
---> ordertest.php
<?php
$current_firstname = $_POST["cfname"];
$current_lastname = $_POST["clname"];
$current_email_address = $_POST["cemail"];
echo "current_firstname ".$current_firstname;
echo "<br>";
echo "current_lastname ".$current_lastname;
echo "<br>";
echo "current_email_address ".$current_email_address;
?>
also tried
<input type="submit" onclick="this.form.action='ordertest.php'" value="Payment Submit" />
Oppps.
using this combination works, $_GET
<input type="submit" onclick="this.form.action='order.php'" method="POST" value="Payment Submit" />
---> ordertest.php
<?php
$current_firstname = $_GET["cfname"];
$current_lastname = $_GET["clname"];
$current_email_address = $_GET["cemail"];
echo "current_firstname ".$current_firstname;
echo "<br>";
echo "current_lastname ".$current_lastname;
echo "<br>";
echo "current_email_address ".$current_email_address;
?>

js submit not works not sending post form data to php

After certain time (2min) when i try form submit using javascript it just submit form without post data value !!! searched solution on web but unable to find solution!! Hope someone can help me
Code:
<script type="text/javascript">
$(function() {
$('.countdown.callback').countdown({
date: +(new Date) + 120000,
render: function(data) {
$(this.el).text( this.leadingZeros(data.min, 2) + "min " + this.leadingZeros(data.sec, 2) + " sec");
if(this.leadingZeros(data.min, 2) < 01 && this.leadingZeros(data.sec, 2) <= 10){
$(this.el).addClass('ended');
}
},
onEnd: function() {
document.getElementById('myform').submit();
}
});
});
</script>
html code with php
<?php
foreach($row as $r){
if(!empty($r['question'])){
?>
<form method="post" id="myform" action="subanswer.php" >
<p><span style="color:rgb(33,79,157);"><?php echo $r['id'];?> <?php echo $r['question'];?></span><br />
<input type="radio" name="<?php echo $r['test_id'].'_'.$r['id'];?>" value="<?php echo $r['ans1'];?>"> <?php echo $r['ans1'];?>
<br>
<input type="radio" name="<?php echo $r['test_id'].'_'.$r['id'];?>" value="<?php echo $r['ans2'];?>"> <?php echo $r['ans2'];?>
<br>
<input type="radio" name="<?php echo $r['test_id'].'_'.$r['id'];?>" value="<?php echo $r['ans3'];?>"> <?php echo $r['ans3'];?>
<input type="hidden" name="testname" value="<?php echo $_GET['id']?>">
</p>
<?php
}
}
?>
<input type="submit" name="subans" value="add" class="but" style="width:15%;" />
</form>
From your code, it looks like you're calling the wrong form:
document.getElementById('myForm').submit();
The ID myFom does not exist. You have:
<form method="post" id="myform1" action="subanswer.php" >
Also not sure why people don't stick to JQuery throughout. Would advise:
$("#myForm1").submit();

HTML fields are shown empty when they contain value

I have an edit user form. When a user visits this page, details regarding to him are shown in the fields. He can edit the fields if he want to, and then submit the form.
<form id="edit-form"method="post" action="<?php echo $_SERVER['PHP_SELF'] ; ?>">
<input id="txtalias" name="txtalias" type="text" value="<?php echo 1; ?>" >
<input type="text" id="txthour_max" name="txthour_max" value="<?php echo 2; ?>" >chk1
<input type="text" id="txtminute_max" name="txtminute_max" value="<?php echo 3; ?>" >chk2
<input type="text" id="txthour_def" name="txthour_def" value="<?php echo 4; ?>" >chk3
<input type="text" id="txtminute_def" name="txtminute_def" value="<?php echo 5; ?>">chk4
<button id="serv_butn" type="submit">Save settings</button>
</form>
This is the PHP part:
if((isset($_POST['txtalias']))&&(isset($_POST['txthour_max']))&&(isset($_POST['txtminute_max']))&&(isset($_POST['txthour_def']))&&(isset($_POST['txtminute_def'])))
{
$z = $_POST['txtalias'];
$y= $_POST['txthour_max'];
$w=$_POST['txtminute_max'];
$x = $_POST['txthour_def'];
$u = $_POST['txtminute_def'];
}
And if the user, doesn't want to make any changes & he clicks the submit button,
Notice: Undefined variable
is shown, even though the text fields have values stored in them.
How to solve this issue?
<input id="txtalias" name="txtalias" type="text" value="<?php echo $env; ?>" >
If this is a textbox, and when i click submit button, following notice is shown
Notice: Undefined variable: env in C:\wamp\www\project\mypage.php on line 212 Call Stack #TimeMemoryFunctionLocation 10.0010163472{main}( )..\mypage.php:0 ">
your
<input id="txtalias" name="txtalias" type="text" value="<?php echo $env; ?>">
$env has not define;
please check when you declare $env

Categories

Resources