form and file upload to two different controllers - php

I have a form that I want to submit with one submit button.
However my action to upload a file is a different controller then the database posts. it looks like this...
<form method="post" action="/admin/upholstery_product/add/">
<h3>Add a Product</h3>
Frame: <input type="text" name="productname" /><br>
Parent Category:
<select name="category">
<?php
foreach($cats AS $cat){
$sel = ($curCat->category_id == $cat->category_id) ? 'selected' : '';
echo "<option value='".$cat->category_id."' ".$sel.">" . $cat->category_name . "</option>\n";
}
?>
</select><br />
Family: <input type="text" name="family" /><br>
Width: <input type="text" name="width" size="6" /><br>
Height: <input type="text" name="height" size="6" /><br>
Depth: <input type="text" name="depth" size="6" /><br>
Seat Width: <input type="text" name="seat_width" size="6" /><br>
Seat Height: <input type="text" name="seat_height" size="6" /><br>
Seat Depth: <input type="text" name="seat_depth" size="6" /><br>
Arm Height: <input type ="text" name="arm_height" size="6" /><br>
Features: <textarea cols="40" rows="20" name="features"></textarea><br>
<form method="post" action="/upload/product_image_upload">
Image File: <input type="file" name="image" id="image"/><br>
</form>
<form method="post" action="/upload/product_image_upload">
Thumbnail File: <input type="file" name="thumbnail" id="thumbnail"/><br>
</form>
<form method="post" action="/upload/pdf_upload">
PDF File: <input type="file" name="pdf" id="pdf"/><br>
</form>
Status: <select name="status">
<option value="active">Active</option>
<option value="inactive">Inactive</option>
</select> <br>
<input type="submit" name="submit" value="Add Product" /><br>
</form>
is there a way to post the file actions to one controller and the submit to my other controller with a single submit button? right now the code doesnt work.

What you want to do is, strictly speaking, not possible: You have two separate forms on one page, andf you want to submit both simultanously.
The main problem here is this: The result of a submitted form is a new page. Different actions = different pages. Which one do you want to display?
Workaround: Submit both the file and the data to the same script; if the handlers need to be separate, make the first handler post the data to the second handler "behind the scenes".

Related

How can I send a post form without click submit button?

For example below code, I have to click on submit button to go to the destination post page, but I want to go to the destination page without click on the submit button, and I want to go to the destination page with click on the button in my telegram bot an run my code page.
<html>
<body>
<form action="?????????" method="post" target="_blank">
type: <input type="text" name="type" value="111" ><br>
amount: <input type="text" name="amount"value="1000"><br>
cellphone: <input type="text" name="cellphone"value="2222"><br>
email: <input type="text" name="email"value="111#gmail.com"><br>
webserviceId: <input type="text" name="webserviceId"value="34433"><br>
redirectUrl: <input type="text" name="redirectUrl"value="w2323.php"><br>
issuer: <input type="text" name="issuer"value="ssdsd"><br>
redirectToPage: <input type="text" name="redirectToPage"value="True"><br>
scriptVersion: <input type="text" name="scriptVersion"value="Script"><br>
firstOutputType: <input type="text" name="firstOutputType"value="get"><br>
secondOutputType: <input type="text" name="secondOutputType"value="get"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
Add an id to the form, then bind a function on a button. Although your question is a bit unclear on what you want exactly
<form id='someid'>
type: <input type="text" name="type" value="111" ><br>
amount: <input type="text" name="amount" value="1000"><br>
cellphone: <input type="text" name="cellphone" value="2222"><br>
email: <input type="text" name="email" value="111#gmail.com"><br>
webserviceId: <input type="text" name="webserviceId" value="34433"><br>
redirectUrl: <input type="text" name="redirectUrl" value="w2323.php"><br>
issuer: <input type="text" name="issuer" value="ssdsd"><br>
redirectToPage: <input type="text" name="redirectToPage" value="True"><br>
scriptVersion: <input type="text" name="scriptVersion" value="Script"><br>
firstOutputType: <input type="text" name="firstOutputType" value="get"><br>
secondOutputType: <input type="text" name="secondOutputType" value="get"><br>
<button onclick="myFunc()">
</form>
JS
function myFunc() {
document.getElementById("someid").submit();
}

getting object not found error after clicking submit button, I want to display the value of hidden tag

<html>
<BODY>
<form action= "myscript.php" method="post">
first name: <input type="text" name="firstname" value="Mary"><br>
Last name: <input type="text" name="lastname" value= "clan"><br>
<input type="checkbox" name="mychoices[]" value="choice1" checked>
<input type="checkbox" name="mychoices[]" value="choice2">
<input type="checkbox" name="mychoices[]" value="choice3">
<select name="myselection">
<option value="selection1" selected>Option1</option>
<option value="selection2">Option2</option>
<option value="selection3">Option3</option>
</select>
<select name="myselections[]" size="3" multiple>
<option value="choice1" selected >Choice1</option>
<option value="choice2" selected>choice2</option>
<option value="choice3">choice3</option>
</select>
<textarea name="mytextarea" rows="10" cols="40">
Welcome to the web developement world.
</textarea>
<input type="password" name="mypassword">
<input type="hidden" name="myname" value = "myvalue">
<input type="reset" value="reset form">
<input type="image" name="myimage" src="desert.jpg" height="42" width="42" onclick= "document.write('<? php Aftersubmit() ?>');"/>
<input type="submit" name="submitbutton" value="Submit Form">
</form>
<?php
function Aftersubmit()
{
$myname = $_POST['myname'];
if(isset($myname)){
echo ($myname);
}
}
?>
</BODY>
</HTML>
I want to display the value of hidden tag after clicking submit button. But getting "Object not found" error 404. Beginner in php, pls help. I also want to know how to call php functions from html.
You can't call a PHP function from an onClick event. That only works with Javascript functions. So one problem is in this line:
onclick= "document.write('<? php Aftersubmit() ?>');
You can remove that, and then pull your PHP out of the function. This will display your name if you click the submit button.
<?php
if(isset($_POST['submitbutton'])){
$myname = $_POST['myname'];
if(isset($myname)){
echo ($myname);
}
}
?>
You may also have to change the action of your form:
<form action= "myscript.php" method="post">
If the file myscript.php doesn't exist, then you'll get a 404 Not Found error every time. You can make a form point to itself by removing the action attribute:
<form method="post">

WordPress and GET

I'm having form and page(Template Page) in WP. Through form, I want send values GET, then the page create query.
form:
<form id="homepage_form" method="get">
<input type="hidden" name="p" value="48" />
<input class="first" type="text" name="name" />
<input type="text" name="location"/>
<select name="category">
<option value="0" disabled selected>Kategori</option>
</select>
<input class="btn" type="submit" value="Search" />
</form>
First Question. Do I need send id page in form?(input type="hidden" name="p" value="48"). Maybe is other method.
Second Question. When I sent this form, I saw "page_id_48/?location=asdasd". WP stolen second GET (name).
Now I know... I don't like forms in WP or I don't understand.
you need the attribute value , in order to GET it.
<input class="first" type="text" name="name" value="something" />
In WP can't to send get 'name'
<input class="first" type="text" name="name" />
<- bad
<input class="first" type="text" name="notname" /> <- ok

html form is being sent on reset button

I have a problem with the html form.
This is my code:
<form name="contactform" method="post" action="send_form_email.php">
<input id="first_name" type="text" name="first_name" size="30"/>
<input id="company_name" type="text" name="company_name" size="30"/>
<input id="email" type="text" name="email" size="30"/>
<input type="image" align="middle" src="images/accept.png" alt="Submit" /> <input id="reset" type="image" src="images/reset.png" alt="Reset" align="middle" />
</form>
when I click any of this images my form is sent. How can I make this reset picture to only reset and not to send the form.
Your reset button is not a reset button. It has type="image", it is server side image map.
When you click on a server side image map the form will be submitted and the coordinates you clicked on will be included in the data.
If you want a reset button that is an image use:
<button type="reset"><img src="..." alt="..."></button>
The image type is like a submit button.
You have to take type="reset" and style your button with css.
Use
<input type = "reset" value = "" class = "btnResetClass" />
Your, btnResetClass will have Css Setting for background image to be set by you.
You have to use type="reset".
This jsFiddle example would be better.
HTML:
<form id="contactform" method="post" action="send_form_email.php">
<div>
<label>
First Name: <input id="first_name" name="first_name" type="text" size="30" />
</label>
</div>
<div>
<label>
Company Name: <input id="company_name" name="company_name" type="text" size="30" />
</label>
</div>
<div>
<label>
Email: <input id="email" type="text" name="email" size="30" />
</label>
</div>
<input id="submit" type="submit" value="" title="Submit" />
<input id="reset" type="reset" value="" title="Reset" />
</form>
CSS:
#submit, #reset {
border: none;
width: 64px;
height: 48px
}
#submit {
background: url('images/submit.png')
}
#reset {
background: url('images/reset.png')
}

How to process multiple forms using one php script

I have multiple forms and I have one php script that I want to use to process these forms but when I click on submit for any of the forms...the script is processed by the number of forms with the submit button named 'submitForm' in this case, the alert will show 3 times instead of once! What am I not doing right?
NB. I hope this makes much sense?
html code
<form action="" name="form1" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
php script
<?php
if (isset($_POST['submitForm'])) {
echo('<script>alert("Form Submitted")</script>');
}
?>
when I click on submit for any particular form, it submits all the forms.
this is not true.
Once your forms have proper formatting, your browser will submit only current one.
(and PHP has nothing to do here)
however, whole page will be reloaded, if you mean that. That is okay - when you submit a form, a page is intended to reload. If you need another behavior, you have to explain your wishes.
Also note that none of your text fields being sent to the server as they have no names.
I guess the question I should be asking is, how do I pass a particular form to php instead of writing multiple php scripts to handle each form!!!
well, it seems you want to ask how to distinguish these forms.
add a hidden field into each
<input type="hidden" name="step" value="1" />
and then in PHP
if ($_POST['step'] == 1) {
//first form
}
if ($_POST['step'] == 2) {
//second
}
This submits one form of many to php. Copy, paste, test, and study.
<?php
if (isset($_POST['submitForm'])) {
print_r($_POST);
}
?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
Using a,b,c,d for the first form, e,f,g,h for the second form and i,j,k,l for the third form and submitting the second form yields the following output:
Array
(
[A] => e
[B] => f
[C] => g
[D] => h
[submitForm] => Submit Form
)
#Jay
Actually its not hard.
Once you supply form names, your work is done. the DOM does the rest.
write one php block to do your functions (create/update/retrieve/delete)
Whichever button is clicked, by default it submits only the elements enclosed together with it.
if(!empty($_POST)){
if(isset($_POST['submit'])){
print "<pre>";
var_dump($_POST); // write your code here as you would
print "<pre>";
}
}
try this with your form above.
I know this is an old post but here's how I solve this very problem.
All you need to do is make sure the submit buttons in each form have different names. Eg:
<form action="" name="form1" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm1" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm2" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm3" />
</form>
Then, you simply check which form's submit button was pressed.
<?php
if (isset($_POST['submitForm1'])) {
echo('<script>alert("Form 1 Submitted")</script>');
} elseif (isset($_POST['submitForm2'])) {
echo('<script>alert("Form 2 Submitted")</script>');
} elseif (isset($_POST['submitForm3'])) {
echo('<script>alert("Form 3 Submitted")</script>');
}
?>
If you need dynamic forms, you may try below code. While statement can be changed to fetch data from DB and use foreach instead. Hope you know this.
Here, I used while($n<10) for 10 dynamic forms.
You can also use tag as below if you need separate form names.
<form action="" name="form<?=$n?>" method="post">
This will create separate form names such as form1, form2, etc but not necessary here.
<?php
if (isset($_POST['submitForm'])) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
$n=0;
while($n<10) {
$n++;
?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<?php
}
?>
Sample page with output when I click row 5..

Categories