input required depending on button of form - php

To enter data into my database I created this form. The user can enter data add by clicking the add button the information is entered into the database. I am using required to force the user to enter text.
Now I added the edit and delete button. Therefore the user shall select a record with the radio buttons provided. But with the required in the input elements it is only possible to do edit or delete if text is entered.
Is it somehow possible to assign the required to a certain button?
<form>
<table>
<tr>
<td>
<input type="radio" name="aName" />
</td>
<td>a</td>
<td>record</td>
</tr>
<td>
<input type="radio" name="aName" />
</td>
<td>another</td>
<td>record</td>
</tr>
<tr>
<td></td>
<td>
<input type="text" placeholder="enter" required >
</td>
<td>
<input type="text" placeholder="something" required>
</td>
</tr>
</table>
<input type="submit" value="add" formaction="form1.php" formmethod="post"/>
<input type="submit" value="delete" formaction="form2.php" formmethod="post"/>
<input type="submit" value="edit" formaction="form3.php" formmethod="post"/>
</form>

A way to do this, is without using 'required', but reproducing this html5 effect using event handlers, focus() and alert().
event.preventDefault() prevents the form to submit.
Check it:
<form method="POST">
<table>
<tr>
<td>
<input type="radio" name="aName" />
</td>
<td>a</td>
<td>record</td>
</tr>
<td>
<input type="radio" name="aName" />
</td>
<td>another</td>
<td>record</td>
</tr>
<tr>
<td></td>
<td>
<input id="enter" type="text" placeholder="enter" >
</td>
<td>
<input id="something" type="text" placeholder="something" >
</td>
</tr>
</table>
<input id="add" formaction="form1.php" formmethod="post"
onclick="
if(document.getElementById('enter').value===''){
event.preventDefault();
document.getElementById('enter').focus();
alert('Please,fill out this field');
} else if(document.getElementById('something').value===''){
event.preventDefault();
document.getElementById('something').focus();
alert('Please,fill out this field');
}"
type="submit" value="add"/>
<input id="delete" type="submit" formaction="form2.php" formmethod="post value="delete"/>
<input id="edit" type="submit" formaction="form3.php" formmethod="post value="edit"/>
</form>

Related

Transform value of PHP form on submit

i'm setting up donate option on a website using przelewy24.pl. They have this startup template to send values by $_GET method to their website.
Everything works fine exept the amount field. Przelewy24 needs a gr amount (like cents) and i would like for the donor to type in integer in full zł (like $).
If the upper is not clear - when i type 100 in the field, it sends it to przelewy24 as 100 gr, whitch will be 1 zł.
I need to know how could i format the amount sent to them as in simple calculation - when 100 is typed, get sends 10000. (x*100)
The form used is shown below. The quick-start guide is avaliable here, but only in polish
<form method="get" action="https://sklep.przelewy24.pl/zakup.php">
<input type="hidden" name="z24_id_sprzedawcy" value="TWOJ_ID">
<input type="hidden" name="z24_crc" value="KLUCZ_ZAKUPU">
<input type="hidden" name="z24_return_url" value="TWOJASTRONA.PL">
<input type="hidden" name="z24_language" value="pl">
<table>
<tr>
<td align="right">Nazwa produktu:</td>
<td>
<input type="text" name="z24_nazwa" value="Opłata za rezerwację NR: 04/234/A3953">
</td>
</tr>
<tr>
<td align="right">Dodatkowy opis:</td>
<td>
<textarea name="z24_opis" style="width:250px">Dodatkowe informacje...
</textarea>
</td>
</tr>
<tr>
<td align="right">Do zapłaty:</td>
<td><input type="text" name="z24_kwota"></td><!--KWOTA W GROSZACH-->
</tr>
</table>
<input type="submit" value="zapłać z przelewy24.pl">
</form>
You can do it with a simple Javascript code.
You need to capture the value from input, transform it, and put the value on input hidden:
function formatMoney(e) {
document.getElementById('z24_kwota').value = (!isNaN(e.target.value) ? e.target.value : 0) * 100
// just to debug.. you can remove this line:
document.getElementById('final_value').innerHTML = document.getElementById('z24_kwota').value
}
<form method="get" action="https://sklep.przelewy24.pl/zakup.php">
<input type="hidden" name="z24_id_sprzedawcy" value="TWOJ_ID">
<input type="hidden" name="z24_crc" value="KLUCZ_ZAKUPU">
<input type="hidden" name="z24_return_url" value="TWOJASTRONA.PL">
<input type="hidden" name="z24_language" value="pl">
<table>
<tr>
<td align="right">Nazwa produktu:</td>
<td>
<input type="text" name="z24_nazwa" value="Opłata za rezerwację NR: 04/234/A3953">
</td>
</tr>
<tr>
<td align="right">Dodatkowy opis:</td>
<td>
<textarea name="z24_opis" style="width:250px">Dodatkowe informacje...
</textarea>
</td>
</tr>
<tr>
<td align="right">Do zapłaty:</td>
<td>
<input type="hidden" name="z24_kwota" id="z24_kwota">
<input type="text" onkeyup="formatMoney(event)"></td><!--KWOTA W GROSZACH-->
</tr>
</table>
<input type="submit" value="zapłać z przelewy24.pl">
</form>
<!-- you can remove this line: -->
Final Value: <span id="final_value"></span>
Try changing the value before submitting the form like below,
<form method="get" id="myform" action="https://sklep.przelewy24.pl/zakup.php">
<input type="hidden" name="z24_id_sprzedawcy" value="TWOJ_ID">
<input type="hidden" name="z24_crc" value="KLUCZ_ZAKUPU">
<input type="hidden" name="z24_return_url" value="TWOJASTRONA.PL">
<input type="hidden" name="z24_language" value="pl">
<table>
<tr>
<td align="right">Nazwa produktu:</td>
<td>
<input type="text" name="z24_nazwa" value="Opłata za rezerwację NR: 04/234/A3953">
</td>
</tr>
<tr>
<td align="right">Dodatkowy opis:</td>
<td>
<textarea name="z24_opis" style="width:250px">Dodatkowe informacje...
</textarea>
</td>
</tr>
<tr>
<td align="right">Do zapłaty:</td>
<td><input type="text" name="z24_kwota"></td><!--KWOTA W GROSZACH-->
</tr>
</table>
<input type="submit" value="zapłać z przelewy24.pl">
</form>
<script
src="http://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script type="text/javascript">
var firstSubmit = false;
$('#myform').on('submit',function(e){
if(!firstSubmit){
e.preventDefault();
firstSubmit = true;
var amount = parseInt($('input[name=z24_kwota]').val());
$('input[name=z24_kwota]').val(amount*100);
$('#myform').trigger('submit');
}
})
</script>
Note: I have given an id to the form as myform

Submit button is upper than looping table input field

I have got in trouble when I was going to show (print is more suitable) a list of input field within form.
According to normal situation, there will be a Submit button below it in order to submit the form to (any pages).
Then, I was so confused., because when I use this below code to loop.
<table>
<form>
for($a=1; $a<=10; $a++){
<tr>
<td>
<div class="form-group">
<input class="form-control" type="text" name="name<? echo $a; ?>" required="required"/>
</div>
</td>
<td>
<div class="form-group">
<input class="form-control" type="text" name="url<? echo $a; ?>" required="required"/>
</div>
</td>
</tr>
}
<input type="submit" value="Submit" class="btn btn-primary"/>
</form>
</table>
Tadaa...then it shows:
And the Submit button is on the top of the form?!
Can any help me to fix it?
Or...at least some reason?
Update your code like
<tr>
<td colspan="2">
<input type="submit" value="Submit" class="btn btn-primary"/>
</td>
</tr>
it will display submit button below the input field.
You have incorrect order of html tags.
<table>
<form>
<tr>
...
</tr>
<input type="submit" value="Submit" class="btn btn-primary"/>
</form>
</table>
<table> tag can only contain (in this particular order)
an optional <caption> element,
zero or more <colgroup> elements,
an optional <thead> element,
either one of the following:
zero or more <tbody> elements
one or more <tr> elements
an optional <tfoot> element
Try reordering your elements like this:
<form>
<table>
<tr>
...
</tr>
</table>
<input type="submit" value="Submit" class="btn btn-primary"/>
</form>

updating/deleting form data with php to update database using phpmyadmin

I am in the midst of completing a uni assignment and ive come across the issue of being totally stumped on how to update and delete data in my database through the use of a form.
I have successfully connected the database so that when the user selects the the addressID in the previous page they wish to work on, it will take them to the updateform.php page.
My code for the form is as follows:
<form method="post">
<tr>
<td>Firstline</td>
<td><input type="text" name="firstline" class="form-control"/></td>
</tr>
<tr>
<td>Secondline</td>
<td><input type="text" name="secondline" class="form-control"/></td>
</tr>
<tr>
<td>City</td>
<td><input type="text" name="city" class="form-control"/></td>
</tr>
<tr>
<td>State</td>
<td><input type="text" name="state" class="form-control"/></td>
</tr>
<tr>
<td>Zip</td>
<td><input type="text" name="zip" class="form-control"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Update" class="btn btn-success btn-lg"/></td>
</tr>
<tr>
<td></td>
<td><input type="delete" name="submit" value="Delete" class="btn btn-success btn-lg"/></td>
</tr>
I am now completely stumped on how to connect the data they enter in the form to update the database.
I am trying to update the addresses table with the selected addressID chosen earlier if that helps.
Any push in the correct direction will be greatly appeciated.
Kind Regards
Change these line:
<form method="post">
to
<form method="post" action="process.php">
and
<input type="submit" name="submit" value="Update" class="btn btn-success btn-lg"/>
to
<input type="submit" name="update" value="Update" class="btn btn-success btn-lg"/>
and
<input type="delete" name="submit" value="Delete" class="btn btn-success btn-lg"/>
to
<input type="delete" name="delete" value="Delete" class="btn btn-success btn-lg"/>
process.php:
if(isset($_REQUEST['update']))
{
// update block
// get all required value and fire update query
}
if(isset($_REQUEST['delete']))
{
// delete block
// get all required value and fire delete query
}
You could try something like this:
updateform.php
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
strip tags from user input:
$firstline= trim(strip_tags($_POST['first_line']));
$secondline= trim(strip_tags($_POST['second_line']));
create query:
$myquery = "INSERT INTO tablename (firstline, secondline)
VALUES ($firstline', '$secondline')";
execute query (confirm success/failure if/else):
if (#mysqli_query($database, $myquery)) {
print '<p>Entries accepted</p>';
}
else {
print '<p style="color: red;">Could not INSERT values because:<br />'
. mysqli_error($database) . '</p>;
}
}
?>
Then have your form post values for the query variables:
<form action="updateform.php" method="post">
<tr>
<td>Firstline</td>
<td><input type="text" name="firstline" size="20" value= <?php if (isset($_POST['firstline'])) {
print htmlspecialchars($_POST['first_line']);
} ?> /></td>
<tr>
<td>Secondline</td>
<td><input type="text" name="secondline" size="20" value= <?php if (isset($_POST['secondline'])) {
print htmlspecialchars($_POST['second_line']);
} ?> /></td>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Update" class="btn btn-success btn-lg"/></td>
</tr>
<tr>
<td></td>
<td><input type="delete" name="submit" value="Delete" class="btn btn-success btn-lg"/></td>

php:using two buttons inside same form and form submission

i am designing a login page using html and php.it includes two buttons one for login and another one to redirect to the registration page.i am planning to use php code to validate login data on the same page.here is my code
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<div>
<table style="width: 100%; height: 377px;">
<tr>
<td rowspan="4"><img src="woman.jpg" height="100%" width="100%" alt="woman"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td align="right" style="padding:0px;color:white" >username:</td>
<td><input runat="server" name="Text1" type="text" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td align="right" style="padding:0px;color:white">password</td>
<td><input runat="server" name="Text2" type="text" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td></td>
<td ><input name="Submit1" type="submit" value="login" />
<input onclick="document.location.href='regforswa.php'" name="Submit2" type="submit" value="register" /><br/>
forgot password</td>
</tr>
</table>
</div>
</form>
but the button for registration is not working properly.it is not redirecting to the registration page.i don't want to put register button outside the form.i want to keep the same design and achieve required functionality.
Change the Button-Type from type="submit" to type="button" to disable the form submission:
<input onclick="document.location.href='regforswa.php'" name="Submit2" type="button" value="register" />

Form that has multiple tbody and multiple hidden fields with same name sends only the last hidden field in the form

This form has multiple tbody in one form.
Each tbody contains "hidden" field with "unique" value.
However, when this form is sent, it only sends the last "pouzivatel_id" value.
e.g.
I want to execute "pouzivatel_id"=4, but the form sends "pouzivatel_id"="1"
How would I fix this without using multiple forms?
<form method="post" action="lib/action/admin.post.php">
<table>
<thead>
<tr>
<th>Osobné číslo</th>
<th>Prezývka</th>
<th>Meno</th>
<th>Priezvisko</th>
<th>Operácie</th>
</tr>
</thead>
<tbody>
<tr>
<td>00000000004</td>
<td>user4</td>
<td>user</td>
<td>user</td>
<td>
<input type="hidden" name="pouzivatel_id" value="4"/>
<button type="submit" name="ziadatel-schvalit" class="tlacidlo zelena">Schváliť</button>
<button type="submit" name="ziadatel-zmazat" class="tlacidlo cervena">Zamietnuť</button>
</td>
</tr>
</tbody>
<tbody>
<tr>
<td>00000000003</td>
<td>user3</td>
<td>user</td>
<td>user</td>
<td>
<input type="hidden" name="pouzivatel_id" value="3"/>
<button type="submit" name="ziadatel-schvalit" class="tlacidlo zelena">Schváliť</button>
<button type="submit" name="ziadatel-zmazat" class="tlacidlo cervena">Zamietnuť</button>
</td>
</tr>
</tbody>
<tbody>
<tr>
<td>00000000002</td>
<td>user2</td>
<td>user</td>
<td>user</td>
<td>
<input type="hidden" name="pouzivatel_id" value="2"/>
<button type="submit" name="ziadatel-schvalit" class="tlacidlo zelena">Schváliť</button>
<button type="submit" name="ziadatel-zmazat" class="tlacidlo cervena">Zamietnuť</button>
</td>
</tr>
</tbody>
<tbody>
<tr>
<td>00000000001</td>
<td>user1</td>
<td>user</td>
<td>user</td>
<td>
<input type="hidden" name="pouzivatel_id" value="1"/>
<button type="submit" name="ziadatel-schvalit" class="tlacidlo zelena">Schváliť</button>
<button type="submit" name="ziadatel-zmazat" class="tlacidlo cervena">Zamietnuť</button>
</td>
</tr>
</tbody>
</table>
You could use a different name for every submit button. If a submit button is pressed, only this submit button is sent to your PHP script. In your PHP script you check which button was pressed.
Example HTML code:
<input type="hidden" name="pouzivatel_id[]" value="2"/>
<button type="submit" name="ziadatel-schvalit_2" class="tlacidlo zelena">Schváliť</button>
<button type="submit" name="ziadatel-zmazat_2" class="tlacidlo cervena">Zamietnuť</button>
<input type="hidden" name="pouzivatel_id[]" value="1"/>
<button type="submit" name="ziadatel-schvalit_1" class="tlacidlo zelena">Schváliť</button>
<button type="submit" name="ziadatel-zmazat_1" class="tlacidlo cervena">Zamietnuť</button>
Example PHP code:
<?PHP
foreach($_POST['pouzivatel_id'] as $id)
{
if(isset($_POST['ziadatel-schvalit_' . $id]))
{
//Schváliť
}
elseif(isset($_POST['ziadatel-zmazat_' . $id]))
{
//Zamietnuť
}
}
?>

Categories