php form post values not working properly [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
If i do the normal calculation means its working, but if i pass the value from form means its not working kindly please help me.
For example:
echo $a=(1.5 * 10E-8) - (4.6 * 10E-8);
Result : -3.1E-7 but its not working when I get the values from form.
For example:
echo $m3=($_REQUEST['m3']); echo "<br>"; \* m3 value getting from form */
echo $m2=($_REQUEST['m2']); echo "<br>"; \* m2 value getting from form */
echo $b=$m3-$m2;
Result : -3.1
I need the result fully with that scientific notation.

You need to cast the form's post values into float
echo $m3=(float)($_REQUEST['m3']); echo "<br>"; \* m3 value getting from form */
echo $m2=(float)($_REQUEST['m2']); echo "<br>"; \* m2 value getting from form */
echo $b=$m3-$m2;

Form values return strings or arrays of strings. You need to cast them into an numeric type, if you would like to do calculations with them..
For example:
$m3 = intval($_REQUEST["m3"]);

Form working properly. You just need to always echo your result in scientific notation and remove * from form inputs:
$a = '1.510E-8';
$b = '4.610E-8';
echo sprintf('%e', $a-$b);
//result: -3.100000e-8

Related

How to name an array after content from a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I've a form tag which sends the name for an array to a function but how can I name my array after this? Ok, code says more than thousand words:
<?php
function dynamic($id, $name, ... ){
...
echo "<select id='$id' name='$name' size='...' multiple>";
...
echo "<select id='$id' name='$name'>";
...
}
?>
<form ... method="post">
<p>
<?php
echo dynamic("dynamic1", "choice1", ...);
?>
</p>
<p>
<?php
echo dynamic("dynamic2", "choice2", ...);
?>
</p>
<p>
<?php
echo dynamic("dynamic3", "choice3", ...);
?>
</p>
<input type="submit" value="send"/>
</form>
I want to create a list where you can select multiple items but for this $name needs ot be an array. The array should named like the second variable. In one case it should be named choice1 in another choice2
Like how do I get from $name = "choice1"; to choice1[]
#edit Added a new line in function to show my problem. somtimes $name needs to be and array and sometimes not
Any ideas?
You are looking to use dynamic variable names, which is possible in PHP, but you need to be careful with this. Production code using this can be difficult to maintain and throw errors quite easily.
Anyway, lets say you have a value in the form $_POST that you want to use as a variable name. You would do so like this.
$id = "gettheidsomewhere";
${$id}[] = "whatever";
Like i said, use this carefully. Dynamic variable names are dangerous and very hard to debug when things break.
If you do not know the value used for $id, then you will need to loop through your post variables and assign them accordingly. I would assume you want to add some extra logic, but here is a basic example.
Using a key value loop you can obtain the name of the post variable, stored as $key and the value. So for $_POST["something"] = "test", when this line is looped over, $key will be "something" and $value will be "test".
foreach ($_POST as $key => $value)
{
${$key}[] = $value;
}

Display the names of selected checkboxes and the values [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to display the name of all the selected check boxes and their values. At the moment it shows the values of all the selected check boxes but not the name of the selected check box. How would I do this?
The sections of code are below. The HTML form section has 9 identical checkboxes apart from the name and value.
<?php
echo $_POST['jan'];
echo $_POST['feb'];
echo $_POST['mar'];
echo $_POST['apr'];
echo $_POST['may'];
echo $_POST['jun'];
echo $_POST['jul'];
echo $_POST['aug'];
echo $_POST['sep'];
?>
<div class="checkbox">
<label>$15 :
<input name="jan" value="15" type="checkbox"> January
</label>
</div>
Any help would be great.
Checkbox parameters are only set if the box is checked. So test whether it's set:
if (isset($_POST['jan'])) {
echo 'jan: ' . $_POST['jan'];
}
You can put this into a loop:
$months = array('jan', 'feb', ...);
foreach ($months as $m) {
if (isset($_POST[$m])) {
echo $m . ': ' . $_POST[$m];
}
}

Creating HTML Forms from PHP Arrays [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to make a form in html that would create radio options bases upon the specified array in PHP.
Code:
<form name="form" action="Test.php" method="get">
<?php
//Creates the Array
$radioButtonArray = array("cat", "dog", "sheep", "moose");
//Length of the Array
$count = count($radioButtonArray);
//Runs for each index.
for($x = 0; $x < $count; $x++)
//Creates a radio button with the specified length
echo "<input=\"radio\" name=\"Animal\" value=\"{$radioButtonArray[$x]}\">{$radioButtonArray[$x]} <br>";
?>
</form>
As you can see firstly I open the form tag inside of HTML. Then I create a array with animals names and run a loop through each array index. During each loop it should create a new radio button and then make a new line as directed by the echo.
The issue is that when I run the file the output should be for example:
(RADIO BUTTON HERE) cat
(RADIO BUTTON HERE) dog
(RADIO BUTTON HERE) sheep
(RADIO BUTTON HERE) moose
Instead I get:
cat
dog
sheep
moose
I know that it is reading the echo line so the error would have to be located on that line. I am very new to PHP and decently familiar with HTML so a simple but detailed explanation of what I did wrong or what I should do would be very greatly appreciated. Thank you in advance.
How to Fix:
I did not correctly enter the format for declaring a input.
//Change This
echo "<input=\"radio\" name=\"Animal\" value=\"{$radioButtonArray[$x]}\">{$radioButtonArray[$x]}<br>";
//To This
echo "<input type=\"radio\" name=\"Animal\" value=\"{$radioButtonArray[$x]}\">{$radioButtonArray[$x]}<br>";
There is small error on the echo statement. HTML radio button should read
but your output statement reads instead.
Hence you should change
echo "<input=\"radio\" name=\"Animal\" value=\"{$radioButtonArray[$x]}\">{$radioButtonArray[$x]} <br>";
To
echo "<input type=\"radio\" name=\"Animal\" value=\"{$radioButtonArray[$x]}\">{$radioButtonArray[$x]} <br>";
Try this:
echo "<input type=\"radio\" name=\"Animal\" value=\"{$radioButtonArray[$x]}\">{$radioButtonArray[$x]} <br>";
You are not specifying the input type.
input=\"radio\" should be input type=\"radio\"
It should be input type="radio" not input="radio"

Php variable not showing up? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi I'm trying to change the picture for facebook thumbnail by having php set the image in the meta tag depending on the page. I have this code but for some reason when I go to Facebook and debug it it actually shows the variable and not what the value of the variable is. Here is my code please help thank you!
<?php
if($ifpage == 'picture.php')
{
$metaimage = '<meta property="og:image" content="http://www.quickdailylaugh.com/photos/print $picture" />';
print($metaimage);
}
?>
Your variable is inside of a string enclosed with single quotes. Single quotes will take the literal value of the string $varname and not translate the variable to it's value. You need to use double quotes. Example:
$var1 = 'test';
$foo = 'The value of var1 is: $var1'; // The value of var1 is: $var1
$bar = "The value of var1 is: $var1"; // The value of var1 is: test
To interpret the varible you must use double quotes.
<?php
if($ifpage == 'picture.php')
{
$metaimage = "<meta property=\"og:image\" content=\"http://www.quickdailylaugh.com/photos/print $picture\" />";
print($metaimage);
}
?>
I had to do another query for it to show... Thank you all for the answers here is what i used...
<?php
if($ifpage == 'picture.php')
{
$photoid = $_GET['picture'];
$sql = "SELECT * FROM photos WHERE id=$photoid";
$pic = mysql_query($sql);
while($row = mysql_fetch_array($pic))
{
$picture=$row['picture'];
}
$metaimage = "<meta property=\"og:image\" content=\"http://www.quickdailylaugh.com/photos/$picture\" />";
print($metaimage);
}else{
?>

PHP script in FORM HTML <input> [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
<form id="submit-form" method="post" action="" enctype="multipart/form-data">
<label>dateField1</label>
<?php
print "<b>calendar:</b><br/>";
$dateField1 = new dateField($format,"date1",$img);
$dateField1->setTitles($arr_daysOfTheWeek,$arr_months,$format_title);
$dateField1->setCssClasses($arr_cssClasses);
print "value:" . $dateField1->makeDateField();
?>
<input type="text" class="span3" name="dateField1" tabindex="2" value="php code" />
</form>
I want to make that value in php script ... be related to the value of the input form html
I tried so many ways ,but not implemented , please help
print "... value="'.$dateField1'" ... " />
Recheck this part, especially the quotes and the escaping
print "... ";
/* escaping a variable, this is actually not needed when using double quotes */
print "..." . $abc . "...";
print "... $abc ... ";
print '...' . $abc . '...';
/* escaping a var, but enclosing it in single quotes in the result code */
print "... abc='" . $def . "' ...";
For as far as I can see and derive from your comments below, you are using some kind of dateField object. This object contains some data and you're trying to use its value.
The data you enter in the value-tag of the input-field must be a String. But you are trying to put an Object in there. You must extract the value-data from the dateField-Object
This is a complete guess. But I have no info from you to work on...
I assume you classes/dateField.class.php looks something like this:
class dateField {
function __contruct($format,$name,$img) {
// ...
}
function setTitles($daysOfTheWeek,$months,$format_title) {
// ...
}
function setCssClasses($cssClasses) {
// ...
}
function makeDateField() {
// ...
}
}
Now you need to fill in these blanks:
What does makeDateField() do?
Does it return the HTML-code for a Date Field?
Does it return a value (single value or array)?
Is there some method that returns the value?
E.g. function getDate() or something similar?

Categories