Losing GET variable on form submit or on refresh - php

I am sending an id from a link
<a class="suba" href="empty.php?id=1" ></a>
and recieving this id in the empty.php page :
require_once 'connection.php';
session_start();
$id=$_GET['id'];
on refresh or on form submit the variable $id is lost :
<form action="" method="get">
<input type="submit" name="add" value="next">
</form>
<?php if(isset($_GET['add']) && $_SESSION['j'] < $filecount){
$_SESSION['j']++;
echo '<input type=hidden name=id value=' .$id. '>';
} ?>
I tried adding a hidden input in the form but it's not working also tried saving the variable in a session:
$_SESSION["word"] = $id;

You must print the Id in your form action
<form action="?id=<?= $id ?>" method="get">
<input type="submit" name="add" value="next">
</form>

Related

PHP isset not accepting form name

I am having an issue trying to get my form with a hidden input field to be recognized when I check if it isset. Not sure if it is because I am running the form in a loop or what. I do know when I click submit for any of the fields from the loop the value of the hidden field is registering properly, it is just somehow the 'form1' is not registering as isset. Any ideas?
<?php
if(isset($_POST['form1']))
{
echo $_POST['cid'];
} else {echo "nope!";}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" name="form1">
// gets array
$listchars = $user->getCharacterList($_SESSION['user_id']);
// loops through array
foreach($listchars as $chardata){
echo $chardata['name']; ?>
<input type="hidden" name="cid" value="<?php echo $chardata['cid'];?>">
<input type="submit" name="submit" value="submit">
<?php } ?>
</form>
The form name is not submitted
Use
<input type="submit" name="form_name">
or
<input type="hidden" name="form_name">

Passing Input values from one PHP page to another within hidden fields

$sql="SELECT vName,id FROM employee WHERE vName LIKE '%$my_data%' ORDER BY vName";
$result = mysql_query($sql);
if($result)
{
while($row=mysqli_fetch_array($result))
$hid='<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />';
echo($hid);
echo $row['vName']."\n";
}
How to pass the value of a hidden input field to another PHP script? I am using auto complete. how to pass the value auto complete page to index page
You have two options:
Sessions
PHP Sessions
Session support in PHP consists of a way to preserve certain data across subsequent accesses.
eg:
<?php
// Page1.php
session_start();
$_SESSION["key"] = "random value";
Then:
<?php
// Page2.php
session_start();
echo $_SESSION["key"];
// Output would then be ... random value
POST
Using the PHP $_POST
Taking what you currently have, you'd do:
<form method="post" action="somescript.php">
<input type="hidden" name="xyz" id="abc" value="<?=$row['id'] ?>" />
<button type="submit" name="submit" value="submitForm" />
</form>
Then on somescript.php if you do:
<?php
print_r($_POST);
You'll see an array with the data from your form, hidden value included
Create a form
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
And Get value on action_page.php
$_GET['xyz']
You enter your html code inside php code like this
<?php
while($row=mysqli_fetch_array($result))
{
?>
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
<?php
echo $row['vName']."\n";
}
?>

using a value from select HTML PHP

i'm trying to get a value from a select but i don't get how to do it, here is my code:
<form method="post">
<select name="List">
<?php while($Adresses = mysqli_fetch_array($adress)) { ?>
<option value="<?php echo $Addresses[0];?>"><?php echo $Addresses[0]; ?> </option><?php } ?>
</select>
<input type="submit" name="Edit" value="Edit">
</form>
I need the value of the selected address here:
<?php if(isset($_POST['Edit'])):
$_SESSION['adress']= $_POST['List'];?>
<p><b>Change your adress:</b> <input type="text" name="new_adress" value= "<?php echo $_SESSION['adress']; ?> " /></p>
<input type="submit" name="save" value="Save">
<input type="submit" name="delete" value="Delete">
<?php endif; ?>
and this is the error message:
Undefined index: List
I have tried
<?php $_SESSION['adress']= $_GET['List'];
but it did not work either...
thanks for you help in advance.
PHP is a serverside language.
HTML, CSS, and JavaScript are clientside languages.
PHP is for sending out HTML, CSS, and JavaScript data. To send from HTML to PHP you need to submit your form.
In your html:
<html>
<head>
</head>
<body>
<form action='test.php' method='post'>
<?php
echo "
<input name='name' value='" . $_SESSION['name'] . "'>
<input name='address' value='" . $_SESSION['name'] . "'>
";
?>
<input type='submit' value='Update Information'>
</form>
</body>
</html>
In your 'test.php':
<?php
$_SESSION['name'] = $_POST['name'];
$_SESSION['address'] = $_POST['address'];
?>
This will save it into your session. To actually save it you need to look into SQL or XML data parsing.
The only way to process data from HTML => PHP is with form submitting. PHP is processed by the server, not your internet browser.
Use POST for getting the data.
<?php $_SESSION['adress']= $_POST['List'];

PHP Delete not working as I would I like

Can someone help me. My delete code below works, but it's deleting the most recent Favorited file and not the specific file chosen. Here's the code:
while($row=$query->fetch())
{
$id=$row['id'];
$vid=$row['thread_id'];
$preview=$row['preview'];
$tt=$row['thread_title'];
$fav=$row['fav'];
$List.='<form action="" method="POST" id="postForm">
<div class="LISTT">'.$preview.'<br/><label id="pwords">'.$tt.'</label><br/>
<input type="submit" name="submit" value="Remove" id="DeleteButton"/>
</div></form>';
if(isset($_POST['submit']))
{
$query=$db->prepare("DELETE FROM favorite WHERE thread_id=:thread");
$query->execute(array(':thread'=>$vid));
}
}
You need to add a hidden form field that contains the Thread ID into your form, then read that back in your form handler, something like this:
while($row=$query->fetch())
{
$id=$row['id'];
$vid=$row['thread_id'];
$preview=$row['preview'];
$tt=$row['thread_title'];
$fav=$row['fav'];
$List.='<form action="" method="POST" id="postForm">
<div class="LISTT">'.$preview.'<br/><label id="pwords">'.$tt.'</label><br/>
<input type="hidden" name="thread" value="' . $vid . '" />
<input type="submit" name="submit" value="Remove" id="DeleteButton"/>
</div></form>';
if(isset($_POST['submit']))
{
$id = $_POST["thread"];
$query=$db->prepare("DELETE FROM favorite WHERE thread_id=:thread");
$query->execute(array(':thread'=>$id));
}
}
The reason for this is because you have the If statement in your while loop. The logic in the code you have given is to delete records when $_POST['submit'] is set. So it will follow the loop to delete the records and not a specific record.
You need to pass the id you want to delete to the user, as you are using form to do this, have a hidden field with the id.
if(isset($_POST['submit']))
{
$query=$db->prepare("DELETE FROM favorite WHERE thread_id=:thread");
$query->execute(array(':thread'=>$_POST['id']));
}
while($row=$query->fetch())
{
$id=$row['id'];
$vid=$row['thread_id'];
$preview=$row['preview'];
$tt=$row['thread_title'];
$fav=$row['fav'];
$List.='<form action="" method="POST" id="postForm">
<div class="LISTT">'.$preview.'<br/><label id="pwords">'.$tt.'</label><br/>
<input type="submit" name="submit" value="Remove" id="DeleteButton"/>
<input type="hidden" name="id" id="id" value="'.$id.'" />
</div></form>';
}

Change hidden field value on Submit click

I have a form with multiple items and an id attributed to each of these items, on Submit I want to be able to grab the id of the item that was clicked - I have tried using js, something like:
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>" type="submit" value="Add">
</form>
I want to be able to grab this value: $item_id = $_POST["item_id"]; on add_item_cart.php, but this doesn't seem to be working.
Is this a problem with my js syntax or is my logic not plausible to solve this problem? Is it submitting before changing the value?
EDIT:
Let's see if I can explain myself better, I want to assign that hidden value dynamically, imagine that my form has 3 submit buttons (one for each item displayed). Depending on the one that is clicked, I want to pass the item's id to my hidden field, so if I click button1 - $_POST["item_id"]=1, button2 - $_POST["item_id"]=2... etc
Here is my actual form (non simplified example)
<form method="post" action="add_item_cart.php">
<table style="width:600px">
<tr>
<?php foreach ($items as $item): ?>
<td>
<center>
<span style="font-size:20px"><?php echo $item["item_name"] ?></span><br/>
€<?php echo $item["price"] ?><br/>
Quantidade: <input type="text" value="1" style="width:30px"><br/>
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo $item["id"]; ?>" type="submit" value="Adicionar">
</center>
</td>
<?php endforeach; ?>
</tr>
</table>
</form>
When your form is posted and you want to collect the item_id value on add_item_cart.php first you need to actually assign a value to id as in (Assuming item_id is a php variable). The id is just used for setting the css editing not a value...
<input type="hidden" id="item_id" value="<?php echo $item_id; ?>" name="item_id">
you cannot have id='' for the value because 'value' is value.
Then you can get that value on your other page with:
<?php
if(isset($_POST['item_id'])){
$item_id = $_POST['item_id'];
}
?>
If you want to edit the variable after post depending on which button you hit you can try.
<input name="submit_item1" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item2" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item3" id="btn_sub" name="button1" type="submit" value="Add">
Then on the top of your page you can do.
<?php
if(isset($_POST['submit_item1'])){
$item_id = 1;
}
if(isset($_POST['submit_item2'])){
$item_id = 2;
}
if(isset($_POST['submit_item3'])){
$item_id = 3;
}
?>
If you are creating forms from an array of items you could do something like, (assuming you somehow have an id associated with those items; I would need to know more information about how you are making your item list. But it would generally do like this.
<?php
foreach($item_array as $item){
?>
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id" value="<?php echo $item['id']; ?>">
<input name="submit_item" id="btn_sub" type="submit" value="Add">
</form>
<?php
}
?>
Then on the top of your page you can just get $_POST['item_id'] and since that value is dynamically set you do not need any conditionals you can just get that value and run any query.
UPDATE
Use normal button instead of submit button and use javascript for submitting the form.
<form method="post" name="f1" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>; document.f1.submit();" type="button" value="Add">

Categories