Passed data value will stay on page after submitting form - php

I have a 2 pages and I want to stay the data value that i passed from page1.php
page1.php
<form action='page2.php' method='POST' class='form-inline'>
<input type='submit' class='btn btn-info' value='Manage Child'
name='manage'>;
<input type='hidden' value='<?php echo $id;?>' name='id'>
</form>
page2.php
$id = $_POST['id'];
if(isset($_POST['passinfo'])){
$studentid=$_POST['studid'];
}else{
$studentid='';
}
<form>
<input type='hidden' name='parentid' value='<?php echo $id;?>'
<input type='hidden' name='studentid' value='<?php echo $studentid;?>'
<input type='text' name='studentname'>
</form>
<form action='' method='POST'>
<input type='hidden' name=studid value='<?php echo $stud_id?'>
<input type='submit' class='btn btn-primary' value='Assign' name='passinfo'>
</form>
my problem is if I click the 'passinfo' button and submit the 'studid' value the '$parentid' value will be unindentified variable. My question is how do I maintain the value of the '$parentid' even if I submit a form

You must place hidden inputs in one form:
<form action='' method='POST'>
<input type="hidden" name="parentid" value='<?=$id?>'>
<input type="hidden" name="studentid" value='<?=$studentid?>'>
<input type='hidden' name=studid value='<?=$stud_id?'>
<input type='submit' class='btn btn-primary' value='Assign' name='passinfo'>
</form>

Related

How to accelerate performance work with a lot of conditions in PHP?

I have a theoretical question. In one project (PHP) from companies, I have a registration form in which various options are selected on the basis of which contracts are proposed to the user.
For example:
If the user selects the "A" option, the script does something.
If the user selects "A" and "B" then the script does something different.
If the user selects "B", "C" and "Q" it does something else. (...) etc
In the script I have all the combinations for each condition listed, but every now and then a new condition appears, for example "Z". Then I have to add more and more code exponentially, which is taking me over 2 hours at the moment.
Do you have any ideas, hints on how to change it, improve something or implement something so as not to write so much code? How do you deal with such projects? I am looking for a semi - automation or acceleration of work :).
I'm sure there's a solution, but I probably don't know it.
Ok, if you need more details, below is the piece of script from a different, simpler part. Can it be improved in some way?
<?php
(...)
echo "<td>
<center><center> <form action='pobierz.php' method='post'>
<input type='hidden' name='typ_umowy' value='umowa_zlecenie_glovo'>
<input type='hidden' name='login' value='$login'>
<input type='submit' class='downloadbutton' value='Pobierz'> </form> </center>
</td>";
echo "<td>
<center> <form action='Start_usuwanie_nie_mam.php' method='post'>
<input type='hidden' name='typ_umowy' value='umowa_zlecenie_glovo'>
<input type='hidden' name='login' value='$login'>
<input type='hidden' name='ID' value='$id'>
<input type='submit' class='deletebutton' value='Usuń' onclick=\"return confirm('Czy na pewno chcesz usunąć tą umowę?')\"> </form> </center>
</td>";
echo "</tr>";
}
if(mysqli_num_rows($wynikzug) == 0) {
echo "<td> <center><form action='Start_Formularz_Umowa_Zlecenie_Glovo.php' method='post'>
<input type='hidden' name='login' value='$login'>
<input type='hidden' name='id' value='$id'>
<input type='submit' class='generatebutton' value='Generuj' disabled> </form> </center> </td>";
echo "<td>
<center><center> <form action='pobierz.php' method='post'>
<input type='hidden' name='typ_umowy' value='umowa_zlecenie_glovo'>
<input type='hidden' name='login' value='$login'>
<input type='submit' class='downloadbutton' value='Pobierz' disabled> </form> </center>
</td>";
echo "<td>
<center> <form action='Start_usuwanie_nie_mam.php' method='post'>
<input type='hidden' name='typ_umowy' value='umowa_zlecenie_glovo'>
<input type='hidden' name='login' value='$login'>
<input type='hidden' name='ID' value='$id'>
<input type='submit' class='deletebutton' value='Usuń' disabled onclick=\"return confirm('Czy na pewno chcesz usunąć tą umowę?')\"> </form> </center>
</td>";
echo "</tr>";
}
}
if(mysqli_num_rows($wynik5) == 0) {
echo "<tr>";
echo "<td>".'Umowa Zlecenie Glovo'."</td>";
echo "<td>".'Nie mam działalności gospodarczej'."</td>";
echo "<td><img src='unchecked1.png'></td>";
echo "<td> <center> <form action='Start_Formularz_Umowa_Zlecenie_Glovo.php' method='post'>
<input type='hidden' name='login' value='$login'>
<input type='hidden' name='id' value='$id'>
<input type='submit' class='generatebutton' value='Generuj'>
</form>
</center>
</td>";
echo "<td> <center> <form action='pobierz.php' method='post'>
<input type='hidden' name='typ_umowy' value='umowa_zlecenie_glovo'>
<input type='hidden' name='login' value='$login'>
<input type='submit' class='downloadbutton' value='Pobierz' disabled> </form> </center>
</td>";
echo "<td>
<center> <form action='Start_usuwanie_nie_mam.php' method='post'>
<input type='hidden' name='typ_umowy' value='umowa_zlecenie_glovo'>
<input type='hidden' name='login' value='$login'>
<input type='hidden' name='ID' value='$id'>
<input type='submit' class='deletebutton' value='Usuń' disabled> </form> </center>
</td>";
echo "</tr>";
}
$wynik3 = mysqli_query($con,"SELECT * FROM example_usermeta where user_id = '$id' and meta_key = 'umowa' and Description='umowa_najmu' ");
if(mysqli_num_rows($wynik3) > 0) {
echo "<tr>";
echo "<td>".'Umowa Najmu Samochodu'."</td>";
echo "<td>".'Nie mam działalności gospodarczej'."</td>";
echo "<td><img src='checked1.png'></td>";
$wyniknu = mysqli_query($con,"SELECT * FROM example_dokumenty where Login = '$login' and Typ_Umowy='umowa_najmu'");
if(mysqli_num_rows($wyniknu) > 0) {
echo "<td> <center> <form action='Start_formularz_umowa_najem.php' method='post'>
<input type='hidden' name='login' value='$login'>
<input type='hidden' name='id' value='$id'>
<input type='submit' class='generatebutton' value='Generuj' disabled> </form> </center> </td>";
echo "<td><center> <form action='pobierz.php' method='post'>
<input type='hidden' name='typ_umowy' value='umowa_najmu'>
<input type='hidden' name='login' value='$login'>
<input type='submit' class='downloadbutton' value='Pobierz'> </form> </center> </td>";
echo "<td>
<center> <form action='Start_usuwanie_nie_mam.php' method='post'>
<input type='hidden' name='typ_umowy' value='umowa_najmu'>
<input type='hidden' name='login' value='$login'>
<input type='hidden' name='ID' value='$id'>
<input type='submit' class='deletebutton' value='Usuń' onclick=\"return confirm('Czy na pewno chcesz usunąć tą umowę?')\"> </form> </center>
</td>";
echo "</tr>";
}
if(mysqli_num_rows($wyniknu) == 0) {
echo "<td> <center> <form action='Start_formularz_umowa_najem.php' method='post'>
<input type='hidden' name='login' value='$login'>
<input type='hidden' name='id' value='$id'>
<input type='submit' class='generatebutton' value='Generuj' disabled> </form> </center> </td>";
echo "<td><center> <form action='pobierz.php' method='post'>
<input type='hidden' name='typ_umowy' value='umowa_najmu'>
<input type='hidden' name='login' value='$login'>
<input type='submit' class='downloadbutton' value='Pobierz' disabled> </form> </center> </td>";
echo "<td>
<center> <form action='Start_usuwanie_nie_mam.php' method='post'>
<input type='hidden' name='typ_umowy' value='umowa_najmu'>
<input type='hidden' name='login' value='$login'>
<input type='hidden' name='ID' value='$id'>
<input type='submit' class='deletebutton' value='Usuń' disabled onclick=\"return confirm('Czy na pewno chcesz usunąć tą umowę?')\"> </form> </center>
</td>";
echo "</tr>";
}
}
if(mysqli_num_rows($wynik3) == 0)
{
echo "<tr>";
echo "<td>".'Umowa Najmu Samochodu'."</td>";
echo "<td>".'Nie mam działalności gospodarczej'."</td>";
echo "<td><img src='unchecked1.png'></td>";
echo "<td><center><form action='Start_formularz_umowa_najem.php' method='post'>
<input type='hidden' name='login' value='$login'>
<input type='hidden' name='id' value='$id'>
<input type='submit' class='generatebutton' value='Generuj'> </form> </center> </td>";
echo "<td> <center> <form action='pobierz.php' method='post'>
<input type='hidden' name='typ_umowy' value='umowa_najmu'>
<input type='hidden' name='login' value='$login'>
<input type='submit' class='downloadbutton' value='Pobierz' disabled> </form> </center> </td>";
echo "<td>
<center> <form action='Start_usuwanie_nie_mam.php' method='post'>
<input type='hidden' name='typ_umowy' value='umowa_najmu'>
<input type='hidden' name='login' value='$login'>
<input type='hidden' name='ID' value='$id'>
<input type='submit' class='deletebutton' value='Usuń' disabled> </form> </center>
</td>";
echo "</tr>";
}
$wynik4 = mysqli_query($con,"SELECT * FROM example_usermeta where user_id = '$id' and meta_key = 'umowa' and Description='oświadczenie_zleceniobiorcy' ");
if(mysqli_num_rows($wynik4) > 0) {
echo "<tr>";
echo "<td>".'Oświadczenie Zleceniobiorcy'."</td>";
echo "<td>".'Nie mam działalności gospodarczej'."</td>";
echo "<td><img src='checked1.png'></td>";
$wynikoz = mysqli_query($con,"SELECT ID FROM example_dokumenty where Login = '$login' and Typ_Umowy='oświadczenie_zleceniobiorcy'");
if(mysqli_num_rows($wynikoz) > 0) {
echo "<td><center> <form action='Start_formularz_oświadczenie_zleceniobiorcy.php' method='post'>
<input type='hidden' name='login' value='$login'>
<input type='hidden' name='id' value='$id'>
<input type='submit' class='generatebutton' value='Generuj' disabled> </form></center> </td>";
echo "<td> <center> <form action='pobierz.php' method='post'>
<input type='hidden' name='typ_umowy' value='oświadczenie_zleceniobiorcy'>
<input type='hidden' name='login' value='$login'>
<input type='submit' class='downloadbutton' value='Pobierz'> </form> </center> </td>";
echo "<td>
<center> <form action='Start_usuwanie_nie_mam.php' method='post'>
<input type='hidden' name='typ_umowy' value='oświadczenie_zleceniobiorcy'>
<input type='hidden' name='login' value='$login'>
<input type='hidden' name='ID' value='$id'>
<input type='submit' class='deletebutton' value='Usuń' onclick=\"return confirm('Czy na pewno chcesz usunąć tą umowę?')\"> </form> </center>
</td>";
echo "</tr>";
}
if(mysqli_num_rows($wynikoz) == 0) {
echo "<td><center> <form action='Start_formularz_oświadczenie_zleceniobiorcy.php' method='post'>
<input type='hidden' name='login' value='$login'>
<input type='hidden' name='id' value='$id'>
<input type='submit' class='generatebutton' value='Generuj' disabled> </form></center> </td>";
echo "<td> <center> <form action='pobierz.php' method='post'>
<input type='hidden' name='typ_umowy' value='oświadczenie_zleceniobiorcy'>
<input type='hidden' name='login' value='$login'>
<input type='submit' class='downloadbutton' value='Pobierz' disabled> </form> </center> </td>";
etc etc ...

hidden values are not passing in form action

When I save this form all type="text" fields are working fine. But hidden field values are not passing.
If I changed from "hidden" to "text" it works fine. I don't know why I am getting this problem.
<form action="../model/meter-reading-model" method="POST">
<input type='hidden' name='hdnTakenDate' id='hdnTakenDate' value='<?php echo $TakenDate; ?>' />
<input type='hidden' name='hdnSiteHeadDbKey' id='hdnSiteHeadDbKey' value='<?php echo $SiteHeadDbKey; ?>' />
<button type='submit' name="btnSaveData" id="btnSaveData" class='btn btn-lg btn-danger'> Save Reading Data </button>
</form>
Receiving in another page
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if(isset($SaveData)){
$TakenDate = mysqli_real_escape_string($conn, $_POST['hdnTakenDate']);
$SiteHeadDbKey = mysqli_real_escape_string($conn, $_POST['hdnSiteHeadDbKey']);
}
}
It solved for me when i change this
<input type='hidden' name='hdnTakenDate' id='hdnTakenDate' value='<?php echo $TakenDate; ?>' />
<input type='hidden' name='hdnSiteHeadDbKey' id='hdnSiteHeadDbKey' value='<?php echo $SiteHeadDbKey; ?>' />
to this
<input type='hidden' name='hdnTakenDate' id='hdnTakenDate' value='<?php echo $TakenDate; ?>' **/>**
<input type='hidden' name='hdnSiteHeadDbKey' id='hdnSiteHeadDbKey' value='<?php echo $SiteHeadDbKey; ?>' **/>**

PHP: How to use method GET, if action already contains GET value

I have a form, something like this:
<form action='index.php?page=search&filter=1'>
<input type='text' name='ID'>
<input type='text' name='number'>
<input type='submit'>
</form>
If I submit form, it will go to page:
index.php?ID=value1&number=value2
I would like to have
index.php?page=search&filter=1&ID=value1&number=value2
How can I append extra fields to url ?
Thank you for help.
you should put these values in hidden inputs:
<form action='index.php'>
<input type='hidden' name='page' value='search'>
<input type='hidden' name='filter' value='1'>
<input type='text' name='ID'>
<input type='text' name='number'>
<input type='submit'>
</form>
You can submit additional GET value as a hidden field value in the form which will be submit along with form submission.
<form action="index.php" method="get">
<input type='text' name='ID'>
<input type='text' name='number'>
<input type='hidden' name='page' value='search'>
<input type='hidden' name='filter' value='1'>
<input type='submit'>
</form>
Use hidden fields with your form and you will get exact url you want.
Try below code :
<form action="index.php" method="get">
<input type='hidden' name='page' value='search'>
<input type='hidden' name='filter' value='1'>
<input type='text' name='ID'>
<input type='text' name='number'>
<input type='submit'>
</form>
Try using some hidden input fields and use get method in form submitting.
Try this:
<form action="index.php" method="GET">
<input type='hidden' name='page' value='search' />
<input type='hidden' name='filter' value='1' />
<input type='text' name='ID' value='value1' />
<input type='text' name='number' value='value2' />
<input type='submit' value='Submit' />
</form>

Sending php in a name value which is an html form

<form action='itemEdit.php' method='post' />
<input type='hidden' name='<?php $row['id'];?>' value='' />
<input type='submit' name='submit' value='Edit'
</form>
is it possible to send this php script to the 'itemEdit.php' page and put the row output into a php variable? I've tried this and haven't had any success. I can't figure this out by just using mySql. Thanks
You should do the following..
<form action='itemEdit.php' method='post' />
<input type='hidden' name="YouFieldNameHERE" value="<?php $row['id'];?>" />
<input type='submit' name='submit' value='Edit'/>
</form>
Then in your itemEdit.php you can access it like ...
$fieldValue = $_POST['YouFieldNameHERE'];
You need to use the name attribute to indicate what value you are passing and either use echo or a shorttag or no value will be in the name attribute:
<input type='hidden' name='id' value='<?= htmlentities($row['id']) ?>'>
<input type='hidden' name='id' value='<?php echo htmlentities($row['id']) ?>'>
If you are naming your attribute with the id, it would look like this:
<input type='hidden' name='<?= htmlentities($row['id']) ?>' value=''>
<input type='hidden' name='<?php echo htmlentities($row['id']) ?>' value=''>
The htmlentities helps protect against XSS.
Also be sure to close the submit tag.
If it is HTML5, you don't need the />
two thing one use value for not name for input value.
another use echo or <?= to print value in input
instead of this
<input type='hidden' name='<?php $row['id'];?>' value='' />
use
<input type='hidden' name='inputname' value='<?php echo $row['id'];?>' />
to get value in next page
echo $_POST['inputname'];

When set value of action, post/get don't work

I have form:
echo "
<form action='http://domain.com/' method='get'>
<input type='hidden' name='order_id' value='".$_SESSION['shoppingcart_id']."' />
<input type='hidden' name='account_id' value='".$account_id."' />
<input type='hidden' name='action' value='add' />
<input type='submit' class='form-button' value='Buy' />
</form>
";
and get don't get any results.
http://domain.com/
If I set value of action to empty like:
<input type='hidden' name='action' value='' />
the result is:
http://domain.com/?order_id=xxxxxxxx&acount_id=xxxxxxxxx$action=
I'm confused why with empty action get results, but with action don't get anything!?
p.s. With "post" is the same.

Categories