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 7 years ago.
Improve this question
Pardon if parts of this question don't make senseāI'm a newb to programming in general. Feel free to correct me!
Here's my problem: I'm making a PHP upload page that uses $_POST. There are two upload fields in the HTML section, both of which are optional. So here's my code for, let's say, example-upload.php:
<form action="" enctype="multipart/form-data" method="post">
<input type="file" id="upload1" name="upload1" accept=".jpg">
<input type="file" id="upload2" name="upload2" accept=".jpg">
<button type="submit" name="submit">Submit either, both, or none of these</button>
</form>
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['upload1'])) {
$filename_upload1 = $_FILES['upload1']['name'];
}
if (!empty($_POST['upload2'])) {
$filename_upload2 = $_FILES['upload2']['name'];
}
// Checks if there is no upload at all
if (!isset($filename_upload1) && !isset($filename_upload2)) {
echo 'You didn\'t upload anything and that\'s OK';
} else {
if (isset($filename_upload1)) {
move_uploaded_file($_FILES['upload1']['tmp_name'], 'path/to/file/' . $filename_upload1);
}
if (isset($filename_upload2)) {
move_uploaded_file($_FILES['upload2']['tmp_name'], 'path/to/file/' . $filename_upload2);
}
echo 'One or two files was successfully uploaded';
}
}
?>
Each time I run this, submitting either one or both files, I get the "You didn't upload anything and that's okay" message, leading me to believe that I'm doing something wrong with the $_FILES variable. The unusual thing is that I have a different form on a similar page, except with one upload field instead of two. That seems to work.
Any advice? Thank you!
There's no $_POST['upload1'] variable in your form.
Files are passed within $_FILES array.
So, in simplest case you can check $_FILES['upload1']['name']:
if (!empty($_FILES['upload1']['name'])) {
$filename_upload1 = $_FILES['upload1']['name'];
}
And the same check for upload2:
if (!empty($_FILES['upload2']['name'])) {
$filename_upload2 = $_FILES['upload2']['name'];
}
You have to check $_FILES and not $_POST
if (!empty($_FILES['upload1']))
{
$filename_upload1 = $_FILES['upload1']['name'];
}
Related
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 3 years ago.
Improve this question
How to print all even numbers between two input values typed by user. please write code hint only in php.
I can give you a hint. Lookup the modulo operator. With it you can do something like this:
<?php
if (($number % 2) === 1)
{
echo "$number is odd.";
}
if (($number % 2) === 0)
{
echo "$number is even." ;
}
?>
Also. Have a look at how you ask questions on stack overflow. Or they will prevent you from asking questions in the future if you don't adhere to those rules.
use this code, for user input i have created a form.
<html>
<body>
<form method="post">
<input type="number" name="num1">
<input type="number" name="num2">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
// while loop that will print number
while($num1 <= $num2){
// here is the condition to check the EVEN number
if($num1%2!==0){
echo $num1."<br>";
}
// increasing the loope counter by 1
$num1++;
}
}
?>
i hope now your problem has been solved..
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 7 years ago.
Improve this question
This is my code, I get variable from textbox but nothing appears.
This code checks if a input is number:
this is a number
or not:
this is not a number
<?php
echo'<form method="post" action="">';
echo '<input type=text name=t/>';
echo'<input type=submit name=su/>';
echo'</form>';
if(isset($_POST['su']))
{
if (ctype_digit($_POST['t'])) {
echo "This is number.\n";
} else {
echo "This is not a number.\n";
}
}
?>
All of code is in one page.
Your code is failing because you have unquoted elements/attributes.
The following require quotes for:
echo '<input type=text name=t/>';
^^^^ ^^^^^^
echo'<input type=submit name=su/>';
^^^^^^ ^^^^^^^
Modify your code to read as:
<?php
echo'<form method="post" action="">';
echo '<input type="text" name="t"/>';
echo'<input type="submit" name="su"/>';
echo'</form>';
if(isset($_POST['su']))
{
if (ctype_digit($_POST['t'])) {
echo "This is number.\n";
} else {
echo "This is not a number.\n";
}
}
?>
Certain web browsers will not accept unquoted elements/attributes and will simply be ignored, such as the current version of Firefox and will fail silently even with error reporting set to catch/display, strangely enough.
You can test for a number using the JS function isNAN. PHP processes on the server so once the page is loaded it is not available. Javascript is available though after the page loads. JS is client side and is executed on the browser. Here's a js solution:
var test = 1;
if(isNaN(test)) {
alert('String');
} else {
alert('number');
}
This would alert number.
Here's a longer write up on that JS function, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN.
If method is not defined in <form> then the default is GET. So
<form>
is the same as
<form method="GET">
If you want to use $_POST, you need to do
<form method="POST">
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
I have an issue with a PHP page which displays a "?>" symbol in the top left corner. It looks like this:
<?php
include_once("DBconnect.php");
$getuser = $_POST["RegUsername"];
$getpass = $_POST["Pass"];
$getrepass = $_POST["RePass"];
$getemail = $_POST["Email"];
if($getuser){
if($getpass){
if($getrepass){
if($getpass == $getrepass){
if($getemail){
$code = rand();
if(mysqli_query($link, "INSERT INTO users VALUES ('', '$getuser', '$getpass', '$getemail', '0', '$code')")){
echo "INFO1";
}
else{
echo "ERROR6";
}
}
else{
echo "ERROR5";
}
}
else{
echo "ERROR4";
}
}
else{
echo "ERROR3";
}
}
else{
echo "ERROR2";
}
}
else{
echo "ERROR1";
}
?>
And I use this jQuery function to display the PHP returned value in my HTML page:
$("#RegSubmit").click(function(){
$.post( $("#RegForm").attr("action"),
$("#RegForm :input").serializeArray(),
function(info){
$("#RegErrorLog").empty();
$("#RegErrorLog").html(info);
});
$("#RegForm").submit(function(){
return false;
});
});
I always get the "?>" in front of the PHP "ERROR" returned value.
How can I get rid of that? Or how can I return a value from the PHP file using a variable instead of echo
I guess there's a problem in your DBconnect.php file.
Apart from that... you should really think about validating values taken from Http POSTs in your PHP script, before using them in db queries.
Check if you are not printing that symbol on the included file "DBconnect.php".
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
I'm trying to make an incredibly basic php form that outputs some text a user puts in to a database. Looked at a couple other questions like this to make sure and from what I can tell no one else asked about this. Here's the create.php that takes input from the html.
<?php
include 'connection.php';
$firstname= $_POST('inputName');
$lastname= $_POST('inputName2');
if($_POST ['SUBMIT']) { echo "please fill out the form";
header('location: ../index.html');
} else {
mysql_query("INSERT INTO requestdata ('firstname', 'lastname')
VALUES ('$firstname', '$lastname')") or die(mysql_error());
echo "User has been added"; header ('Location: ../index.html');
}
?>
Here's the html code for the form:
<form action = "php/create.php" method = "POST">
First Name <input type ="text" name='inputName' value=""/>
Last Name <input type="text" name='inputName2' value=""/>
<br />
<input type="submit" name = "button">
</form>`
and the error I'm getting is:
"Fatal error: Function name must be a string in
mywebsite.com/php/create.php on line 8
I think your $_POST variables are wrong.
Please try $_POST['inputName'] instead of $_POST('inputName') and $_POST['inputName2'] instead of $_POST('inputName2')
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 have a special form I have been making that uses some cusotm post types in wordpress. At one point I need to echo a variable $i into an if statement.
There is some validation stuff at the top that will look like this and the code in the loop is below. Pretty much I have been trying to get the majorCause1Error to be majorCause $i Error if you know what I mean, so all up it will be like 1-13
Edit: Sorry If it is hard to see what I am asking, I am finding it really hard to word my problem.
So there is a loop running around the li tags and it echos $i into the name etc so it becomes majorCause1 then next one majorCause2 and the next one magjorCause3 etc etc
Under the labels there is an if statement that is like - if($majorCause1Error !='') { do something } - I want this to be like if($majorCause1Error !=''){} and then the next one be like if($majorCause2Error !=''){} and then if($majorCause3Error !=''){}
Does this make more sense?
Here is a link to the site http://www.foresightaus.com.au/form/
if(trim($_POST['majorCause1']) === '') {
$majorCause1Error = "Please enter a major cause.";
$hasError = true;
} else {
$majorCause1 = trim($_POST['majorCause1']);
}
if(trim($_POST['majorCause2']) === '') {
$majorCause2Error = "Please enter a major cause.";
$hasError = true;
} else {
$majorCause2 = trim($_POST['majorCause2']);
}
<li class="fill-in">
<label for="majorCause<?php echo($i); ?>"><?php echo($j); ?>. State one major cause:</label>
<input type="text" name="majorCause<?php echo($i); ?>" id="majorCause<?php echo($i); ?>" value=""/>
<?php if($majorCause1Error != '') { ?>
<span class="error"><?=$majorCause1Error;?></span>
<?php } ?>
</li>
You probably want to be using an array but what you are referencing is called a variable variable and is supported by PHP!
Something like this should do it
${"majorCause{$i}Error"}