PHP script in FORM HTML <input> [closed] - php

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?

Related

PHP table creation with name and score [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am having some serious trouble for some reason with creating a basic PhpMyAdmin database and making a page with a scoreboard. There will be a game that you play, and when you're done you can input a score and your name using basic form elements. When you press submit the page will reload and you will be able to see the top ten scores, ranked by highest first.
My issue is that I have no idea where to start with this. I have just started Php and don't wish for anything crazy. I have the ~/php/db_connect.php set up correctly already; I just need to make the function work.
How do you recommend I go through with this? Example code is extremely helpful.
I know the first response is "what have you tried?" and I haven't tried much.
This is what I have right now:
// define variables and set to $name = $myArray[0];
$babyinfo = fgets($myfile);
$myfile = scoreboard-dk;
$myArray = explode(',', );
$score = $myArray[1];
$name = $myArray[2];
$insertStmt = "INSERT INTO scoreboard-dk ('score','name') VALUES ('$score','$name')";
// Inserting Babynames into database
$db->query($insertStmt);
?>
<form action=" $db;?>" method="post">
Name: <input type="text" name="name" value=" echo $name;?>" required><br>
Score: <input type="text" name="score-dk" value=" echo $score;?>" required><br>
<input class="btn btn-primary" type="submit">
</form>
<tr> <th scope=row> echo $i;?></th> <td> echo $score;?></td> <td> echo $name;?></td> <td> echo $votes;?></td> </tr>
Thanks in advance.
Ok. First some mistakes you made:
$myfile = scoreboard-dk; isn't working. This way it would be a constant. You need the "$" or quotation marks if it should be a string.
$myArray = explode(',', ); I don't know what you want to do? The second argument is missing. This statement won't work. Second argument has to be a string.
You have to properly escape the query before executing the statement.
You can do this by replacing the following line before building the string:
$score = $db->real_escape_string($myArray[1]);
$name = $db->real_escape_string($myArray[2]);
Furthermore are you sure you use the correct indices for the array access? Counting starts with 0, not with 1.
You can't use PHP code without the opening tags. I thought that you cut that away at the start of the file. You always have to open PHP code blocks with
Perhaps you should search for example code elsewhere. I think stack is more for specific questions. But the code actually shows that you lack of some basic knowledge ... no offense.
The reason people can't help you is that your question is way too broad and everyone will have a different approach about how to implement it.
That being said, here is the pseudo code I would use to implement this. It can be done in a single file. Good luck!
File: score_keeper.php
<?php
error_msg = array
if (form submitted)
$name = name from form
$score = score from form
// Do validation to ensure name and score is as expected.
if name is empty
error_msg[] = 'Name cannot be empty'
if score is not numeric
error_msg[] = 'Score must be numeric'
if empty(error_msg)
// INSERT
// Make sure you use parameterized queries
SQL = INSERT into table (name, score) VALUE (?, ?)
end-if
end-if
// READ top 10
SQL = SELECT name, score FROM table WHERE ...
if !empty(error_msg)
show error_msg
?>
<form method="post">
<input name="name">
<input name="score">
</form>
HTML table
<?php
// output top 10 results

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;
}

Converting post data to string [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 6 years ago.
Improve this question
I'm writing a php program that will convert user input to a string and count the times each character is used, eventually for each word. Does anyone know how to convert the post data to a string? I was looking at implode and count_chars, but implode isn't converting to string like expected. I'm not sure how to show the error that it is encountering to give more information. I'm running it and writing it in phpFiddle. I'm not sure how to run it elsewhere. Please give info on what could be wrong with implode, how to show errors in phpFiddle, or run this in a browser without phpFiddle.
<?php
echo $_POST['value'];
$post_string = implode($_POST);
foreach (count_chars($post_string, 1) as $i => $val) {
echo "there were $val instances of \"", chr($i) , "\" in the string. \n";
}
?>
<form method="post" action="">
<input type="text" name="value">
<input type="submit">
</form>
This might be what you are looking for....
implode("", $_POST)
Imploding PHP Arrays usually requires two arguments, delimiter, and array.
To convert the $_POST array into a string, you should be able to implode it using;
implode('', $_POST);
I hope this is what you are looking for.
<?php
if (isset ($_POST['value'])) {
echo $_POST['value'] . "<br>";
}
$post_string = implode("", $_POST);
foreach (count_chars($post_string, 1) as $i => $val) {
echo "There were $val instances of \"", chr($i) , "\" in the string. <br>";
}
?>
<form method="post" action="">
<input type="text" name="value">
<input type="submit">
</form>

How to make a volunteering calculator in my html [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm currently coding a website in html and CSS. I have learned, php well at least enough for the thing i'm currenly trying to make. So I basically have the following,
How do you accomplish getting a number/constant 40 and subtracting that from a user inputted number. In a html document
<form action="welcome.php" method="post">
$string = "cool";
Hours:<input type ="text" name ="name"<br>
$string = "cool"
Hours: <input type="text" name="name"><br>
<input type="submit">
echo 40-"$cool";
</form>
this is wrong and will return the echo and original php code. If i wrap it in php it will display an error
The $cool should not be quoted. Also, why are you setting the $string two times with same value?
<form action="welcome.php" method="post">
<?php $string = "cool"; ?>
Hours:<input type ="text" name ="name"><br>
Hours: <input type="text" name="name"><br>
<input type="submit">
<?php echo 40-$cool; ?>
</form>
Also, the file itself has to be php, not html.
Firstly, you're injecting PHP inside HTML, you can't do that. It will produce a parse error, if your file is indeed a .php extension.
Edit: As noted in comments, you can inject PHP inside HTML, just as long as you include PHP tags and enclosed using <?php ?> or <?= ?>; the latter being short open tag syntax.
Now, as I understand it, you want another field where a user will enter a number, then substract (from user input) your number 40 being a constant, and add on the word "cool" after the result.
Here's how and with the user's name echo'd also if filled and checking if it's a number using is_numeric().
Sidenote: Using the number 2 in the input and with the name John, will output "38 cool John".
<?php
if(isset($_POST['submit'])){
if(isset($_POST['number']) && is_numeric($_POST['number'])){
$string = "cool";
$name = $_POST['name'];
$number = $_POST['number'];
$constant = 40;
$total = $constant - $number;
echo "$total $string $name";
} // brace for if(isset($_POST['number'])...
else{
echo "It's not a number";
}
} // brace for if(isset($_POST['submit']))
?>
<form action="" method="post">
Name:<input type ="text" name ="name"<br>
Number: <input type="text" name="number"><br>
<input type="submit" name="submit" value="Submit">
</form>
Footnotes:
You can also use an alternate method to echo the variables, such as:
echo "$total $string $name"; with a space between
echo "$total-$string-$name"; with a hyphen seperator
as noted in comments
The dot concatenation is just force of habit on my part.
Sidenote: You must use double quotes for this, since variables only get parsed inside double quotes.
Otherwise, you would get the following parse error using echo $total $string $name;:
Parse error: syntax error, unexpected '$string' (T_VARIABLE), expecting ',' or ';'...
Using echo $total-$string-$name; would work, but it will only echo the value from the substraction, instead of the intended echo'd string(s).
You don't need PHP for this at all. You can just use some basic java script. To use php, you need to set this whole page up differently.

php form post values not working properly [closed]

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

Categories