I can't manage different buttons with different names - php

here is my code:
<?php
ob_start();
session_start();
require 'connection.php';
require 'core.inc.php';
?>
<?php
$take_thread_pid_query = #mysql_query(" select pid from threads ");
$row_take_thread_pid = mysql_fetch_array($take_thread_pid_query);
$pid = $row_take_thread_pid['pid'];
while($row_take_thread_pid = #mysql_fetch_array($take_thread_pid_query))
{
?>
<form action="kill_threads.php" method="POST" >
<label> <?php echo "<br/><br/>thread".$row_take_thread_pid['pid']; ?><input type="submit" value = " <?php echo $row_take_thread_pid['pid'];?> " name = " <?php echo
$row_take_thread_pid['pid']; ?> " /> </label>
<?php }?>
</form>
<?php
$t = "4756";//[4756 is on of the pids in my thread table].this is for testing but doesnt works,it cant find any button with this name.
if ( isset($_POST[$t] ) ) echo "im a killed thread..";
?>
The big problem is that im trying to give different names to each button i create,but it seems this is not working because when im trying to see if a button isset['???']
What i have to do...?
For example
thread 1 [button 1]
thread 2 [button 2]
thread 3 [button 3 ]
So if now i click button 1 i want thread1 row deleted from database.
phpmyadmin works like this.
Im so complicated..please help,thanks in advance.

Your code is:
value = " <?php echo $row_take_thread_pid['pid'];?> "
name = " <?php echo $row_take_thread_pid['pid']; ?> "
You are placing spaces before and after the php text so you either have to remove the spaces or code for them

I would suggest you to use another hidden input element within the form instead of doing it with submit form, in this way you can kill all the threads with one if block by passing thread id to it.
while($row_take_thread_pid = #mysql_fetch_array($take_thread_pid_query))
{
?>
<form action="kill_threads.php" method="POST" >
<label> <?php echo "<br/><br/>thread".$row_take_thread_pid['pid']; ?>
<input type="hidden" name="pid" value="<?php echo $row_take_thread_pid['pid'];?>" />
<input type="submit" value = "Delete" name = "delete_thread" /> </label> <?php }?>
</form>
<?php
$t = "4756";//[4756 is on of the pids in my thread table].this is for testing but doesnt works,it cant find any button with this name.
if ( isset($_POST[$t] ) ) echo "im a killed thread..";
?>
Another problem you may come across is using multiple forms without name and id. So add a dynamic number in the form tag like like this
<form name="<?php echo $i; ?>" id="<?php echo $i; ?>" action="kill_threads.php" method="POST" >
Here $i is counter variable or you can use $row_take_thread_pid['pid'] for this purpose.

Related

Form submission shows the result after the second page refresh only in PHP

I created a form where as soon as I click on the input of the submit, it writes me a value on the column to the database table.
So far everything is fine, but when the current page reloads, I can't see the result that was written to the database.
I can only see the result if I submit the form for the second time on the same page.
My code:
<?php
$increId = $_order->getRealOrderId();
$pathAss = 'My file';
$connectionresource = Mage::getSingleton('core/resource');
$connectionWrite = $connectionresource->getConnection('core_write');
$table = 'sales_flat_order';
$query = "UPDATE ".$table." "
. "SET upload_file_1='" . $pathAss . "'"
. " WHERE increment_id='".$increId."'";
$connectionWrite->query($query);
?>
<form id="abbLogoOrder" action="" method="post" enctype="multipart/form-data">
<a href="<?php echo $_order->getupload_file_1(); ?>"
title="<?php echo basename($_order->getupload_file_1()); ?>">
<?php echo basename($_order->getupload_file_1()); ?>
</a> //THIS VALUE RETURNS EMPTY AFTER FIRST SUBMIT FORM
<b>
<?php
/* $testget = ['getupload_file_'.$count.'()'];
echo $_order->$testget; */
?>
</b>
<div class="upload-btn-wrapper">
<button class="btn" style="font-size:13px;">SELECT YOUR FILES</button>
<input type="file" name="abbFile<?php echo $count; ?>[]" id="abbFile<?php echo $count; ?>" multiple="multiple" />
<input type="submit" value="SEND" />
</div>
</form>
What am I doing wrong?
You are not re-fetching the order details after the save function. So the details are getting from the initial state, so the result is old, but the next reload it changes to updated value and shows the right output.
But still it's unclear where you are loading the order object.

php set input "name" to php echo variable

I have a form with two radio buttons as "yes" and "No"
I m bringing the "name" of the input from PHP variable I get from the DB.
But however, it echoes in the form but when submitting gives the error.
My HTML code goes as:
<form action="phpfile.php" method="post">
<?php
$sql6 = "SELECT * FROM subadminpriv WHERE subadmin_id = '$sa_id'";
$result6 = $conn->query($sql6);
if ($result6->num_rows > 0) {
while($row6 = $result6->fetch_assoc()) {
$sa_id = $row6["subadmin_id"];
$privilege = $row6["privilege"];
$status = $row6["status"];
?>
<div class="togglebutton m-t-30" >
<label >
<strong>⇒ <?php echo $privilege; ?> </strong> <br>
<input name="<?php echo $privilege; ?>" value="1" type="radio">Yes
<input name="<?php echo $privilege; ?>" value="0" type="radio">No
</label>
</div>
<hr>
<?php } }else { } ?>
<button class="btn button" type="submit">Assign Privileges</button>
</form>
Where the $privilege is "Can See Vendors"
So in here, I'm trying to get the fields "sa_id, privilege, status" from the DB and echo as many radio(yes/no) as the fields in DB.
It works, however, and I echoed the "name" field in the input with PHP variable when I inspect it, it gives the value form the DB, but when I submit and echo it, it gives an error.
The PHP phpfile.php page goes as :
<?php include ('../db.php'); ?>
<?php
$priv1 = $_POST['privilege1'];
echo $priv1;
?>
Here "privilege1" is the actual privilege name from the DB field.
while it gives error as "Notice: Undefined index: privilege1 in D:\xampp\htdocs\blah blah blah/pIpfile.php on line 6"
While I Was expecting value I given to radio button like 1 or 0;
Hope i m Clear with my problem. Any Help is Appreciated...
Don't know how I missed it.
The name in the input field won't accept space, so I changed it to "Can_See_Vendors"
Solved

PHP: how to update html text element, based on value from select element?

I have an an html form with only <select> element. Below the form I have a text box, I want to update the value of the text box based on the option selected in the form.
I am not sure if this is possible in PHP, I know I can do it with Javascript/Jquery, but I am working on a school project and its a PHP project. My teacher has done it but Im not sure if he used PHP.
Here is my code:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="books">
<?php
$books = simplexml_load_file("books.xml");
foreach($books->book as $book){
$code = $book->code;
$title = $book->title;
echo "<option value='$code'>$code: $title</option>";
}
?>
</select>
</form>
<?php
$choice = $_POST['books'];
echo "<input type='text' value='$choice'>";
?>
When the page first loads I want the value from the selected option to be in the text box. I am not sure how I could go about doing this in PHP. Help would be appreciated.
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="books" onchange="javascript:document.form.submit();">
<?php
$books = simplexml_load_file("books.xml");
foreach($books->book as $book){
$code = $book->code;
$title = $book->title;
echo "<option value='$code'>$code: $title</option>";
}
?>
</select>
</form>
<?php
$choice = isset($_POST['books'])?$_POST['books']:'';
echo "<input type='text' value='$choice'>";
?>
Edit, as a pure PHP solution with a submit button, which will load data on initial page load then load data with the submit button as per selection.
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="books">
<?php
$books = simplexml_load_file("books.xml");
foreach($books->book as $book){
$code = $book->code;
$title = $book->title;
echo "<option value='$code'>$code: $title</option>";
}
?>
</select>
<input type="submit" value="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])){
$choice = isset($_POST['books']) ? $_POST['books'] : $books->book[0]->code;
echo "<input type='text' value='$choice'>";
}
else{
$choice = isset($_POST['books']) ? $_POST['books'] : $books->book[0]->code;
echo "<input type='text' value='$choice'>";
}
?>
Original answer with onchange
The following would work, using Javascript and getElementById which would echo the selection in a text box using an onchange function.
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="books" onchange="update_textbox()" id="id_books">
<?php
$books = simplexml_load_file("books.xml");
foreach($books->book as $book){
$code = $book->code;
$title = $book->title;
echo "<option value='$code'>$code: $title</option>";
}
?>
</select>
</form>
<?php
$choice = isset($_POST['books']) ? $_POST['books'] : '';
echo "<input type='text' value='$choice' id='showit'>";
?>
<script>
function update_textbox(){
var selectbox = document.getElementById('id_books');
var id_books = selectbox[selectbox.selectedIndex].text;
document.getElementById('showit').value=id_books ;
}
</script>
To load data into the text box at initial page load, use:
Replace:
$choice = isset($_POST['books']) ? $_POST['books'] : '';
with:
$choice = isset($_POST['books']) ? $_POST['books'] : $books->book[0]->code;
When you first load the page, there are no POST data, so you could set $choice to the first book in list:
$choice = isset($_POST['books']) ? $_POST['books'] : $books->book[0]->code;
But you have to udnerstand the difference between PHP and JavaScript in here.
PHP can only change the value, when there is a request on the server as it is a server-side language. If you wish to update the value instantly, without submiting the form, you need to use JavaScript.
if it must be purely PHP, then put a submit button on your form for when a selection is made. This will set the value you are looking for. Others have provided samples on how to do this form submission automatically with javascript, but the end result is the same.
You can do it with PHP, it looks as though you're just missing the submit button to actually post the selection.
To submit the form without a submit button, you need to use javascript, eg.
<select onchange="this.form.submit()">....</select>
Below is a simple example (static options rather than xml) that also sets the selected option in the select (IMO this is a more practical use than printing it in the text input)
<?php
$val = isset($_REQUEST['books']) ? $_REQUEST['books'] : null;
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="books" onchange="this.form.submit()">
<option value=>Choose a book</option>
<option<?php if($val && $val === 'opt 1'){ print ' selected';}?>>opt 1</option>
<option<?php if($val && $val === 'opt 2'){ print ' selected';}?>>opt 2</option>
</select>
<noscript><button type="submit">Submit</button></noscript>
</form>
<?php if($val){ ?>
<input type="text" value="<?php echo $_REQUEST['books']; ?>">
<?php } ?>
This is probably ok just for a school exercise, but in the real world you would probably want to sanitize any user input ($_REQUEST['book'] / $_POST['book']) and use a better templating engine than vanilla PHP (I like Twig) to help separate presentation from functional logic.

Getting all checked items from a while loop and put them into database as true or false

I am currently doing up a list of names that has signed up for a activity which is then needed to go through another selection process and to be put into the database. I have currently done the getting and displaying of names from the people that has signed up.
However, I am not too sure on the process of how I can get those checked which is being selected in the selection process.
This is currently the codes that I have done to get and display the names of the people who have signed up. Upon clicking submit, it would go to another page which would then process, get those checked and marked as 1 or true to the database (MySQL) or either set as 0 or false if it has not been checked.
<?php
if ($totalShortlist > 0) {
?>
<?php
while ($row = mysqli_fetch_assoc($result)) {
$firstName = $row['first_name'];
$lastName = $row['last_name'];
?>
<form id="selectionProcess" action = "doShortlistProcess.php?id=<?php echo $id ?>" method = "post">
<fieldset data-role="controlgroup">
<label><input type="checkbox" name="selectionProcess" id="selectionProcess" value="<?php echo $id ?>"/><?php echo $firstName; ?> <?php echo $lastName; ?></label>
</fieldset>
<?php
}
?>
<input type="submit" class="btnSelect" data-theme="b" data-inline="true" name="shortlistCandidate" value="Submit Shortlisted Candidates"/>
</form>
<?php
}
// end for loop
else {
echo "No candidates to be shortlisted";
}
mysqli_close($link);
?>
The $id as shown is the brought over from the list of categories on the previous page, which allows to get all the names in that categories to be displayed.
Your code places the form in the wrong place compared to the while loop. The action also doesn't need you to compose a query string. Better code (careful, I've also cut out some presentation):
if ($totalShortlist > 0) {
?>
<form id="selectionProcess" action = "doShortlistProcess.php" method = "post">
<fieldset>
<?php
// use while loop to display a tick box for each item
while ($row = mysqli_fetch_assoc($result)) {
$firstName = $row['first_name'];
$lastName = $row['last_name'];
?>
<label>
<input type="checkbox" name="selectionProcess" id="selectionProcess" value="<?php echo $id ?>"/>
<?php echo $firstName; ?> <?php echo $lastName; ?>
</label>
<?php
} // end of while loop
?>
</fieldset>
<input type="submit"/>
</form>
<?php
} // end of shortlist > 0
I recommend you test this code using the GET method. It will show you what form the submission takes.
To process the result, you should use the list of responses, process it and turn it into one or more SQL statements.
An alternative would be to use an action for each checkbox, but then use ajax to a php page that would modify the status of single subscriber. The change would be made immediately as users click.

Forms and getting data from forms [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
So, I have a problem with getting data from forms and using it in my codes.
this is choose options page:
<form action="options.php" method="post">
<label>Select number of options:</label>
<select name="options">
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select><br><br>
<input type="submit" name="next" value="Next"><br><br>
</form>
and this is options.php page
<?php
// put your code here
$options = $_POST['options'];
if (isset($_POST['submit'])) {
echo $options;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Fill in the following fields:<br><br>
<?php
//loop to prompt the user to enter options' details
for ($i = 1; $i <= $options; $i++) {
$optionName = "option$i";
?>
<?php echo 'Option ' . $i; ?><input type="text" name="<?php echo $optionName; ?>"/><br><br>
<?php
}
?>
<input type="submit" name="submit" value="Next"/>
</form>
Somehow I can't get it working. It keeps giving me an error which is "undefined index". I tried to fix it with isset(). I keep doing something wrong here but I don't know what is it. Will someone please help me or suggest some solutions and ways to get it working. I am new at this and started learning last week.
Put these at the beginning of your code to see what's actually posted (it will also show the get values). These format the Get and Post values nicely. I use them all the time. Once you see what's actually posted you'll be able to see what isn't what you're expecting.
echo("<br><br>Get contents:"); echo("<pre>" . print_r($_GET, 1) . "</pre>");
echo("<br>Post contents:"); echo("<pre>" . print_r($_POST, 1) . "</pre>");
exit;
Your $options = $_POST['options']; should be $options = (isset($_POST['options']) ? $_POST['options'] : "");
You have
<input type="submit" name="next" value="Next"><br><br>
Later you check
if (isset($_POST['submit'])) {
The name you give the input, is the key you want to use when looking up the value.
if (isset($_POST['next'])) {
To start you $_POST['submit'] will never be set because there's nothing posting to that field.
If you want to debug this, let's start by getting the output of EVERYTHING posted. We can do that by doing a print_r($_POST); like the following.
<?php
echo '<pre>';
print_r($_POST);
echo '</pre>'
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Fill in the following fields:<br><br>
<?php
//loop to prompt the user to enter options' details
for ($i = 1; $i <= $options; $i++) {
$optionName = "option$i";
?>
<?php echo 'Option ' . $i; ?><input type="text" name="<?php echo $optionName; ?>"/><br><br>
<?php
}
?>
<input type="submit" name="submit" value="Next"/>
</form>
Then you can see what POST fields are available to you easily and continue coding your project from there.

Categories