there seem to be a lot of people asking this question and not getting an answer.
is there an issue with checking if a file upload part of a form has been filled in or not?
is this because its a global variable and is allways set?
the problem im having is checking if a file input ytpe has been filled in or not.
the code below shows various examples of checking to see if the file field is filled in by the user or not.
they all fail to spot when its been filled in:
<?php
if (isset($_POST['add']))
{
//if (!isset($_POST['pic'])) { echo"pic not filled in";}else{echo"pic filled in";}
if (!isset($_POST['pic'])) { echo"pic not filled in";}
if (isset($_POST['pic'])) { echo"pic filled in";}//end of check to see if picture has been filled in
//if (!isset($_POST['userfile[]'])) { echo"pic not filled in";}
//if (isset($_POST['userfile[]'])) { echo"pic filled in";}//end of check to see if picture has been filled in
//$pictureName = $_REQUEST['pic'];
//if ($pictureName == ''){echo"pic is blank";}
//else{echo"pic is NOT blank";}
//if ($_POST['pic'] == ""){echo"pic is blank";}
//else {echo"pic is NOT blank";}
}
else
{
/////////render form
?>
<form enctype="multipart/form-data" action="" method="post" id="save"><fieldset>
<input type="text" name="fileName" id="fileName" value=""/>
<input type="file" name="userfile[]" id="pic" />
<input name="add" id="save_button" type="submit" value="Update"/>
</fieldset></form>
<?php
}
?>
because your using 'pic' not 'userfile'. is the html name that makes the post key value, not the id.
it should be userfile not userfile[] unless you have a multi input form
P.S I see no large number of unanswered questions of this type.
i have found the answer ive been looking for finally.
a massive thanks to You whos answer to this question helped me a lot:
the problem i was having was that if you check to see if a field in the form isset it will always be set once the submit button has been pressed.
if, like me, you want to check if something has actually been entered in to the field then i would say you need to do an if(!empty) check.
if the enctype="multipart/form-data" is added to the form this will stop working for file type fields.
please see my example below and thanks again to You:
<h1>test</h1>
<p>check to see if a field in a form as been filled in:</p>
<?php
if (isset($_POST['info'])) {
// do some stuff
echo"info isset. it will always be set when you click the submit button</br>";
if(!empty($_POST['info'])){ echo"info has been filled in</br>";}else{ echo"info is still empty</br>";}
if(!empty($_POST['filename'])){ echo"filename has been filled in</br>";}else{ echo"filename is still empty</br>";}
}
else
{
?>
<form method="post">
<input type="text" name="info" /></br>
<input type="file" name="filename" /></br>
<input type="submit" />
</form>
<?php }?>
Related
I have an input type text box as follows
<input type="text" name="deleteprofileconfirmation" id="deleteprofileconfirmation" class="editprofileinput">
Delete Account
I need to pass the value entered in the input type text to deleteaccount.php
I can do with help of jquery, no problem, i need a pure php solution...
I tried using sessions, but problem is how to read the value in input type when link is clicked.. $_POST is also not working...
i cannot use form because this is a form in another form so html5 is not allowing nested forms, sorry should have mentioned that earlier
the following is not working on deleteaccount.php
if (isset($_POST['deleteprofilebutton']))
{
$delete_profile = strtolower($_POST['deleteprofileconfirmation']);
}
Make your link as
href="../controllers/deleteaccount.php?id=$ID_VALUE"
and update the POST to GET
if (isset($_GET['id']))
{
$delete_profile = strtolower($_GET['id']);
}
That would be GET now.
Make sure users with no privileges can hit this url and delete the profiles. Do check the user rights before processing the delete operation.
You can do this using form
<form action="../controllers/deleteaccount.php" method="post">
<input type="text" name="deleteprofileconfirmation" id="deleteprofileconfirmation" class="editprofileinput">
<input type="submit" class="deleteprofilebutton" name="deleteprofilebutton" id="deleteprofilebutton" value="Delete Account">
</form>
You could also give each one a unique name and just check the $_POST for the existence of that input.
<input type="submit" name="deleteprofileconfirmation" value="Delete" />
<input type="submit" name="submit" value="Submit" />
And in the code:
if (isset($_POST['deleteprofileconfirmation'])) {
//delete action
} else if (isset($_POST['submit'])) {
//submit action
} else {
//no button pressed
}
I'm a newbie in PHP, and I would like to send datas from a form and display it into the same page, here is my code for better understanding:
<form method="post" action="same_page.php">
<input type="text" name="owner" />
<input type="submit" value="Validate" />
</form>
<?php
if(isset($_GET['owner']))
{
echo "data sent !";
}
?>
So normally, after having entered some random text in the form and click "validate", the message "data sent!" Should be displayed on the page. I guess I missed something, but I can't figure out what.
You forgot to add submit name in your form.You are using POST as method so code should be
<form method="post" action="">
<input type="text" name="owner" />
<input type="submit" name="submit_value" value="Validate" />
</form>
<?php
if(isset($_POST['submit_value']))
{
echo '<pre>';
print_r($_POST);
}
?>
Will display your post values
You are using a POST method in your form.
<form method="post" action="same_page.php">
So, change your code to:
if (count($_POST) && isset($_POST['owner']))
Technically, the above code does the following:
First checks if there are content in POST.
Then, it checks if the owner is set.
If both the conditions are satisfied, it displays the message.
You can actually get rid of action="same_page.php" as if you omit it, you will post to the same page.
Note: This is a worst method of programming, which you need to change.
You should Replace $_GET['owner'] with $_POST['owner'] as in your form you have specified method='post'
Replace:
$_GET['owner']
With:
$_POST['owner']
Since you are using the post method in your form, you have to check against the $_POST array in your PHP code.
I have a simple form which was working and now im finding the post data isnt being sent and i can't see the problem
<form role="form" name="challengeform" action="scripts/arena_setup.php" method="POST" onsubmit="return confirm('Are you sure you want to attack this player?');">
<input type="hidden" name="member_id" value="<? echo $member_id;?>">
<input type="image" src="img/map/attack.png" alt="Attack" />
</form>
which is being handled by
if(isset($_POST['challengeform'])){
...
}else{ echo 'error'; }
it always shows the error due to the post data being missing but i just cant see what i've done. Any ideas?
if(isset($_POST['challengeform']))
Form names are not part of the POST data. Only fields within the form.
Try testing for the field itself
if(isset($_POST['member_id']))
You shouldn't write the name of the form. Just write input's name to get the data. Ex:
$var = $_POST['member_id'];
I don't know what happened but there's no value return.
Please check if i made any mistake
Value is blank if you have no check for null or blank value on transfer.php
Case 1 : You are access file in same flow then no need to modify any thing its wokring.If you are access file without press submit button.directly using url then post is blank.
e.g. http://localhost:8080/stackoverflow/transfer.php
Case 2 : MIME type issue, enctype="text/plain" in the form, this should fix the issue.
from_post.php
<html>
<body>
<form action="transfer.php" method="post" enctype="text/plain">
<input type="text" name="name" value="">
<input type="submit" name="submit" value="submit">
</form>
transfer.php
Here you want to check is that submit button if fired or not.
<?php
if(isset($_POST['submit']))
{
$u=$_POST['name'];
if(isset($u) || (!is_empty())){
echo $u;
}
else
{
echo "the post doesn't have any value";
}
echo "<br/>";
}
?>
I am trying to write a dynamic form using PHP. I'd like to have a single webpage that contains two forms:
The upper form allows to search for an element in the mysql database, e.g., for a name
The lower form shows the data that is associated with this name in the database
If I press on the "Search" button of the upper form, then the the lower form is shown and the text fields are filled with data from the database that belong to this name. If I change the user name to some other value and press again "Search", then the data that is associated with the new record is shown and so on.
The lower form also has a button "Update" which allows to transfer changes made to the text boxes (in the lower part) to the database.
Now, I have the following problem: In my script I set initially the value of name (from the upper form) to "". When I then press the "Search" button, then the lower part of the form is shown and the corresponding data is shown in the lower part. When I then press the "Update" button, then the text field associated with name is set to the empty string. This is because in my script I set initially name to the "". I'd like that in this case the data entered in the upper form is not changed, i.e., it stays the same.
I guess, I am missing something here. There is probably an easy solution for this and I am doing something fundamentally wrong. It'd be great if you could help me.
That's what I tried... I deleted lots of details, but I guess that can give you an idea what I am trying to do. Notice that the whole code is in the file update.php.
<?php
function search_bus($mysql, $name)
{
// do some stuff here...
}
function update_bus($mysql, $b_id)
{
// do some stuff here...
}
// some global variables
$b_id = 0;
$username = ""; // username of business
// get b_id that corresponds to username
if (isset($_REQUEST['search']))
{
$b_id =0; // business id
if (isset($_POST['user']))
{
$username = $_POST['user'];
$b_id = search_bus($mysql, $username);
}
}
elseif(isset($_REQUEST['update']))
{
update_bus($mysql, $b_id);
}
?>
<h2>Search:</h2>
<form name="search_bus" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Username: <input type="text" name="user" value="<?= htmlentities($username) ?>"/>
<input type="submit" value="Suchen" name="search"/>
</form>
<?php
if($b_id != 0)
{
?>
<h2>Data:</h2>
<form name="business_design" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<-- some form follows here -->
<?php
}
?>
I think what you're missing is to create a HTML Hidden field to keep the value of Name variable.
<input type="hidden" name="name" value="<?php print $nameVar ?>" />
Add this input to both forms so you can keep the value no matter what button the user clicks.
Hope this helps.
Adding code to verify the
<h2>Search:</h2>
<form name="search_bus" method="post"
action="<?php echo $_SERVER['PHP_SELF'];?>">
Username: <input type="text" name="user" value="<?= htmlentities($username) ?>"/>
<input type="hidden" name="b_id" value="<?php print $b_id?>" />
<input type="submit" value="Suchen" name="search"/>
</form>
<?php if($b_id != 0) { ?>
<h2>Data:</h2>
<form name="business_design" method="post" action="<?php echo $_SERVER['PHP_SELF'];>">
<input type="hidden" name="b_id" value="<?php print $b_id?>" />
<-- some form follows here -->
<?php } ?>
Dont initialize $b_id if it already comes into the http request.
if (!isset($_POST['b_id']))
{
$b_id = 0;
}
else
{
$b_id = $_POST['b_id'];
}
This way you can alway remember the last selected value of b_id.
Hope this can help you.