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'];
?>
Related
I have been given some spaghetti code for a login page to fix.
We've got two input fields ID and Password.
Here's what I've been asked.
So in terms of a user inputting their ID, I want to add '#email.com' onto the end.
Im assume placeholder="#email.com" would work if i could align it to the right, but I also need to it to be added into the POST method. So if the user entered 'ID123' it would post 'ID123#email.com'
Here is the form:
<form action="command.php" formmethod="post">
<div class="ID">User ID:<br><input name="ID" type="text"><br></div>
<div class="Pass"> Password:<br><input name="pwd" type="password"></div>
<input id="submit" type="submit" value="Sign In">
Can anyone help? Is it even possible?
Placeholder in the input type in HTML will just give a hint in a textbox and it will not be added into the input of the user. What you need to do is catch the input type in the command.php you created and add it.
for example, in the command.php
$ID = $_POST['ID'].'#gmail.com'
When you submit the form, your POST data will contain:
$_POST['ID'] = 'test_id'; // Sample Data
$_POST['pwd'] = 'test_pwd'; // Sample Data
So, if you want to add #email.com at the end of the ID, you could simply concatenate the field:
$_POST['ID'] .= '#email.com';
Note: This is a simple solution and doesn't specify SQL injection/vulnerability prevention.
I think you want something like if anyone give ID123#email.com as input it will not change but if only give ID123 then it will be ID123#email.com,then :-
$userId=$_POST['ID'];
if(!filter_var($userId, FILTER_VALIDATE_EMAIL)) {
$userId=$userId.'#email.com';
}
echo $userId;
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>
I have a check box list which I fill it with data from my table.Here is the code:
<?php
mysql_connect("localhost","root","");
mysql_select_db("erp");
$a="Select * from magazine";
$b=mysql_query($a);
$c=mysql_fetch_array($b);
while($c=mysql_fetch_array($b))
{
print '<input type="checkbox"/>'.$c['den_mag'];
echo "</br>";
}
if(isset($_POST['den_mag']))
{
echo "aaaa";
}
?>
It's a simple query and for each data just show it with a checkbox.Now what I want is when I press a checkbox the value of that checkbox to be shown in a table.So if I have check1 with value a , check2 with value b and I check check1 the value a to be outputted to a table row.How can I achieve that? how cand I get which checkbox is checked?
A few notes:
Try to avoid using SELECT * queries. Select the fields you are going to use:
$sql= '
SELECT
id,
den_mag
FROM
magazine
';
Use better variable names. $a and $c make your code harder to follow for others, and for yourself when you come back at a later time. Use more descriptive variable names like $query_object and $row. Your code should read almost like an essay describing what you're doing.
In your form, use an array of elements. By giving the input a name like selected_magazines[], you will end up with an array in your post data, which is what you want -- multiple selections
Use the row ID as the value of the checkbox element. Your array in POST will then be a list of all the IDs that the user selected
Separate your logic from your HTML generation. The top portion of your script should take care of all logic and decisions. At the bottom, output your HTML and avoid making logical decisions. It makes for a script that is easier to follow and maintain, as well as debug.
Here is a sample script incorporating these ideas with the details you've given:
<?php
// FILE: myfile.php
mysql_connect("localhost","root","");
mysql_select_db("erp");
if(isset($_POST['selected_magazine'])) {
// $_POST['selected_magazine'] will contain selected IDs
print 'You selected: ';
print '<ul><li>'.implode($_POST['selected_magazine'], '</li><li>').'</li></ul>';
die();
}
$sql= '
SELECT
`id`,
`den_mag`
FROM
`magazine`
';
$query_object=mysql_query($sql);
$checkboxes = array();
while($row = mysql_fetch_array($query_object)) {
$checkboxes[] = '<input name="selected_magazine[]" value="'.$row['id'].'" type="checkbox" /> '.$row['den_mag'];
}
?>
<form action="myfile.php" method="post">
<?php print implode('<br>', $checkboxes); ?>
<input type="submit" value="Submit" />
</form>
<input name="test" type="checkbox" />
<?php
if(isset($_REQUEST['test'])){
// selected
}
?>
When you give input-type elements (input, textarea, select, button) a name attribute (like I did), the browser will submit the state/value of the element to the server (if the containing form has been submitted).
In case of checkboxes, you don't really need to check the value, but just that it exists. If the checkbox is not selected, it won't be set.
Also, you need to understand the client-server flow. PHP can't check for something if the client does not send it.
And finally, someone mentioned jQuery. jQuery is plain javascript with perhaps some added sugar. But the point is, you could in theory change stuff with jQuery so that it gets (or doesn't get) submitted with the request. For example, you could get jQuery to destroy the checkbox before the form is submitted (the checkbox won't be sent in this case).
Here you go :
<html>
<input name="test" value="true" type="checkbox" />
</html>
<?php
$Checkbox1 = "{$_POST['test']}";
if($Checkbox1 == 'true'){
// yes, it is checked
}
?>
Users of my website can generate a custom form. All the fields are saved in a database with a unique ID. When someone visits the form, the fields 'name' attribute is field*ID*, for example
<p>Your favorite band? <input type="text" name="field28"></p>
<p>Your Favorite color? <input type="text" name="field30"></p>
After submitting the form, I use php to validate the form, but I don't know retrieve the value of $_POST[field28] (or whatever number the field has).
<?
while($field = $query_formfields->fetch(PDO::FETCH_ASSOC))
{
$id = $field[id];
//this doesn't work!!
$user_input = $_POST[field$id];
//validation comes here
}
?>
If anybody can help me out, it's really appreciated!
Add some quotes:
$user_input = $_POST["field$id"];
I'd suggest taking advantage of PHP's array syntax for forms:
<input type="text' name="field[28]" />
You can access this in php with $_GET['field'][28]
$user_input = $_POST['field'.$id];
Remember that you are using a string for the first part of the input name, so try something like: $user_input=$_POST['field'.$id];.
Also, I would suggest calling them into an array to retrieve all data:
<?php
$user_inputs=array();
while($field=$query_formfields->fetch(PDO::FETCH_ASSOC)) {
$id=$field['id'];
$user_inputs[]=$_POST['field'.$id];
}
?>
Hi so have form with a vote and what i want to do is what ever options they tick, when they click submit it the posts all the values of the check boxes ticked to the same page allowed me to echo them
i have tried just doing this
if(isset($_POST['submitted'])) {
$list = $_POST['vote'];
echo $list;
}
but that only echos the last value selected
Thanks,
Ben
ok so i have a fix by changing the name to an array but i got a problem because i used javascript functions like this
checkAll(document.form.vote)
so what do i change it to?
When you name the inputs you can give it a name like name="checkboxes[]" and that will throw it in an array when it posts to the next page. Hope that helps!
In your form set name attribut like this:
<input type="checkbox" name="vote[]" />
Then you can:
foreach($_POST['vote'] as $vote){
echo $vote;
}