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.
Related
I have a piece of php code inside html tag which is supposed to change the tag's style in accordance with the contents of the URL.
There is an html login form which looks like this:
<form class="userdata" action="login.php" method="post">
<input type="text" name="email" placeholder="E-mail" <?php fillin('email'); enlight_unfilled('email');?>><br>
<input type="password" name="pwd" placeholder="Password"><br>
<button type="submit" name="login-submit">Login</button>
</form>
Here are the functions fillin and enlight_unfilled:
<?php
function fillin($key) {
if (isset($_GET[$key])) echo "value=".$_GET[$key];
else echo NULL;
}
function enlight_unfilled($key) {
if (isset($_GET['error']))
if (isset($_GET[$key]) and $_GET[$key] !== "") echo NULL;
else echo "style='border-color: red'";
else echo NULL;
}
?>
If I only apply one of the functions within the tag, they both do what they are expected to – either save the email in the field if it has been already typed in or enlighten the email field if it has been left empty. But if I apply them together, when the field is empty, php assigns the field value 'style='border-color:. I also tried to use functions like print and printf, but the result is the same:
I am a beginner at php coding and mixing it with html, so the question may appear to be dumb, but I did not manage to find any sort of a solution to this issue, so thanks for help and patience in advance!
It looks like you don't properly encase value in quotes, so it just renders the 'style='border-color:.
Let's assume that $_GET[$key] has a value of hello#hello.com. What your PHP & HTML renders is the following:
value=hello#hello.com
See the problem? There are no quotes. That's why the renderer goes forward searching for a valid value. To fix the issue you must add quotes around your $_GET[$key] in the fillin function. Something like this should do the job:
if (isset($_GET[$key])) echo "value='".$_GET[$key] . "'";
It works when ran alone because it reaches the end > and just assumes the value to be hello#hello.com
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
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>
I am currently writing some search engine, where this page is retrieving some _GET variables from a previous page. This is working as intended.
Now I am using those variables as default value in a POST form. However, for some reason, only the first word for each of them is showing up. The form code is as follows:
<form action = "insert.php" method = 'POST'>
<Place name <input type="text" name="name" size = "30" value= <?php echo $_GET['name']; ?> />
Note that when echoing $_GET['name'] anywhere else in the page, everything is fine. Multiple words show up as expected, but when I use it as a text box default value, only the first word shows up on the textbox.
At first, I thought it had something to do with the way those $_GET variables are sent in the URL so I tried this:
$fullname = array();
$fullname = explode("%20", $_GET['name']);
$aaa = implode (' ',$fullname);
...
Place name <input type="text" name="name" size = "30" value= <?php echo $aaa; ?> />
but the result is still the same. If I echo it anywhere else in the page I get the full string, but if it's inside the form only the first word shows up.
What am I missing here?
The value attribute of the input tag needs to be in quotes:
<input type="text" name="name" size = "30" value="<?php echo $_GET['name']; ?>" />"
Otherwise, if $_GET['name'] contains spaces you'll end up with something like: value=John Smith. That will be understood as value=John with an invalid Smith attribute floating around.
Also, consider sanitizing $_GET['name'] with htmlspecialchars. Consider what would happen if $_GET['name'] was "/><script>alert(0)</script><. You'd end up embedding user-controlled code on your website, resulting in a reflected XSS.
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?