2 Form Submits interfering with each other - php

I have a PHP script where I upload a *.txt file and I use the information inside that *.txt file to do some things.
The form for uploading this text file:
<form method="post" action="index.php" enctype="multipart/form-data">
<input class="file-choice" name="file" type="file" />
<input class="upload-button" type="submit" value="Upload Bestand" name="start" />
</form>
After I worked with this file I want to give the visitor a choice to change some things, with their own input.
The form for the user input:
<form method="post">
<div class="input-block">
<b>Rechts naar links:</b> <br>
<input type="radio" name="horizontaal" value="rl-aan">Aan
<input type="radio" name="horizontaal" value="rl-uit">Uit<br>
</div>
<div class="input-block">
<b>Verticaal:</b> <br>
<input type="radio" name="verticaal" value="ver-aan">Aan
<input type="radio" name="verticaal" value="ver-uit">Uit<br>
</div>
<div class="input-block">
<b>Diagonaal:</b> <br>
<input type="radio" name="diagonaal" value="dia-aan">Aan
<input type="radio" name="diagonaal" value="dia-uit">Uit<br>
</div>
<input type="submit" name="test" />
</form>
My problem is when I press the submit button of either the file upload or user input the other form is retested. So for example if I press the submit for the user input, the file that I uploaded before is gone.
I tried changing the method from POST to GET and I've tried to give both the submits the same name but both did not work.
Any suggestions?

I figured it out.
I had to work with sesions to save the needed variables from reading the file and add the name of the user input submit to all the scripts except the read script from the file.

Give different form id and submit button id .

Related

Submitting an image alongside a form with one button

I have a regular form which submits to my database but I want to include a file upload in the form, so as to receive the form input in the expected table using PHP post and also receive the uploaded image.
Use <input type="file"> to include a file upload in the form.
Use something like this to send Files as well as other inputs to the processing page where you can access files and post variables.
<form action="#" method="post" enctype="multipart/form-data">
<input type="text" name="txtbox">
<br />
<input type="file" name="filebox">
<br />
<input type="submit" name="submit" value="submit">
</form>
When this is submitted you can access the data from $_FILES and $_POST
There are various input types in HTML just as the below:
<input type="button">
<input type="checkbox">
<input type="color">
<input type="date">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="hidden">
<input type="image">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="submit">
<input type="tel">
<input type="text">
<input type="time">
<input type="url">
<input type="week">
Each of the above inputs demand different conditions and formats to be satisfied.
For your query the answer is either:
<input type="file">
For webpages that require inputs in a file format. Or,
<input type="image">
For webpages requiring input in an image format.
For further details view the below link for help:
https://www.w3schools.com/html/html_form_input_types.asp

using one input file with two different form

Is there a way to using a input file with two different form?
I need only one input file and have to post this file to .php page depending of user selection.
Thanks.
<label for="file">Photo:</label>
<input type="file" name="file" id="file">
<form action="c.php" method="post" enctype="multipart/form-data" >
<input type="submit" name="submit" value="C">
</form>
<form action="d.php" method="post" enctype="multipart/form-data">
<input type="submit" name="submit" value="D">
</form>
You could redirect to page cd.php which includes c.php or d.php depending on the value C or D

Submit form in two different ways

I have a form with two "a" links. Both should submit form, but in two different ways.
This is html I currently have.
HTML:
<form method="post" id="doc_form" target="somwhere">
<textarea name="doc_text"><?php echo $file; ?></textarea>
<input type="hidden" name="submitted" value="1" />
</form>
<a id="doc_send_email" class="btn">SEND</a>
<a id="pritn_pdf_btn" onclick="document.getElementById('doc_form').submit();" class="btn">Print <span class="span_pdf">PDF </span></a>
When user clicks on #pritn_pdf_btn, then the form is submitted to index.php with target="somwhere". On server side I am generating pdf with value from textarea and this PDF is opened in new tab (target="somwhere" is some trick that opens pdf in new tab).
However, now I also need submit form on #doc_send_email click. In this case I also need to generate PDF, but it should be sent to provided email and then it should show confirmation message on the same page. So:
In index.php I need somehow distinguish what caused form submission (pritn_pdf_btn) or (doc_send_email). If I would be able to set some variable on #pritn_pdf_btn.click and on #doc_send_email.click and then sent it via POST, it would help. But I could not find sollution.
I need some way to submit form with target="somwhere" and without target="somwhere". Probably, there is someway on the #pritn_pdf_btn.click and on #doc_send_email.click to submit form with and without target?
It's easier to do it with multiples submit buttons like this:
<form method="post" id="doc_form" target="somwhere">
<textarea name="doc_text"><?php echo $file; ?></textarea>
<input type="hidden" name="submitted" value="1" />
<input type="submit" name="email" value="1" id="doc_send_email" class="btn" value="SEND"/>
<input type="submit" name="pdf" value="1" id="pritn_pdf_btn" class="btn" value="Print PDF"/>
</form>
Then at backend (asuming it is PHP)
if($_POST['email'])
sendEmail();
else
generatePDF();
Or by using JS:
<form method="post" id="doc_form" target="_self">
<textarea name="doc_text"><?php echo $file; ?></textarea>
<input type="hidden" name="submitted" value="1" />
<input type="hidden" name="action" id="action" value="generate" />
</form>
<a id="doc_send_email" onclick="document.getElementById('action').value='email';document.getElementById('doc_form').submit();" class="btn">SEND</a>
<a id="pritn_pdf_btn" onclick="document.getElementById('action').value='generatePdf';document.getElementById('doc_form').target='somwhere';document.getElementById('doc_form').submit();" class="btn">Print <span class="span_pdf">PDF </span></a>
Then at backend (asuming it is PHP)
if($_POST['action']=='email')
sendEmail();
else
generatePDF();

How to submit a form to destination url as destination site

I need to submit a form with action is:"http://www.destination.com/hotelbooking" as "http://www.destination.com" but I do not own that site.
My site is http://www.mysite.com, after the submitting, url in my browser is "http://www.mysite.com/bookingstatus" instead of "http://www.destination.com/bookingstatus".
I guess that the site force me to submit my form from a browser because if I use curl, it doesn't worked(but I need to do that with php).(*My edit)
How can I solve this problem?
This is my form:
<form name="booking" method="post" action="https://www.destination.com/hotelbooking" >
<input name="roomsnum" type="text">
<input name="adultnum" type="text">
<input name="childnum" type="text">
<input name="infannum" type="text">
<input name="roomsize" type="text">
<input name="submit" type="submit" value="booking">
</form>
Thanks a lot

Form data and Button in the same _POST

Evening!
I'm having a spot of an issue figuring out how to get a particular section of code to work, I have this:
<form>
TestCheckBox
<input type="checkbox" name="TestCheckBox" value="Yes" />
</form>
<div id="search">
<input type="text" id="search_bar" />
<input type="button" value="Search!" />
</div>
And in a php file, I have this:
if($_POST['TestCheckBox'] == 'Yes'){
echo "TEST";
}
if (isset($_POST['action']) && !empty($_POST['action'])) {
generateHTML($_POST['action']);
}
else {
echo "NOTHING IS HERE";
}
Obviously this is not how to do this. I'm curious as to hwo I can make my search bar submit the post data also included in the checkbox.
(It's for a search bar, and the checkboxes are advanced search options, so naturally I only want one search button).
Thank you!
Give the inputs names (without them they cannot be successful controls)
Put them inside the form (otherwise they are only useful to client side scripts)
Make the form make a POST request (it defaults to GET).
Use a submit button so the form will be submitted (regular buttons are only for client side scripts)
It is also beneficial to include labels (which inform users what controls are for and provide larger click targets (the latter is especially important for checkboxes and radio buttons)).
Such:
<form method="post">
<label for="TestCheckBox">TestCheckBox</label>
<input type="checkbox" name="TestCheckBox" id="TestCheckBox" value="Yes" />
<div id="search">
<label for="search_bar">Query</label>
<input type="text" id="search_bar" name='action' />
<input type="submit" value="Search!" />
</div>
</form>
You have to wrap the <form> around all the <inputs>.
<form>
TestCheckBox
<input type="checkbox" name="TestCheckBox" value="Yes" />
<div id="search">
<input type="text" id="search_bar" />
<input type="button" value="Search!" />
</div>
</form>

Categories