Well, I've got a page with HTML like this:
<form method="post" action="" enctype="multipart/form-data">
<!-- Some more markup -->
<form method="post" action="" enctype="multipart/form-data">
<input type="submit"name="reset_ph" value="<?php _e('Reset styles'); ?>" />
<input type="hidden" name="subaction" value="reset_ph" />
</form>
<p><input name="update" type="submit" value="<?php _e('Save changes'); ?>" style="padding:3px;" /></p>
<input type="hidden" name="action" value="update" />
</form>
Then PHP code:
//updating main form
if(isset($_FILES['phtfile']['name']) && $_REQUEST['action']=='update'){
$def_path = TEMPLATEPATH.'/img/author/';
$file_path = $def_path.basename($_FILES['phtfile']['name']);
$file_path = str_replace('\\','/',$file_path);
$file_css_path = get_bloginfo('template_url');
$file_css_path = $file_css_path.'/img/author/'.basename($_FILES['phtfile']['name']);
$isfile = move_uploaded_file($_FILES['phtfile']['tmp_name'],$file_path);
if ($isfile) { update_option('own_pht_url', $file_css_path);}
}
//update subform
if ($_POST['subaction']=='reset_ph'){
global $photo_path;
update_option('own_pht_url', $photo_path.'tmp.jpg');
}
Subform contains a button, that resets image shown to default one (via setting path to image to default). Main form contains the image upload dialog, and should change path to image to a new one, should the file be uploaded. But updating the main form, updates the subform, and path is set to default.
I've figured a workaround, by changing the button to a checkbox, but I'm still interested, does updating the master form always updates every sub-form inside it? No ways around it?
Thank you for your time.
Nested forms are known to cause problems. You can check out this thread:
Why WHATWG disallows nested forms in HTML 4 and HTML5?
Related
I'll try and explain this as best as I can. Basically, I'm using a form to receive a comment. Upon hitting submit, the action creates a link similar to this: http://localhost:8080/camagru/comment.php?comment=test&post=Post
I have a variable with the image name in it that I want to pass as well, so something like this: http://localhost:8080/camagru/comment.php?img=test.png&comment=test&post=Post
I've tried using <form action="<?php echo commentpost.php?img=$img?>"> But everytime the submit button is pressed, it erases the img variable from POST and only puts in the new variables from the form.
Any suggestions?
add new hidden field in form tag like that
<form action="commentpost.php" method="post">
<input type="hidden" value="<?php echo $img ?>" name="img" />
<input type="submit" value="Save" name="IsSubmit" />
</form>
Now you can able to use $_POST['img']
The img variable is in GET.
If you want it in POST, try <input type="hidden" name="img" value="test.png">
use quotes in your case:
<form action="<?php echo "commentpost.php?img=$img"; ?>">
the best practice is to insert hidden element into your form:
<input name="img1" type="hidden" value="test.png" />
<input name="img2" type="hidden" value="test2.png" />
Form 1:
<?php
echo $this->upload_message;
?>
<form enctype="multipart/form-data" method="post" name="mfuploaderwp-uploadform" action="?uploadfile">
Upload file: <input name="mfuploadwp-filename" type="file">
<input class="mfuploadwp-submit" type="submit" value="Upload" name="submit"></form>
Form 2:
<?php
echo $this->upload_message;
?>
<form enctype="multipart/form-data" method="post" name="mfuploaderwp-uploadform" action="?uploadfile">
Upload file: <input name="mfuploadwp-filename" type="file">
<input class="mfuploadwp-submit" type="submit" value="Upload" name="submit"></form>
Is it possible to fetch which form is submitted without giving any extra attributes or so to above forms? The forms are created dynamically based on what user enter for amount number of forms. (In this case user has entered 2 forms)
I want to do this so $this->upload_message would be accurate only for the form that is used for uploading.
Alter the name tags on your <input type="submit"> buttons. Have one as name="submit" and the other as name="submit_two" (for example, bad naming convention), then process code as
if (isset($_POST['submit'])) {
// do stuff
} elseif (isset($_POST['submit_two'])) {
// do other stuff
}
Yes, it's possible.
The cleanest way, in my opinion, is to put an hidden input tag in each form:
<form enctype="multipart/form-data" method="post" name="mfuploaderwp-uploadform" action="?uploadfile">
<input type="hidden" name="active_form" value="1">
(...)
and
<form enctype="multipart/form-data" method="post" name="mfuploaderwp-uploadform" action="?uploadfile">
<input type="hidden" name="active_form" value="2">
(...)
then, in the page that process the form, you can check it in this way:
if( $_POST['active_form'] == 1)
{
(...)
}
elseif( $_POST['active_form'] == 2)
{
(...)
}
If your form is generated dynamically based on the user input(The forms are created dynamically based on what user enter for amount number of forms), in this case you can use three type of solution as far as I know,
You can introduce a new hidden field for each form based on the form number.
Eg:
Upload file: <input name="mfuploadwp-filename" type="file">
<input class="mfuploadwp-submit" type="submit" value="Upload" name="submit">
<input type="hidden" value="1" name="form_id"/>
</form>
in php
switch($_POST['form_id']) {
//the form data to be processed..
}
or
You can update the input field submit button naming based on the form number.
Eg:
Upload file: <input name="mfuploadwp-filename" type="file">
<input class="mfuploadwp-submit" type="submit" value="Upload" name="submit_{form_id}">
you can add a additional parameter in the form method.
...
I have a form on an HTML/PHP page.
I have the same exact code on other pages on the site, and it works fine. I cannot figure it out.
Form:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="prize_id" type="hidden" value="<?php echo $prize_id; ?>" />
<input name="ContestEntry" type="submit" class="submit" value="Enter me in the Raffle!" />
</form>
I looked at the HTML source code, and the action populates the correct page, and $prize_id populates the correct info in the value.
PHP:
if(isset($_POST['ContestEntry'])){
//...code to enter data into form, irrelevent since it won't post anything anyway.
}
else {
echo 'Nothing posted from form';
}
"Nothing posted from form" always shows, and no data is being entered into the database. I've tried changing the form name to 5 different things, thinking maybe there was a conflicting name somewhere, but nothing works.
Any ideas?
If all you need to achieve is check whether the form is submitted or not, it's better to check that the request type is a POST:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Process form
} else {
// Nothing posted!
}
Also change your submit button to:
<button type="submit">Submit</button>
and see what happens
The code you posted actually works for me.
But, if you still can't get it working, I would try checking to see if prize_id is set rather than the button. The PHP script is executed when the form is submitted.
Also, I would recommend that you don't use $_SERVER['PHP_SELF'] as the form action. According to https://stackoverflow.com/a/14093363/3593228, that can make it easy for attackers to insert malicious data. Instead, leave the action empty.
The if Condition is not working because you put it on Button, instead of this just make an hidden field in the form to check form is submit or not like as you already have one. Or make new for this form.
<input name="prize_id" type="hidden" value="<?php echo $prize_id; ?>" />
Consider adding what you're looking for as another hidden field:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="prize_id" type="hidden" value="<?php echo $prize_id; ?>" />
<input name="ContestEntry" type="hidden" value="Enter me in the Raffle!" />
<input type="submit" class="submit" />
</form>
so I'm working on a site, I currently have:
<form method =post action=process.php>
<form method = get action = process3.php>
<input type=submit value = add name = action/>
</form>
</form>
is it possible to add a attribute to control which form this input calls?
For reasons un-mentioned above I am unable to simply use just one method
You cannot nest forms like you do. You can submit forms from outside.
<form id="myform1" name="form1" action="" method="post">
<input name="field1" value="form1" />
</form>
<form id="myform2" name="form2" action="" method="post">
<input name="field2" value="form2" />
</form>
<input type="submit" form='myform1' value="submit1" />
<input type="submit" form='myform2' value="submit2" />
You can also add input fields outside the form tag
add input to form 2:
<input type="text" name='outside' value="outside" form='myform2' />
No. Forms can't be nested like this. That is invalid HTML. Moreso, its impossible to GET and POST data at the same time.
The submit action is strongly tied to its parent form, so I'd recommend writing your forms as siblings, then putting the field in the appropriate one and giving each form its own submit input.
<form method =post action=process.php>
<input type=submit value = add name = differentAction/>
</form>
<form method = get action = process3.php>
<input type=submit value = add name = action/>
</form>
Unless you are using way more sophisticated methods for controlling your forms the best way is to keep your submit action within the corresponding <form> </form> tag.
This might prove to be helpful depending on what you are trying to accomplish:
Submit multiple forms with one submit button
and this:
http://jquery.malsup.com/form/#getting-started
I've created a HTML and PHP form to pretty much just input data into a MySQL table.
I've got a file upload field, but it doesn't upload a file. I don't want it to upload anything to the server, all I want is for it to grab the location of the file selected and input that into the database.
It works perfectly fine on Internet Explorer, but doesn't work on any other browser, I assume it's because of security issues it imposes.
My input code:
<input type="file" id="filename" name="filename" />
And of course when the form is submitted some PHP code runs through to insert the data into the database. I've tried JavaScript to copy value of that field to to another field but again because of security issues that doesn't work.
Does anyone know of a way I can do this in JavaScript? Or even Flash? To be honest, anything similar to these would do, just something that works in browsers other than IE.
Thanks.
EDIT -
Here's my code:
<?php
if (isset($_POST['submit'])) {
$name = addslashes(htmlentities($_POST['name']));
$filename = addslashes(htmlentities($_POST['filename']));
if (#mysql_query("INSERT INTO movies SET
name='$name',
doc_filename='$filename'")) {
echo '<p>Movie added.</p><p>Manage movies.</p>';
} else {
echo '<strong>Error:</strong> '.mysql_error().'';
}
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="newspost" enctype="multipart/form-data">
<div class="form-row">
<label for="fm_title">Movie file path: </label><br />
<input type="file" id="filename" name="filename" />
</div><br />
<div class="form-row">
<label for="fm_title">Movie name: </label><br />
<input type="text" class="input-width" name="name" value="<?php if (!empty($_POST['name'])) { echo $_POST['name']; } ?>" />
</div>
<div class="form-row">
<input type="submit" name="submit" value="Submit" />
</div>
</form>
<?php
}
?>
all I want is for it to grab the location of the file selected and input that into the database.
This is no longer possible using HTML - for security reasons, the location of the selected file is not accessible in the file input's DOM element anymore. Most browsers use C:\Fakepath\ instead.
You might be able to get hold of this information using a Flash or Java based uploader (edit: see e.g. this page in the AS3 manual) SWFUpload's API could be worth a look. It could be that you need to build your own Flash or Java solution for this, though.
how you are posting the data of the form? I am not able to see any way of form post. No Submit button or onclick or onchange event of javascript? and also how you suppose this line to give output
<input type="text" class="input-width" name="name" value="<?php if (!empty($_POST['name'])) { echo $_POST['name']; } ?>" />
as its already in else part of the line
if (isset($_POST['submit']))