Update form fields after posting with PHP_SELF - php

I am using php_self to submit a form. Once the data has been posted, I want to pass a calculated value to another form field on the same page, original form.
The $title_insurance field stays blank. Any ideas on why? Thanks!
<?php
if(isset($_POST['submit']))
{
$sale_price = $_POST['sale_price']; // posted value
$title_insurance = ($sale_price * 0.00575) + 200;
?>
<script type="text/javascript">
document.getElementById("title_insurance").value='<?php echo $title_insurance ; ?>';
</script>
<?php } ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input name="sale_price" type="text" id="sale_price" size="15">
<input name="title_insurance" type="text" id="title_insurance" size="15" value="<?php echo $title_insurance; ?>" />
<input name="submit" type="submit" class="bordered" id="submit" value="Calculate" />
</form>

The submit button is called button, also if you are outputting a javascript to amend the value it need to be run after the DOM has created the element title_insurance.
if(isset($_POST['button']))
{
$sale_price = $_POST['sale_price']; // posted value
$title_insurance = ($sale_price * 0.00575) + 200;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input name="sale_price" type="text" id="sale_price" size="15">
<input name="title_insurance" type="text" id="title_insurance" size="15" value="<?php echo $title_insurance; ?>" />
<input name="button" type="submit" class="bordered" id="button" value="Calculate" />
</form>
<script type="text/javascript">
document.getElementById("title_insurance").value='<?php echo $title_insurance ; ?>';
</script>
A better way in this case would be to forget about the javascript as it is unnecessary and do this
// I am assuming you have initialized $title_insurance
// somewhere above here to its default value!!!!
$title_insurance = isset($_POST['button']) ? ($_POST['sale_price'] * 0.00575) + 200 : $title_insurance;
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input name="sale_price" type="text" id="sale_price" size="15">
<input name="title_insurance" type="text" id="title_insurance" size="15" value="<?php echo $title_insurance; ?>" />
<input name="button" type="submit" class="bordered" id="button" value="Calculate" />
</form>

You have an extra space in your getElementById parameter:
// VV
document.getElementById("title_insurance ").value='<?php echo $title_insurance ; ?>';

What you want to do is best done by AJAX. The <form> construction is outdated and not useful unless you are transferring the user to another page and sending some data along with it - or, if you are finished getting user data and just want to process what was entered and display a completion message.
If you wish to continue processing on the same page, then AJAX is the way to go. And the best way to use AJAX is to have a separate processor file (PHP) that receives the data, processes it, and sends it back.
To convert a <form> construct to AJAX, you really just need to remove the <form></form> tags and convert the submit button from type="submit" to type="button" id="mybutton", and use the IDs on the button and on the other elements to grab the data they contain and feed them to the AJAX code block. The examples in the link at bottom shows what you need to know - they are simple, helpful examples.
To conserve resources, you can use the same PHP processor page for multiple AJAX requests -- just send a variable (eg. 'request=save_to_db&first_name=bob&last_name=jones', ) and test for what "request" is received, that will determine what your PHP processor file does and echoes back.
This post, and the examples it contains, will help.

try this first
In you coding you missed this $_POST['button']
and
<?php
if(isset($_POST['button']))
{
$sale_price = $_POST['sale_price']; // posted value
$title_insurance = ($sale_price * 0.00575) + 200;
?>
<script type="text/javascript">
document.getElementById("title_insurance ").value='<?php echo $title_insurance ; ?>';
</script>
<?php } ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input name="sale_price" type="text" id="sale_price" size="15">
<input name="title_insurance" type="text" id="title_insurance" size="15" value="<?php echo $title_insurance; ?>" />
<input name="button" type="submit" class="bordered" id="button" value="Calculate" />
</form>
and also refer this FIDDLE it will more helpful to you..

Related

Passing Input values from one PHP page to another within hidden fields

$sql="SELECT vName,id FROM employee WHERE vName LIKE '%$my_data%' ORDER BY vName";
$result = mysql_query($sql);
if($result)
{
while($row=mysqli_fetch_array($result))
$hid='<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />';
echo($hid);
echo $row['vName']."\n";
}
How to pass the value of a hidden input field to another PHP script? I am using auto complete. how to pass the value auto complete page to index page
You have two options:
Sessions
PHP Sessions
Session support in PHP consists of a way to preserve certain data across subsequent accesses.
eg:
<?php
// Page1.php
session_start();
$_SESSION["key"] = "random value";
Then:
<?php
// Page2.php
session_start();
echo $_SESSION["key"];
// Output would then be ... random value
POST
Using the PHP $_POST
Taking what you currently have, you'd do:
<form method="post" action="somescript.php">
<input type="hidden" name="xyz" id="abc" value="<?=$row['id'] ?>" />
<button type="submit" name="submit" value="submitForm" />
</form>
Then on somescript.php if you do:
<?php
print_r($_POST);
You'll see an array with the data from your form, hidden value included
Create a form
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
And Get value on action_page.php
$_GET['xyz']
You enter your html code inside php code like this
<?php
while($row=mysqli_fetch_array($result))
{
?>
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
<?php
echo $row['vName']."\n";
}
?>

How to call php variable input text from another Php page

My question is how do I call input value of user selected which has show by php code from another php.
Normal simple way we get the input this way
$calendar_id_val = $_GET['calendar_id'];
and now it is not working:
For example Show.php, I have one form which show the values from Database and show the result with php variable with While Loop.
<input name="calendar_id" value="<?php echo $calendar_id;?>">
and when user is submit that form I will carry these user selected value and perform insert.php
While you are doing echo in the input use echo $calendar_id_val
You should use form like,
<form action="insert.php" method="get">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>"/>
<input type="submit" name="submit_id" value="Insert"/>
</form>
Insert.php
echo $calendar_id_val = $_GET['calendar_id'];
Does this answer your question?
<form action="insert.php" method="GET">
<input name="calendar_id" value="<?php echo $calendar_id;?>">
</form>
If you want to redirect the user back to show.php, add this to the end of your insert.php script
header('Location: show.php');
exit();
I suggest $_POST var like this:
<form action="insert.php" method="POST">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>" />
<input type="submit" name="submit" value="Submit" />
</form>
insert.php file:
echo $calendar_id = $_POST['calendar_id'];

How to set two buttons on one form html&php

This form should calculate numbers and save
Now there are two buttons One is call Calculator and two call Save
If I press Calculator
I get the form action is going to file name save.php And I do not want it that way
How can I set it up that button do something else
Example
Calculator = Calculator
Save = save.php
Is it possible to set it
Because it is one form
Thanks to anyone who can help
<?php
error_reporting (0);
$NUM = $_POST["NUM"];
$NUM2 = $_POST["NUM2"];
$NUM = "$NUM";
$NUM2 = "$NUM2";
$subtotal= $NUM+$NUM2;
?>
<form action="save.php" method="POST" name="Calculator">
<p>
<input name="NUM" type="text" value="<?php echo $_POST["NUM"]; ?>" />
</p>
<p>+</p>
<p>
<input name="NUM2" type="text" value="<?php echo $_POST["NUM2"]; ?>" />
</p>
<p>
<input name="subtotal" type="text" value="<?php echo "$subtotal";?>" />
</p>
<p>
<input name="submit" type="submit" value="Calculator" />
<p>
<input name="submit" type="submit" value="Save" />
</p>
</form>
You can have all the logic in a single PHP script (no need to direct to a different script depending on the button). If the logic is complicated, use include statements in order to separate the code.
Name the buttons differently:
<input name="calculator_submit" type="submit" value="Calculator" />
<input name="save_submit" type="submit" value="Save" />
Then in PHP:
if (isset($_GET['calculator_submit'])) {
// ...
} else if (isset($_GET['save_submit'])) {
// ...
} else {
// ...
}
If you really need different PHP script, then you'll have to go with Javascript (function will change the form action when a submit is clicked).
Since you are now using two submit buttons, both will submit the form and go to save.php.
Make your "calculator" button an input type=button instead of submit, and handle it via JavaScript.
Just FYI:
HTML5 allows to define a different form target URL by specifying the formaction attribut on a submit button – but browser support is lousy as of now.
Form and Buttons
<input name="submit" type="button" onclick="submitForm('Calculator')" value="Calculator" />
<input name="submit" type="button" onclick="submitForm('Save.php')" value="Save" />
Some jquery:
function submitForm(path) {
$('#Calculator').attr('action', path);
$('#Calculator').submit();
}

Passing PHP form variables from one page to other

my php configuration on server are showing i can post variables to maximum size upto 8MB , thats enough .. but how to check for number of variables , sever is running ubuntu 4.4 , php .
i have a page which takes students marks and send them to a action page , but on action page doing echo for the post variables nothing is being displayed , where are doing an echo "hello"; this shows ...
this is the page which sends the variables
<form name="frm" action="marklistI.php" method="POST" class="" >
<?php $tb->displayTable() ?>
<div class="mainframe">
<input type="hidden" name="batch" value="<?php print $_GET['batch']; ?>"/>
<input type="hidden" name="sem" value="<?php print $_GET['sem']; ?>" />
<input type="hidden" name="chance" value="<?php print $_GET['chance']; ?>"/>
<input name="submit" type="submit" class="hide" value="Save"/>
<input type="hidden" name="url" value="<?php print $_SERVER['REQUEST_URI']; ?>"/>
</div>
</form>
and this are the variables are coming to action page .. but on echo they are not showing any value .
$dept =$_COOKIE['dept'];
$join=$_POST['batch'];
$type='e';
$sem=$_POST['sem'];
$chance=$_POST['chance'];
try placing this code on your action page:
if (isset($_GET)) {
echo "<h3>GET METHOD</h3>";
var_dump($_GET);
}
if (isset($_POST)) {
echo "<h3>POST METHOD</h3>";
var_dump($_POST);
}
if (isset($_COOKIE)) {
echo "<h3>COOKIE METHOD</h3>";
var_dump($_COOKIE);
}
See which method returns your variables and use it, otherwise, you are not filling any values on the form.
this is your code:
<form name="frm" action="marklistI.php" method="POST" class="" >
<?php $tb->displayTable(); ?>
<div class="mainframe"> <input type="hidden" name="batch" value="<?php print $_GET['batch']; ?>"/>
<input type="hidden" name="sem" value="<?php print $_GET['sem']; ?>" />
<input type="hidden" name="chance" value="<?php print $_GET['chance']; ?>"/>
<input name="submit" type="submit" class="hide" value="Save"/>
<input type="hidden" name="url" value="<?php print $_SERVER['REQUEST_URI']; ?>"/>
</div>
</form>
One possible reason for your issue:
You use "_GET[]" variables here but the form is POST.
GET and POST are two different methods to send data, GET is in the URL path (a=&b=&c=) while POST is hidden in the HTML headers.
So make sure you read those results as "$_POST['name']" and not GET.
I suggest this in the "receiving script" for debugging:
var_dump($_GET);
var_dump($_POST);
And in your browser use Chrome or Firefox + Firebug and Press "f12".
In that debugger you can catch the POST when you click the button and you can look which variables were sent.
That should help you debug your issue fast.
One other suggestion, I personally would write the code less "mixed".
It makes it hard to read and hard to modify.
Why not like this:
<?php
echo "
<form name='frm' action='marklistI.php' method='POST' class='' >".
$tb->displayTable().
"<div class='mainframe'>
<input type='hidden' name='batch' value='$_GET[batch]'/>
<input type='hidden' name='sem' value='$_GET[sem]' />
<input type='hidden' name='chance' value='$_GET[chance]'/>
<input name='submit' type='submit' class='hide' value='Save'/>
<input type='hidden' name='url' value='$_SERVER[REQUEST_URI]'/>
</div>
</form> ";
?>
My guess for your problem is that those values in the formular are actually empty, that's why you don't receive anything.

Unlimited fields in PHP?

How do I do unlimited fields in php? Here is the scenario:
At first, there are only 2 fields, lets called: first name1, last name1
What I want to do is, when I click the "add" button, it will add another 2 fields in new row, the fields label/name should be first name2, last name2. And when I click again, it will have first name3, last name3, and so on..
Can anyone give me some sample script in php? I am new to PHP.
The form should be in HTML. If somebody can give Ajax sample code, would be a big plus.
That depends on what you mean by "field." It sounds as though you're talking about a form, which wouldn't be PHP, but instead HTML. You could have a button [Add] post back to the server, which then refreshes the page with another set of form-inputs. You also do that via javascript without having to refresh the page.
Simple Javascript (jQuery) Example:
$(document).ready(function(){
$("input[value='Add']").click(function(event){
event.preventDefault();
$("p.field:last").clone().insertAfter("p.field:last");
});
});
<form method="post">
<p class="field">
<input type="text" name="firstname[]" value="" />
<input type="text" name="lastname[]" value="" />
</p>
<p>
<input type="submit" name="submit" value="Add" />
<input type="submit" name="submit" value="Done" />
</p>
</form>
Simple PHP Example:
I don't encourage you use this as-is
<?php
$count = 1;
if ($_POST["submit"] == "Add") {
$count = ($_POST["firstname"]) ? (count($_POST["firstname"]) + 1) : 1;
} else
if ($_POST["submit"] == "Done") {
print "<pre>";
print_r($_POST["firstname"]);
print_r($_POST["lastname"]);
print "</pre>";
}
?>
<form method="post">
<?php for($i = 0; $i < $count; $i++) { ?>
<p class="field">
<input type="text" name="firstname[]" value="<?php print $_POST["firstname"][$i]; ?>" />
<input type="text" name="lastname[]" value="<?php print $_POST["lastname"][$i]; ?>" />
</p>
<?php } ?>
<p>
<input type="submit" name="submit" value="Add" />
<input type="submit" name="submit" value="Done" />
</p>
</form>
There are two ways to do this, either using solely PHP or by some fancy JavaScript. I will tackle the PHP-only solution. A JavaScript solution would be much more responsive as there wouldn't be repeated round trips to the server but it would also only work for users who have JavaScript enabled, whereas a PHP solution works for everybody.
A general outline of the solution is this:
Initially $count is 1, and one row is generated.
If the user clicks Add, the form is posted back to the very same PHP file with a hidden count variable included. The script restarts from the beginning, increments $count, and displays one more row than the last time.
If the user clicks Submit, the names that have been entered are processed.
Here's some sample code. I apologize that I do not have PHP installed on the machine I'm writing this one so this is entirely untested. Hopefully there aren't too many horrendous syntax errors!
<?php
$count = isset($_POST['count']) ? $_POST['count'] : 1;
if (isset($_POST['add']))
++$count;
else if (isset($_POST['submit']))
{
header('Content-Type: text/plain');
print_r($_POST);
exit;
}
?>
<html>
<body>
<form action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']) ?>" method="post">
<input type="hidden" name="count" value="<?php echo $count ?>" />
<?php for ($i = 1; $i <= $count; ++$i) { ?>
[<?php echo $i ?>]
First: <input type="text" name="firstName<?php echo $i ?>"
value="<?php echo htmlspecialchars($_POST["firstName$i"]) ?>" />
Last: <input type="text" name="lastName<?php echo $i ?>"
value="<?php echo htmlspecialchars($_POST["lastName$i"]) ?>" />
<br />
<?php } ?>
<input type="submit" name="add" value="Add" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Oh and you want a JavaScript solution, eh? Well you've got the really nice jQuery answer already. How about a ridiculously long plain-JavaScript solution, then?
<html>
<head>
<script type="text/javascript">
// <![CDATA[
var count = 0;
function addRow() {
var table = document.getElementById("table");
var row = document.createElement("tr");
var countCell = document.createElement("td");
var countText = document.createTextNode(++count);
var firstCell = document.createElement("td");
var firstInput = document.createElement("input");
var lastCell = document.createElement("td");
var lastInput = document.createElement("input");
firstInput.type = "text";
firstInput.name = "firstName" + count;
lastInput.type = "text";
lastInput.name = "lastName" + count;
table .appendChild(row);
row .appendChild(countCell);
countCell.appendChild(countText);
row .appendChild(firstCell);
firstCell.appendChild(firstInput);
row .appendChild(lastCell);
lastCell .appendChild(lastInput);
}
// ]]>
</script>
</head>
<body>
<form action="somewhere.php" method="post">
<table id="table">
<tr>
<th>Row</th>
<th>First</th>
<th>Last</th>
</tr>
</table>
<script type="text/javascript">
addRow();
</script>
<input type="button" value="Add" onclick="addRow()" />
<input type="submit" value="Submit" />
</form>
</body>
</html>

Categories