I am trying to fetch some form data but something interrupts,
<?php
if(isset($_POST['submit'])){
$data = $_POST;
}
?>
<form action = "page.php" method="post">
Username<input type="username" name="username"/><br/>
Password<input type="text" name="password"/><br/>
<input type="submit" name="submit"/>
</form>
okay, the above outputs to
Array ( [username] => '' [password] => '' [email] => '' [submit] => Submit )
Okay, if i am to send this into database. i should be sending it without the key submit and value submit. How do you do it?
Unset it.
if ( isset($_POST['submit']) ) {
unset($_POST['submit']);
}
A better practice would be to save it to a new variable and make changes there.
Related
I'm searching a way to create arrays from a form and transfer it to another page:
This is my actual code :
<?php
$nba = $_GET["nbadultes"]; // recup number of passangers
$adu = array();
for ($i=1;$i<=$nba;$i++)
{
echo '<h3>Passager '.$i.'</h3>
<label>CIVILITÉ</label>
<select class="full-width" name="Civilite">
<option value="Mr" selected >Mr.</option>
<option value="Mrs" >Mrs.</option>
</select>
<label>FIRSTNAME *</label>
<input type="text" name="FIRSTNAME"/>
<label>LASTNAME *</label>
<input type="text" name="LASTNAME"/> ';
$adu[$i][civilite] = $_POST['civilite'];
$adu[$i][FIRSTNAME] = $_POST['FIRSTNAME'];
$adu[$i][LASTNAME] = $_POST['LASTNAME'];
}
$_SESSION['liste_adultes'] =$adu;
?>
And this code in the next page to read the array :
<?php
if (isset($_SESSION['liste_adultes']))
print_r ($liste_adultes);
?>
But I don't know why the array is empty :
Array
(
[1] => Array
(
[Civilite] =>
[FIRSTNAME ] =>
[LASTNAME] =>
)
[2] => Array
(
[Civilite] =>
[FIRSTNAME ] =>
[LASTNAME] =>
)
)
You need to take care of the form inputs you send
As I can see, right now the form is not submitted, so you don't need a session but a form which have set the action to a page that handles the sent data. Something like:
<form method="post" action="my-action-file.php">
<?php
$nba = $_GET["nbadultes"];
for ($i = 1; $i <= $nba; $i++) {
echo '<h3>Passager '.$i.'</h3>
<label>CIVILITÉ</label>
<select class="full-width" name="Civilite[]">
<option value="Mr" selected >Mr.</option>
<option value="Mrs" >Mrs.</option>
</select>
<label>FIRSTNAME *</label>
<input type="text" name="FIRSTNAME[]"/>
<label>LASTNAME *</label>
<input type="text" name="LASTNAME[]"/> ';
}
?>
<input type="submit" value="Submit" />
</form>
In the my-action-file.php file you can get the $_POST variables as:
<?php
foreach ($_POST['Civilite'] as $key => $civilite) {
$firstName = $_POST['FIRSTNAME'][$key];
$lastName = $_POST['LASTNAME'][$key];
// now you have the sent data.. just use it like you need..
}
// of course you can add these data to an array if you like so
In the second page you need to read the session data, then read the specific value you saved in the session into the variable you used:
<?php
if(!isset($_SESSION)){ session_start();}
if(isset($_SESSION['liste_adultes'])){
$liste_adultes = $_SESSION['liste_adultes'];
print_r($liste_adultes);
}
?>
Also, in the first page, make sure you post data and read it with $_POST; or via query string with $_GET
I'm making a very simple stock control system. In the page that the user will be able to do the updates, I'm listing all the contents in the table and putting everything in text fields, like the image.
So, the user should just change what he wants and when he clicks on submit, it would update every row in the table, with its respective value. But my code isn't working. I don't know how to make this kind of update and I'm sure it's wrong, because it doesn't even work. Here my form:
<form action="update_pcs.php" method="POST">
<label>Peça</label>
<label for="txtQtd" id="qtd">Qtd.</label>
<br/>
<?php foreach($rtn as $pcs){ ?>
<input type="text" name="txtNome[]" id="txtNome" value="<?=$pcs['pc_nome']?>" />
<input type="text" name="txtQtd[]" id="txtQtd" value="<?=$pcs['num']?>"/>
<input type="hidden" name="txtId[]" id="txtId" value="<?=$pcs['id']?>" />
<br />
<br />
<?php } ?>
<br />
<br />
<input type="submit" value="Enviar" name="btnEnvia" />
</form>
And my file update_pcs.php, which should do the update in the table.
<?php
include_once 'mdl.php';
$conexao = new modelDB();
$qtd = $_POST['txtQtd'];
$nom = $_POST['txtNome'];
$id = $_POST['txtId'];
/*This makes the $dados array keep in the value ['nome'] another array, the value ['qtd'] another array and ['id'] another array*/
$dados = array('nome'=>$nom,
'qtd'=>$qtd,
'id'=>$id);
foreach($dados as $dado){
/* I'm doing it that way but it isn't working */
$nomeAt = $dado['nome'];
$qtdAt = $dado['qtd'];
$id = $dado['id'];
$conexao->alteraDb("update pcs_estq set pc_nome ='{$nomeAt}', num = '{$qtdAt}' where id = '{$idAt}'");
}
My function must be right because when I change the php variables for values, it works. How could I make this right?
With sending array names in your html form, you will receive array post variables.
First change your form like below to help posting more appropriate data.
<input type="text" name="values[<?=$pcs['id']?>][Nome]" id="txtNome" value="<?=$pcs['pc_nome']?>" />
<input type="text" name="values[<?=$pcs['id']?>][Qtd]" id="txtQtd" value="<?=$pcs['num']?>"/>
<input type="hidden" name="values[<?=$pcs['id']?>][id]" id="txtId" value="<?=$pcs['id']?>" />
Actually this way you don't need to send id but i am sending for simple explanation.
Now you post, you'll receive an array like :
values => array (
1 => array (
'id'=> 1,
'Nome' => 'Some text',
'Qtd' => 1
),
2 => ....
)
Now you can get your data and insert your db.
$values = $_POST['values'];
foreach ($values as $dado) {
$nomeAt = $dado['Nome'];
$qtdAt = $dado['Qtd'];
$id = $dado['id'];
By the way, i strongly recommend using pdo, if not please make sure you validate your data before passing database query.
I've designed one HTML form as follows :
<form action="sample_test.php" method="post">
<input type="text" name="fileName" value="8.png" id="fileName[]">
<input type="text" name="fileLink" value="https://www.filepicker.io/api/file/zZ993JyCT9KafUtXAzYd" id="fileLink[]">
<input type="text" name="fileName" value="2_OnClick_OK.jpg" id="fileName[]">
<input type="text" name="fileLink" value="https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ" id="fileLink[]">
<input type="submit" name="Submit" value="Submit File">
</form>
Then the code in sample_test.php is as follows :
<?php
print_r($_POST); die;
?>
The output I got is as follows :
Array ( [fileName] => 2_OnClick_OK.jpg [fileLink] => https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ [Submit] => Submit File )
But this is not the desired output. I want the desired output array to be printed in following manner:
Array
(
[8.png] => Array
(
[0] => https://www.filepicker.io/api/file/zZ993JyCT9KafUtXAzYd
)
[2_OnClick_OK.jpg]
(
[0] => https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ
)
)
For now I've just demonstrated with two elements only but in real situations hundreds of such elements could present on the form.
So what changes do I need to make in my HTML as well as PHP code? Please help me.
Thanks in advance.
What you ask is impossible by just modifying the HTML code, because you would like a value (of fileName) to become an index in the array you get. That's impossible, the index will always be the name of the input.
However, if you have a look here : POSTing Form Fields with same Name Attribute , you will be able to get arrays of fileName and fileLink, and I'm pretty sure you can do something from there.
A few things wrong, but you are close. Make the name field an array instead of the id - plus your ids need to be unique.
<input type="text" name="fileName[]" value="8.png" id="fileName1">
<input type="text" name="fileLink[]" value="https://www.filepicker.io/api/file/zZ993JyCT9KafUtXAzYd" id="fileLink1">
<input type="text" name="fileName[]" value="2_OnClick_OK.jpg" id="fileName2">
<input type="text" name="fileLink[]" value="https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ" id="fileLink2">
Not tested, but should do the trick.
Is there any solution for this? Sorry if I didn't explain this good.
When I put some value in $_POST['name'] and submit form I want to put that value in array( $arr ) and when I put some diferent value second time in $_POST['name'] and submit form I want that value put in array ( $arr ) to. Now that array should have two element ( first value and second value from $_POST['name'] )
<?php
session_start();
$_SESSION['name'] = 1;
$arr =array();
if(isset($_POST['submit']) && trim($_POST['name']) != '') {
$arr[] = $_POST['name'];
}
print_r($arr);
?>
<form action="" method="post">
<input type="text" name="name" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
I started session, chacked if is form submitted and if is an empty field.
I tried with hidden input with value of $token = rand(); adn it not worked.
This should be easy, but I have some trouble do fix it.Tnx!
PHP is a stateless language and doesn't store variables from one page load to another. Each time you are submitting the form the $arr variable is destroyed from memory when the page has been processed.
If you want to keep information across page loads then you need to store it in the super global variable $_SESSION as a key value pair.
If you use the modified code below it will store the value typed into the name input when the form is posted in a two level multidimensional array that can be accessed using $_SESSION['name'] and a numeric index.
<?php
session_start();
if(isset($_SESSION['name']) && !is_array($_SESSION['name']))
{
$_SESSION['name'] = array();
}
if(isset($_POST['submit']) && trim($_POST['name']) != '') {
$_SESSION['name'][] = $_POST['name'];
}
print_r($_SESSION);
?>
<form action="" method="post">
<input type="text" name="name" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
I'm doing some tests with forms and ran into a snag, I have a very simple test script that works, but the problem is that my FILE field in the form is not getting submitted to the $_POST array for some reason, if I take out encode="multipart/form-data" it does, otherwise I just get the other fields. Here is the script:
<?PHP
print_r($_POST);
echo"<br>";
print_r($_FILES);
$name = $_FILES["img1"]["name"];
$tmp_name = $_FILES["img1"]["tmp_name"];
$uploads_dir = "uploads";
echo "<br>TEMP NAME:</b>";
echo $tmp_name;
move_uploaded_file($tmp_name, "$uploads_dir/$name");
?>
<html>
<body>
<br><br>
-------------------------
<form name="test" id="test" action="index.php" method="post" enctype="multipart/form-data">
<label>Name</label>
<input type="text" name="title" id="title">
<br>
<label>File</label>
<input type="file" name="img1" id="img1" size="40">
<br>
<input type="submit" value="submit">
</form>
The output is:
POST ARRAY: Array ( [title] => some title )
FIELS ARRAY: Array ( [img1] => Array ( [name] => chicken.jpg [type] => image/jpeg [tmp_name] => /tmp/phpcrBLw9 [error] => 0 [size] => 30940 ) )
TEMP NAME:/tmp/phpcrBLw9
As you can see, the POST only has the title, not the file name, I need it to have both for programming reasons, is there a way to do that without having to cheat / have hidden fields or edited values manually?
You will not be able to see $_FILES inside $_POST. They are different superglobals, if you vardump($_FILES); you will see that it stores all the information about the uploaded file, like size, tmp name etc. This is how PHP has been built, and the only thing you can do about it is access the name using
$_FILES['img1']['name']
You can access the name of the uploaded file here:
$_FILES["img1"]["name"]