Pass value from form to alter file PHP without POST - php

I´m trying to pass ALTER a value from my database with a form.
Because of the POST action, I putted the n_processo (primarykey) on the form so the POST can pass it to the file and edit the table.
I don´t want this value to appear on the table (Form), because it´s readonly, so that the POST can see it and edit the values.
Does anyone know how to change that?
Form file:
<form action="alterar_aluno3.php" method="POST">
<table>
<tr>
<th width="15%">Nº de Aluno</th>
<td><input type="text" maxlength="5" class="input-field4" name="teste" readonly value="<?php echo $idaluno;?>"/></td>
</tr>
<tr>
<th width="20%">Pessoas com quem vive:</td>
<td><input type="text" class="input-field4" name="agregado_existente" value="<?php echo $agregado_existente;?>"/></td>
</tr>
</table>
<p align=right>
<!--alterar este botao -->
<button type="submit" value="Alterar">Alterar</button>
<button type="cancel" onclick="window.location='http://donutsrool.pt/ficha_aluno.php';return false;">Cancelar</button>
</p>
</form>
Insert file:
<?php
include "functions.php";
session_start();
//captar os dados recebidos do formulário com o método POST
$idaluno1=$_POST['teste'];
$agregado_existente = $_POST['agregado_existente'];
$altera="UPDATE aluno SET `agregado_existente`='$agregado_existente' WHERE `n_processo`=$idaluno1;";
$resultado =DBExecute($altera);
header("Location: ficha_aluno.php");
?>

You can put id in a hidden field instead of text field.
<input type="hidden" maxlength="5" class="input-field4" name="teste" value="<?php echo $idaluno;?>"/>
Hidden fields work the same as of text fields.
Their value can only be set with PHP on page load or through JavaScript events.
Only difference is that they are not visible on browser.
Though, they can be seen in View Source of the page.

Try this:
<?php
include "functions.php";
session_start();
//captar os dados recebidos do formulário com o método POST
$idaluno1=$_POST['teste'];
$agregado_existente = $_POST['agregado_existente'];
$altera="UPDATE aluno SET `agregado_existente`='$agregado_existente' WHERE `n_processo`='$idaluno1'";
$resultado =DBExecute($altera);
header("Location: ficha_aluno.php");
?>

Related

Retrieve Data from data base to html form on button CLICK

I am actually starting my newest codes with HTML/PHP .
I am searching for retrieving data (list of persons) from mysql Data Base, displaying it into html table, then when I will clik on button "edit" it will show me in another page the details of the selected person like this :
It works fine for all the rows expects the firt row of the table.
Any help please !!!
there is my code :
<table border = 1>
<caption> Liste des personnes </caption>
<tr>
<th>id </th>
<th>nom</th>
<th>prenom</th>
<th>date Naissance</th>
<th>sexe</th>
<th>ville</th>
<th>comptence</th>
<th>photo</th>
</tr>
<?php while ($obj = mysqli_fetch_object($result)){ ?>
<tr>
<td> <?= $obj->id ?> </td>
<td><?= $obj->nom?></td>
<td><?= $obj->prenom?></td>
<td><?= $obj->dateNaissance?></td>
<td><?= $obj->sexe?></td>
<td><?= $obj->ville?></td>
<td><?= $obj->competence?></td>
<?php if (isset($obj->photo)) {?>
<td><img src="uploads/<?= $obj->photo?>" width =20 height = 20 >
<?php } ?>
<td>
<form name="editPerson" action="edit.php" method="POST">
<input type="hidden" name="id" value="<?= $obj->id ?>">
<input type="submit" name="editer" value="Edit">
</form>
</td>
</tr>
<?php } ?>
</table>
Explainations
You can't have an action='edit.php' as you will edit a specific user, not all of them. You need to specify in your action what user you want to edit. And it is mostly done with the ID of the user. So you action will look like this action='edit.php?id=1. And so, your method would be GET.
In your edit.php, you will have a $_GET['id'] variable that will contain the ID of the user to be edited. So you will have to first create a new query to search for this specific user.
You can then proceed on preparing the query.
$query = $connexion->prepare('SELECT * FROM users WHERE id = :id');
And then, executing the query with the id from the URI.
$query->execute(['id' => $_GET['id']]);
And cast the result to a variable to get the user.
Then, all you have to do in your page is a little bit of refactoring from your previous page. Meaning that there will be now a big <form> tag surrounding your <table> tag and the <td> tag will now contain <input value='<?php $user->id; ?>'> tag for example for the ID. And your edit buttons will now be a save button.
The method of the <form> in your edit?id=1 would be a POST method ot itself. So in the same page, you will be able to update the user. You can also cast the form to another page, like saveUser.php. Just be consistent from one solution to another in all your project.
<form method='POST' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
Use this Code
<input type="submit" name="editer" value="Edit">
Instead of
<form name="editPerson" action="edit.php" method="POST">
<input type="hidden" name="id" value="<?= $obj->id ?>">
<input type="submit" name="editer" value="Edit">
</form>
And get the id value on edit.php file by using $_GET['id'].
Example for edit.php file:
$id = $_GET['id']

How to memorize a radio selection (twice)

this is probaly an easy one, but I just cant seem to figure it out. I've tried googling for this aswell, but without any luck to my particular problem...
What I want, is that the radio selection gets remembered two times (kinda), it remembers after the first time I click submit. But when I click submit again on my next page, it wont remember the value.
Well, I want all the information stored in my database pretty much..
Thanks!
EDIT 1: Oh yeah, the thing that does not go into my database is "valgt_skap" or in other words "radios", everthing else works fine.
Bokssvar.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="style2.css?<?php echo time(); ?>" />
<title>Registrering</title>
</head>
<body>
<?php
if(isset($_SESSION['boxfeil'])) echo $_SESSION['boxfeil'];
unset($_SESSION['boxfeil']);
?>
<form action="bestilt.php" method="post" name="inputform_Field">
<table id="valgt_skap_tabell" class="bokssvartabell">
<tr>
<td>Valgt skap</td>
</tr>
<tr>
<td>
<input class="bokssvarskjema" type="text" name="valgt_skap" disabled value= <?php
if(isset(($_POST['radios']))){
echo ($_POST['radios']);
} else {
header('location: index.php');
} ?>>
</td>
</tr>
</table>
<table id="opplysninger_tabell" class="bokssvartabell">
<tr>
<td>Fornavn:</td>
<td>Etternavn:</td>
<td>Telefon:</td>
<td>E-post:</td>
<td>Elev Nummer:</td>
</tr>
<tr>
<td><input type="text" name="Fornavn_nm" id="fornavn_check"></td>
<td><input type="text" name="Etternavn_nm" id="etternavn_check"></td>
<td><input type="text" name="Telefon_nm" id="telefon_check" maxlength=8></td>
<td><input type="text" name="E-post_nm" id="epost_check"></td>
<td><input type="text" name="Elevnummer_nm" id="elevnr_check"></td>
</tr>
</table>
<div style="text-align:center;">
<button id="bestill_skap" type="submit" name="bestill_Skap">Bestill skap</button>
</div>
</form>
</body>
bestilt.php
<?php
require 'connectdb.php';
$inputFornavn_check = $_POST['Fornavn_nm'];
$inputEtternavn_check = $_POST['Etternavn_nm'];
$inputTelefon_check = $_POST['Telefon_nm'];
$inputEpost_check = $_POST['E-post_nm'];
$inputElevnr_check = $_POST['Elevnummer_nm'];
$inputSkap_check = $_POST['valgt_skap'];
$insertInfo_query = "INSERT INTO elever (Fornavn, Etternavn, Telefon, Epost, ElevNr, Skap)
VALUES ('$inputFornavn_check' , '$inputEtternavn_check' , '$inputTelefon_check' , '$inputEpost_check' , '$inputElevnr_check' , '$inputSkap_check')";
$connect_DB->query($insertInfo_query);
?>
Try using sessions to store the value. First use session_start(), then store in $_session['fieldname']=value. Then you can use it in the preceding pages.
On the second page, receive the value and put it on a hidden form element.
<form>
...
<input type="hidden" name="valgt_skap" value="$radioValue">
...
</form>
This element is not shown on the page, although it's present and submited with the form.

PHP sessions do not pass variables to the other page

I know that if i want to pass variables in php from a page to another one I can use sessions, but I don't know what is wrong with this code, it's just doesn't work,
I want to pass the $cout variable from page 1 to page 2
PAGE 1
<?php
session_start();
if(isset($_POST['date']))
{
$dat = $_POST['date'];
$matricule = $_POST['matricule'];
$kilometrage = $_POST['kilometrage'];
$num_fact = $_POST['num_fact'];
$Fournisseur = $_POST['Fournisseur'];
$num_bon = $_POST['num_bon'];
$Fonctionnaire = $_POST['Fonctionnaire'];
$cout = $_POST['cout'];
//here is all what I did
$_SESSION['cout'] = $_POST['cout'];
header("Location: PAGE2.php") ;
}
?>
and here is PAGE 2:
<?php
session_start();
$cout = $_SESSION['cout'];
echo $cout ;?>
and here is the form
<form method="post" action="page1.php" >
<input type="date" placeholder="La Date Ex: 2014-07-17" name="date"><br>
<input type="text" placeholder="Matricule" name="matricule"><br>
<input type="text" placeholder="Kilometrage" name="kilometrage"><br>
<input type="text" placeholder="Numero de facteur" name="num_fact"><br>
<input type="text" placeholder="Fournisseur" name="Fournisseur" ><br>
<input type="text" placeholder="N° de bon à delivrer au fornisseurs ou facture" name="num_bon"><br>
<input type="text" placeholder="Fonctionnaire ayant effectué la Réparation" name="Fonctionnaire" ><br>
<input type="text" placeholder="le coût de la reparation en DH" name="cout" ><br>
<input type="submit" value="Valider">
</form>`
Your code seems correct, but dependant on the existence of $_POST['date'] variable.
When you submit the form - please make sure that it is not empty.
EDIT2:
I have made two files on my own, your code works fine.
You'll need to look for error on part of server php config, or pathways.
Try following: (assuming page1.php and page2.php are in root folder of your site)
in html
<form action="/page1.php" ..
in page1
header("Location: /page2.php");
Try isset($_POST['submit']) in place of isset($_POST['date']). I think you are not filling date.
May be your temp folder is write protected. Check session_save_path() is writable or not.

Retrieve a variable whos name is has another variable in it

Dunno if the title makes sense, but I have a variable which would to put it in basic terms would be called like this:
$_POST['something'+$variable2]
I have a form which is for editing selected records, this form contains entries for all previously selected records:
<form name="input" action="editcar.php" method="POST">
<input type="submit" value="Yes">
while($row = mysqli_fetch_assoc($result))
{
echo'
</div>
<table style="color:white">
<tr>
<td style="text-align:right">Manufacture:</td><td><input type="text" name="manufacture'.$row['carIndex'].'" value="'.$row['make'].'"></td>
<td style="text-align:right">Model: </td><td><input type="text" name="model'.$row['carIndex'].'" value="'.$row['model'].'"></td>
</tr>
<tr>
<td style="text-align:right">Colour: </td><td><input type="text" name="colour'.$row['carIndex'].'" value="'.$row['colour'].'"></td>
<td style="text-align:right">Reg: </td><td><input type="text" name="reg'.$row['carIndex'].'" value="'.$row['Reg'].'"></td>
</tr>
<tr>
<td style="text-align:right">Price: </td><td><input type="text" name="price'.$row['carIndex'].'" value="'.$row['price'].'"></td>
<td style="text-align:right">Mileage: </td><td><input type="text" name="mileage'.$row['carIndex'].'" value="'.$row['miles'].'"></td>
</tr>
<tr>
<td style="text-align:right">Max MPH: </td><td><input type="text" name="mph'.$row['carIndex'].'" value="'.$row['mph'].'"></td>
<td style="text-align:right">MPG: </td><td><input type="text" name="mpg'.$row['carIndex'].'" value="'.$row['mpg'].'"></td>
</tr>
</table>
</form>
</div> ';
}
?>
</form>
The form is looped for each record previously chosen, to enable mass editing. The isue arouses when I realised I'd have multiple inputs with the same name, so I did:
<input type="text" name="model'.$row['carIndex'].'" value="'.$row['model'].'">
Placing the primary key of the record it was currently tired to on the end of it's name. Which seemed like a logical way to go about things.
However now I need to call these variables to place in the mysql query and I dunno how to do that, or even if I can.
I have the selected records saved in an array so I have:
foreach ($postid as $carID)
{
$query = "stuff";
mysqli_query($db, $query);
}
Each loop has $carID containing the variables that was put on the end of the form input names.
So something like:
$_POST['something'+$variable2]
is all I can think of but doesn't work.
Any method that works for my overall code is welcome not just a solution to the issue I've made.
Actually your way should work. Just replace the + with . in $_POST['something'+$variable2].
My tip is: use an array as name in your html instead:
<input type="text" name="model[]" value="'.$row['model'].'">
On php-Side you can loop through all $_POST['model'] since its an array now.
You can add the index for every entry in your html, too:
<input type="text" name="model['.$row['carIndex'].']" value="'.$row['model'].'">
PHP uses a dot for concatenation, not + like Java and Javascript:
$_POST['something' . $variable2]
Try something like this:
<form ...>
<?php
while($row = mysqli_fetch_assoc(...):
$index = $row['carIndex'];
?>
<input type="text" name="carmodel[<?php echo $index?>][model]" value="<?php echo $row['model'] ?>">
<?php endforeach; ?>
</form>
This way you will have the data stored in $_POST['carmodel'] as an array indexed by carIndex value as the structure of data in $_POST is defined by names of inputs, here you will have names likee carmodel[1][model] for example so then in post it will be in $_POST['carmodel'][1][model]
you can read here as well
How would I create this array structure in an HTML form?

Form won't post select value in IE9

All,
I've got the following page:
http://tinyurl.com/7lzr6qo
The original form code is:
<form id="dj_feedback" name="dj_feedback" action="../test-save-feedback" method="post">
<br>
<h2>How was the Event?</h2>
<hr>
<table><tr><td width="100px" valign="middle">
<b>Person<font color="#FF0000"> * </font></b>
</td><td valign="middle">
<select name="dj_name" id="dj_name">
<option value="original"></option>
<?php
$qrydj = "Select user_id, first_name, last_name from users where role='employee'";
$resultdj = mysql_query($qrydj);
while($resultsetdj = mysql_fetch_array($resultdj)){
?>
<option value="<?php echo $resultsetdj['user_id']; ?>"><?php echo $resultsetdj['first_name']." ".$resultsetdj['last_name']; ?></option>
<?php
}
?>
</select></td>
</tr>
</table>
<button class="submit" name="submit_feedback" value="submit_feedback">Get Paid!</button>
</form>
When I view the dropdown source it has a value on the select and then on my second page all I have is a simple echo statement to view what was passed like this:
<?php
session_start();
$user_id = $_POST['dj_name'];
echo "The user id is: ".$user_id;
?>
However, when I post this the value selected in the dropdown menu doesn't get passed. In other browsers this works perfectly fine however.
Does anyone have any idea why this isn't working properly?
Thanks!
You may need to make IE know the button element is in fact submitting the form - Perhaps edit your button:
<button type="submit" class="submit" name="submit_feedback" value="submit_feedback">Get Paid!</button>
Note that the type attribute is a valid one:
http://www.w3schools.com/tags/tag_button.asp
and html5:
http://www.w3schools.com/html5/tag_button.asp

Categories