Transfer value to another page was successful, I have read many topics on this subject, stockoverflow really helped me with this.
On the first page (eg page1.php) I have input. Normally I type a value to it.
With php I write in a session variable and after clicking submit I can transfer this value to the other side (eg page2.php) which is a simple contact page. This value is pasted into a textarea field.
My problem is that it takes only the value that I type directly to the input, which is located in page1.php. I wanted to create rather calculator, which will consist of the price according to the entered number of kilograms of product. (the kilogram is the value entered in the input).
How do I get a form field was clean, and the Order the product was all redirected correctly?
My code in page1.php :
<?php
session_start();
$_SESSION['towar1'] = $cena1;
?>
<form method="get" action="kontakt.php">
<input id="wart1" type="number" value="0" name="towar1" class="ilosc" required>
</form>
My code in page2.php(conttact page) :
<?php
session_start();
$cena1 = $_GET['towar1'];
?>
<label for="wiadomosc"></label>
<textarea placeholder="" name="wiadomosc" required><?php echo $cena1 ?></textarea>
///PROBLEM
when I trying to figure how to get good value of product price (e.g. for 1 kg will be 19$)
i wrote code of this kind directly on my page2.php:
<?php echo $cena1*19 ?>
But when I do this, any direct entrance on the contact page gives me already written text which is 0 (zero).
Can I avoid that 0? How can I make textarea will be blank always, and my page1.php (calculator) will calculate correct number (const which is 19$ per kilo and number of kilos that you entered in input)
Your question is quite not clear, lets go through this: page1.php contains input that manage Item quantity and $_SESSION variable with its price, on the page 2 you need to display quantity*price_per_item number, right? If yes, try the following approach:
<?php
session_start();
$cena1 = $_GET['towar1'] * $_SESSION['towar1'];
?>
<label for="wiadomosc"></label>
<textarea placeholder="" name="wiadomosc" required><?php echo $cena1 ?></textarea>
, where $_GET['towar1'] is an items quantity and $_SESSION['towar1'] is an item price.
p.s For more adnvanced technique do not hesitate to ask in comments.
Hope it helps, cheers.
EDIT
Considering our converstation in comments I can assume that this approach would helps you:
$cena1 = (!empty($_GET['towar1']) && $_GET['towar1'] != 0 )? $_GET['towar1'] * 19 : '';
This line check if $_GET['towar1'] variable is not empty and not zero. If it match the condition - make calculation, else - return an empty string (not zero).
Please note that everything we're talking about above is not secure and can be abused by end-user. Please consider to review input validation aproach.
The reason why it doesn't work is that you are using sessions wrong.
The workflow of application should be like:
user enters the amount
you process it, if needed you give an error message or something else
save amount to session
redirect user to next page
calculate price
display result
Code example using your code:
Page1 should be:
<?php
session_start();
if(isset($_GET['wart1'])) {
$_SESSION['towar1'] = (int) $_GET['wart1'];
header('Location: page2.php');
}
?>
<form method="get" action="kontakt.php">
<input id="wart1" type="number" value="0" name="towar1" class="ilosc" required>
</form>
Page2:
<?php
session_start();
$cena1 = $_SESSION['wart1'] * 19;
?>
<label for="wiadomosc"></label>
<textarea placeholder="" name="wiadomosc" required><?php echo $cena1 ?></textarea>
Related
I created a form by RSForm. I have two textbox in it.
First textbox name is km1 (new_km) and second textbox name is km2 (old_km).
In the first time, user will fill km1 field (new_km) by her car kilometer number.
When the user recourse again and fill the form, km2 (old_km) must shown value that user entered it in previous recourse.
Please guide me?
Best regards.
Right, I know, my English is very bad. sorry.
Like this image: http://persianupload.com/kleeja/do.php?imgf=141839395854871.jpg
I personally use session for that...
Your html:
<?php
session_start();
if(!isset($_SESSION['something'])) $_SESSION['something'] = '';
?>
<input type="text" name="something" value="<?php $_SESSION['something']?>"/>
Your receiver php:
<php
session_start();
if($_POST['something']) $_SESSION['something'] = $_POST['something'];
?>
This is my first post, so excuse me if I will not provide the information correctly.
So my problem is the following:
This is the first form:
<h1>Modificare carti</h1>
<br />
<form action="UTLcrt.php" method="post">
Cod Carte: <br /><input type="numeric" name="cod"><br>
Nume: <br /><input type="text" name="nume"><br>
Autor: <br /><input type="text" name="autor"><br>
Editura: <br /><input type="text" name="editura"><br>
Disponibilitate: <br /><input type="text" name="disp"><br>
Pret: <br /><input type="numeric" name="pret"><br>
<select name="vmod">
<option value="mod">Modificare carte</option>
<option value="str">Sterge carte</option>
<option value="src" >Cauta carte</option>
</select>
<input type="submit">
</form>
The UTLcrt.php contains the following code:
<?php
if (isset($_POST['vmod'])) {
$urls = array(
'mod' => 'modcrt.php',
'str' => 'strcrt.php',
'src' => 'srccrt.php'
);
$url = $urls[$_POST['vmod']];
header("Location: " . $url);
}
?>
And each php page does the following:
modcrt.php changes the entry in our database with the same"cod" with the info provided in the first form
strcrt.php deletes the register in our database, if the "cod" we entered in the first form finds a match
srccrt.php searches in the database if the register with the "cod" provided in the first form was found and shows a possitive message.
My problem is the following: the information I put in the first form doesn't get in the modcrt.php,strcrt.php,src.php pages... the $_Post's are empty...
How to send the information from the first page, trough the second and then to the third?
You can keep them in Session, by using
$_SESSION['info1']=$info1;
The POST values are empty because the third page isn't receiving a POST request. The order of events is this:
User requests the first page.
User POSTs a form to the second page, with values.
Second page tells the user to issue a GET request to the third page.
User requests the third page.
There are a few different ways to keep the information in the chain. You can:
Add it to the query string for the redirect
Store it in session
Store it in a database
etc.
The first one might look like this:
header("Location: " . $url . "?key=value");
Where the key/value pair is similar to those in a POST. In this case the values would be available to the third page in GET:
$_GET['key']
If you use session, the values stay server-side. So in the second page you can set the value:
$_SESSION['key'] = $value;
And then retrieve it in the third page:
$value = $_SESSION['key'];
Note that these session values will continue to live on the server until the session times out. You may want to unset them from the session once you're done with them if it starts to add confusion to other pages the user visits which also make use of these values.
Page 1
<?php
// this starts the session
session_start();
// this sets variables in the session
$_SESSION['color']='red';
$_SESSION['size']='small';
$_SESSION['shape']='round';
?>
Page 2
<?php
$color = $_SESSION['color'];
$size = $_SESSION['size'];
$shape = $_SESSION['shape'];
?>
and so on...
I am trying to multiply to fields together to obtain a total in PHP form.
<label for="190_mnth2"></label>
<div align="center">
<input name="190_mnth" type="text" id="190_mnth2" value="10" size="5" />
</div></td>
<td><div align="center">
<label for="190_rate"></label>
<input name="190_rate" type="text" id="190_rate" value="190.00" size="10" />
</div></td>
<td><div align="center">
<input name="total_190" type="text" id="total_190" value=<? echo '190_mnth2' * '190_rate' ?> size="10" />
The above is my current code but the answer is totally wrong it gives me 36100 What is wrong with my formula if anyone can assist?
First of all you cannot calculate the total like that, it's not Javascript, you need a form with a get/post request which will send a request to the server, server will process and throw the calculated value back to the user.. so wrap the fields around forms, set your method to post(preferred) and than you can write your PHP code like
<?php
if(isset($_POST['submit_button_name'])) { //Use $_GET if it's a GET request
//Save the values in variable
$mnth_190 = $_POST['190_mnth'];
$rate_190 = $_POST['190_rate'];
//Calculate here
$total = $mnth_190 * $rate_190;
/* Now you can use $total either to echo straight in your page,
or inside another input field */
}
?>
Also make sure you validate the data before the form is posted and is calculated, check whether the user input doesn't have string or any other special character.
The purpose of PHP is to generate HTML to display, not to update the HTML of the current page. You can create a POST request that submits your data for display on another page. If you want to dynamically update the total on the current page, you should use Javascript or another front end language.
<? echo '190_mnth2' * '190_rate' ?>
You're attempting to multiply two strings, which will probably be converted by PHP as 190 * 190.
In order to get this to work, you're going to have to do it in two separate steps (with PHP anyway). Because PHP is a server side language, you'll have to $_POST[''], or submit these two values as part of the query string and use $_GET[''] to calculate.
If you don't want to do it this way, then I'd suggest looking at some JavaScript to handle it instead.
I'm going to take a shot at doing something like this, as an example.
$190_mnth2 = 10;
$190_rate = 190;
$total = $190_mnth2 * $190_rate;
then using: value=<? echo '$total'; ?>
So I have some data in MySQL being shown on my PHP page inside a table. I've set it up so that each page only displays 3 results each (temporary until it goes live, then it will be more). Everything works fine and it displays those 3 results on each page just fine.
What I want to do is be able to change the amount of results on each page right from the main PHP page. I can change the amount shown on that one page, but as soon as I go to page 2, it resets to 3 results. How can I remember the number stored in the variable, and display it on every page? I tried using SESSIONS, but I couldn't get it to work. I'm still a beginner btw.
$item = $_REQUEST['item'];
if(isset($_REQUEST['item'])){
$limit=$item;
}
else{
$limit=3;
}
//A few lines down -->
<form action="<?php $_SERVER['REQUEST_URI']?>" method="post">
Items: <input type="text" name="item">
<input type="submit" name"go" value="Go">
</form>
You could add SESSION use this way:
$item = $_REQUEST['item'];
if(isset($_REQUEST['item'])){
$limit=$item;
$_SESSION['item_limit'] = $limit;
}
else{
// If we find a limit set in the session use that
if (!empty($_SESSION['item_limit']) {
$limit = $_SESSION['item_limit'];
}
else {
$limit=3;
}
}
If you don't need to remember the value the next time the user visits the page you can simply output the $limit as a value attribute for the "item" input.
Items: <input type="text" name="item" value="<?php echo $limit; ?>">
I am going back though a web-based document numbering system from few weeks ago. To sum it up, the user types in the project,class,base, and dash number (PPP-CCC-BBBB-DDD) then it is added to a mysql database. Now most doc numbers go in order according to revisions. IE: A document 1465-630-0001-000 becomes, after revision, 1465-630-0002-000.
The boss wants the system to automatically fill the input text box for the base number if it detects that the user is entering a revised doc. So if a user types in 1465 into the project field and 630 into the class field the system should autofill the base field with the next available number. In the previous example this would be 0002.
It needs to be able to search the database for the first two fields so that it can find the next available one. Is there anyway to do this using javascript or something? SO was really helpful with my last javascript question pertaining to this system.
heres an bit of my code if it helps:
` ?>
<div id='preview'></div>
<form id='item' action="submit.php?item=1" method="post">
Enter Title:<input type="text" name="title" size="20"><BR>
Choose Project Code:
<SELECT NAME="project">
<OPTION VALUE="">Project...
<?
$query = "SELECT * FROM project ORDER BY project asc";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
$num = ($row['project']);
$name = ($row['description']);
?>
<OPTION VALUE="<?=$num?>" ><? echo"{$num}" . " | " . "{$name}";?>
<?
}
?>
</SELECT><BR>
Choose Class Code:
<SELECT NAME="class">
<OPTION VALUE="">Class...
<?
$query = "SELECT * FROM class ORDER BY class asc";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
$num = ($row['class']);
$name = ($row['description']);
?>
<OPTION VALUE="<?=$num?>" ><? echo"{$num}" . " | " . "{$name}";?>
<?
}
?>
</SELECT><BR>
Assigned Base Number:<input type="text" name="base" size="20"><BR>
Enter Dash Number:<input type="text" name="dash" size="20"><BR>
Enter Comments:<input type="text" name="comment" size="40"><BR>
<input type="hidden" name="item" value="1"/> `
Just a simple html/php input form with the project and class code list generated from a database pertaining to each.
Thanks for any help-Thomas
Update:
So, you're going to need to make an AJAX call (see example in my comment below) to some PHP script that will retrieve the base value you want and then returns that to the AJAX request. Once the request gets a response, you can use that data to fill in the value the way I originally said...
On a side note, since the example I gave you is a jQuery AJAX function, you should probably check out how to use jQuery to select elements on the page, instead of using straight JS.
E.g. for getting by ID and replacing value:
$("#base").attr('value', valueFromAjaxCall);
How to change value with JS:
If you use PHP to get the base value you want to fill into the field, then you can fill the value in with:
var baseField = document.getElementsByName("base")[0];
baseField.value = <?=$baseValue?>;
The getElementsByName() call returns an array, which is why you have to index into the field you want. I would suggest giving your <input> an id so that you can use document.getElementById() instead. You would do something like:
<input type="text" id="base" size="20">
and the JS to get the input element would be:
var baseField = document.getElementById("base");
...therefore, no need to index, in case you named any fields with the same name.
**Not sure about the PHP syntax.
An ajax call on focus of the 3rd field firing back to the server the values of the first two fields?
first, you'll probably want to use jQuery since it has great support is easy to use and will feel familiar to someone used to PHP.
so include your jQuery javascript code that you can get from :
http://jquery.com/
then, assume a form that looks like:
{form}
<input type=text id='major' name='major' value=''>
{Or a select, your choice}
<input type=text id='minor' name='minor'>
{or a select again}
<input type=text id='sequence' name='sequence' onFocus='getNextSequence()'>
...
{/form}
in your head, have your javascript:
function getNextSequence(){
var major=$('#major').val();
var minor=$('#minor').val();
if(!major){
alert('Select a major version#');
$('#major').focus();
return(false);
}
if(!minor){
alert('Select a minor version#');
$('#minor').focus();
return(false);
}
$.getJSON('http://url.to.getnextNumber.php',
{major:major,minor:minor},
function(data){
if(!data.error){
$('sequence').val(data.nextSequence);
}else{
alert(data.error);
}
}
});
}
the jQuery getJSON call will make a call back to your URL with two $_POST variables, major and minor. do your query, save the result as $result=array('nextSequence'=>$x,'error'=>'false');
and convert it to JSON with echo json_encode($result);
don't include ANY headers or any other content in the output of that file, and jQuery will pull the correct value and insert it where it's supposed to bed