php binding textbox value after submit button onclick - php

I want to submit the textbox value and bind the value in the textbox after the submit button onclick.
echo '<td>';
echo '<form name="form1" method="POST" action="">';
echo Enter here:<br><input type="text" id="textbox1" name="tb1" value="" size="3"<br/>
<input type="submit" id="btn1" name = "btnenter'" value="Enter" />';
echo '</form>';
echo '</td>';
if ( isset( $_POST[btnenter] ) ) {
$value1 = $_POST[tb1];
}
I can get the value, but how to bind the value?

<input type="text" id="textbox1" name="tb1" value="" size="3"<br/>
to
<input type="text" id="textbox1" name="tb1" value=". $value ." size="3"<br/>
Also it would work even if you dont check if the valuable exists, since it would be null so you may prefer not to check if "isnull".
also when using echo, if you use double quotes ("), you can use variables in your echo. such as:
<input type='text' id='textbox1' name='tb1' value='${value}' size='3'<br/>

$value1 = "";
if ( isset( $_POST[btnenter] ) ) {
if( isset( $_POST[tb1] ) ){
$value1 = $_POST[tb1];
}
}
echo '<td>';
echo '<form name="form1" method="POST" action="">';
echo Enter here:<br><input type="text" id="textbox1" name="tb1" value=". $value1 ." size="3"<br/>
<input type="submit" id="btn1" name = "btnenter'" value="Enter" />';
echo '</form>';
echo '</td>';

Related

How can I pass variables from hybrid .php/HTML file to another .php file? -- I've tried all the methods I've found on-line [duplicate]

I have the following code:
<?php
$student_no = $_GET['student_no'];
echo '<form name="student" action="PROCESS_FEE007.php" method="POST">';
echo '</br><table>';
echo '**<input name="student_no" type="hidden" value="$student_no" />**';
echo '<td>Amount: </td><td>'.'<input name="amount" type="text" /></td></tr>';
echo '<tr> <td>Remarks: </td><td>'.'<input name="remarks" type="text" /> </td>';
echo '<tr> <td>';
echo '<td>'.'<input type="submit" value="Save"/></td></tr>';
echo '</table>';
echo '</form>';
?>
On the next page PROCESS_FEE007.PHP the value is not received.
Variables are not parsed by interpreter inside single quotes. You should use double quotes or explicit string concatenation.
In your example the value of $_POST['student_no'] will be string '$student_no', not the value of the $student_no variable.
Besides if you're using method="POST" in your form, you can only get the inputs value through the $_POST array.
<?php
$student_no = $_POST['student_no'];
echo '<form name="student" action="PROCESS_FEE007.php" method="POST">';
echo '</br><table>';
echo '**<input name="student_no" type="hidden" value="'.$student_no.'" />**';
echo '<td>Amount: </td><td>'.'<input name="amount" type="text" /></td></tr>';
echo '<tr> <td>Remarks: </td><td>'.'<input name="remarks" type="text" /> </td>';
echo '<tr> <td>';
echo '<td>'.'<input type="submit" value="Save"/></td></tr>';
echo '</table>';
echo '</form>';
?>
parse student_no in form as
<?php
$student_no = $_GET['student_no'];
echo '<form name="student" action="PROCESS_FEE007.php" method="POST">';
echo '</br><table>';
echo '**<input name="student_no" type="hidden" value="'.$student_no.'" />**';
echo '<td>Amount: </td><td>'.'<input name="amount" type="text" /></td></tr>';
echo '<tr> <td>Remarks: </td><td>'.'<input name="remarks" type="text" /> </td>';
echo '<tr> <td>';
echo '<td>'.'<input type="submit" name="submit_save" value="Save"/></td></tr>';
echo '</table>';
echo '</form>';
?>
and on the PROCESS_FEE007.php page use
<?php
if ($_POST['submit_save']){
var_dump($_POST);die();
}
?>
check the attribute "VALUE" of hidden input field. The value is not put in the field.
First make the input field a text box and after fixing the bug make it a hidden field.
may be useful. (I forgot cuz I am out of PHP long time).
try using $_REQUEST instead of get example $student_no = $_REQUEST['student_no'];

It is not possible to output data from MySQL (Wordpress)

I use a Wordpress site, I do my search on a custom database called "operations". The search is performed on the website.
I need to get other results from the table related to this row on request, not just what I entered. And get other data related to this string. Here is the search form on the site:
<form method="post" action="https://site-name.com/wp-content/themes/theme/select_user.php">
<label for="sku">SKU:</label><br/>
<input type="text" name="sku" size="30"><br/>
<label for="barcode">Barcode:</label><br/>
<input type="text" name="barcode" size="30"><br/>
<input id="submit" type="submit" value="Search"><br/>
</form>
</fieldset>
The database has the following columns: id, date, title, size, sku, barcode, price
File Contents select_user.php:
require( __DIR__ . '/../../../wp-load.php' );
global $wpdb;
$sku = trim($_REQUEST['sku']);
$barcode = trim($_REQUEST['barcode']);
$sql_select = $wpdb->get_results(
$wpdb->prepare(
"
SELECT * FROM " . $wpdb->prefix . "operations
WHERE sku='$sku' || barcode='$barcode',
ARRAY_N
"
)
);
if ($sql_select)
{
foreach($sql_select as $row)
{
echo 'SKU: ' . $row['sku'] .'</br>';
echo 'Barcode: ' . $row['barcode'] .'</br>';
}
}
else {
echo 'No results';
}
With this code, I get the answer "no results".
I would be grateful for any help
use the post hook for wp forms not directly use .php file
add_action( 'admin_post_add_foobar', 'prefix_admin_add_foobar' );
function prefix_admin_add_foobar() {
// Handle request then generate response using echo or leaving PHP and using HTML
}
for your form use :
<form action="http://www.example.com/wp-admin/admin-post.php" method="post">
<input type="hidden" name="action" value="add_foobar">
<input type="hidden" name="data" value="foobarid">
<input type="submit" value="Submit">
</form>
I created my own shortcode, and placed everything that is necessary there. Everything works.
function custom_search_func( $atts ){
echo '<fieldset>
<form action="' . get_permalink() . '" method="POST">
<label for="sku">SKU:</label><br/>
<input type="text" name="sku" size="30"><br/>
<label for="barcode">Barcode:</label><br/>
<input type="text" name="barcode" size="30"><br/>
<label for="date">Date:</label><br/>
<input type="date" name="date" size="30"><br/>
<input type="submit" value="Search">
</form>
</fieldset>';
global $wpdb;
$sku = trim($_REQUEST['sku']);
$barcode = trim($_REQUEST['barcode']);
$date = trim($_REQUEST['date']);
$sql_select = $wpdb->get_results(
$wpdb->prepare(
"
SELECT id, date, title, size, barcode, sku, price FROM " . $wpdb->prefix . "operations
WHERE sku='$sku' || barcode='$barcode' || date='$date'
"
)
);
if ($sql_select)
{
echo '<table border="1" width="100%" cellpadding="5">';
echo '<tr><th>ID</th>';
echo '<th>Title</th>';
echo '<th>Size</th>';
echo '<th>SKU</th>';
echo '<th>Barcode</th>';
echo '<th>Price</th>';
echo '<th>Date</th></tr>';
foreach ( $sql_select as $id) {
echo '<tr>';
echo '<td>';
echo $id->id;
echo '</td>';
echo '<td>';
echo $id->title;
echo '</td>';
echo '<td>';
echo $id->size;
echo '</td>';
echo '<td>';
echo $id->sku;
echo '</td>';
echo '<td>';
echo $id->barcode;
echo '</td>';
echo '<td>';
echo $id->price;
echo '</td>';
echo '<td>';
echo $id->date;
echo '</td>';
echo '</tr>';
}
echo '</table>';
}
else {
echo 'No results';
}
}
add_shortcode( 'customsearch', 'custom_search_func' );
Use the [customsearch] shortcode to output this anywhere on the page

submit ID using multiple checkboxes in PHP HTML Mysql

I am using multiple HTML checkboxes to submit student attendance. The checkbox once clicked submit '1' and if not checked, will submit '0'. I am able to submit '1' when checked but cant submit '0'. Below is my code:
<?php
$subcheck = (isset($_POST['subcheck'])) ? 1 : 0;
$date = date("Y/m/d");
foreach ( $student as $attendance ) {
echo "<tr>";
echo "<td>$attendance->id</td>";
echo "<td>$attendance->name</td>";
echo "<td>
$attendance->classroom_id
</td>";?>
<input type="hidden" name="date[]" value="<?php echo $date;?>" />
<td><input type="checkbox" name="status[]" value="<?php echo $attendance->id;?>"></td>
<td><input type="text" name="reason[]" ></td>
<?php
}
?>
<tr>
<td colspan="2" align="center"><input type="submit" value="Save" name="submit"></td>
This might give you some ideas:
<?php
$subcheck = (isset($_POST['subcheck'])) ? 1 : 0;
$date = date("Y/m/d");
$out = '<table id="tblAttendance"><thead><th>ID</th><th>Name</th><th>Room</th><th>Status</th><th>Reason</th></thead><tbody>';
foreach ( $student as $attendance ) {
$out .= '<tr>';
$out .= '<td>' .$attendance->id. '<input type="hidden" name="studentID[]" value="' .$attendance->id. '"></td>';
$out .= '<td>' .$attendance->name. '<input type="hidden" name="name[]" value="' .$attendance->name. '"></td>';
$out .= '<td>' .$attendance->classroom_id. '<input type="hidden" name="classroomID[]" value="' .$attendance->classroom_id. '"></td>';
$out .= '<td><input type="checkbox" name="status[]" value="yes"></td>';
$out .= '<td><input type="text" name="reason[]" ></td>';
$out .= '</tr>';
}
$out .= '<tr><td colspan="2" align="center"><input type="submit" value="Save" name="submit"></td></tr>';
$out .= '</tbody></table>';
$out .= '<input type="hidden" name="date" value="' .$date. '" />';
echo $out;
?>
Since you are using a type="submit" button, I presume you have this inside a form construct?
Recall how checkboxes work in HTML forms: if the box is checked, the value received on the PHP side will be the value="yes" value. In other words, the variable $_POST['status'][n] will have the value yes.
HOWEVER, if the checkbox is not set, then $_POST['status'][n] will not be set.
Reference:
http://www.html-form-guide.com/php-form/php-form-checkbox.html

php update dynamic sql rows through form

My goal is to retrieve a dynamic SQL list of rows and edit every item in the list within the same page using POST (no multiple changes at once required). Can this be achieved within the same page? I made the script below which displays everything the way i want to, however the switch statement doesn't seem to be fit for purpouse.
I am aware this code might not be best practice (not to mention secure). Sorry for being inflexible, but i'm not allowed to use anything other then PHP/HTML
Code:
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<tr> <td class="tg">' .$row["id"]. '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["voornaam"].'" style="width:100px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["achternaam"].'" style="width:100px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["omschrijving"].'" style="width:500px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td> </tr>' . "\n";
}
} else {
echo "0 resultaten";
}
switch(!empty($_POST))
{
case !empty($_POST[$row["voornaam"]]):
// update SQL query 'firstname' here
break;
}
$conn->close();
?>
By this way you actualy see what you are editing and it's more clean.
// PDO DB conecction (secure way)
try {
$con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $exception){ //to handle connection error
echo "Connection error: " . $exception->getMessage();
}
if isset($_POST["voornaam"]){
$stmt = $con->prepare(" QUERY");
$stmt->execute();
echo "YEAH you did it";
} else {
stmt = $con->prepare(" QUERY");
$stmt->execute();
$rows = $stmt->fetchAll();
foreach ($rows as $rs)
{
$voornaam = $rs["voornaam"]
$achternaam = $rs["achternaam"]
$omschrijving = $rs["omschrijving"]
}
}
// Just an example
echo '<form action= "" method="post"> <input type="text" name="voornaam" value="'. $voornaam .'" style="width:100px"><input name="submit" type ="submit" value="Submit"><br/><br/>';
Value is also the input so you can have only one Submit button at the end cause all other data will be overwritten by itself.
<?php
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo '<tr> <td class="tg">' .$row["id"]. '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["voornaam"].'" style="width:100px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["achternaam"].'" style="width:100px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["omschrijving"].'" style="width:500px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td> </tr>' . "\n";
}
}
else
{
echo "0 resultaten";
}
if( isset( $_POST[$row["voornaam"]] ) && !empty( $_POST[$row["voornaam"]] ) )
{
// update SQL query 'firstname' here
}
$conn->close();
?>
In your switch you're sending !empty($_POST) so boolean (true or false) so unique cases would be: True or False...
It's better if you change your switch to:
if(isset($_POST[$row["voornaam"]]) && !empty($_POST[$row["voornaam"]])){
// update SQL query 'firstname' here
}

Get data from a post form

I don't understand why I can't use my last form in this code. I generated a form using a SELECT list to select the member that I want to update and it works, but I don't know why I can't use datas from this form. Actually, I can't even echo something (see the echo "TEST"; at the end, nothing happens when I submit the form).
<?php $mysqli = new Mysqli("localhost", "root", "", "repertoire"); ?>
<form method="post" action="">
<label>Modifier</label>
<select name='id_modif'>
<?php
$resultat = $mysqli->query("SELECT * FROM annuaire");
while($select = $resultat->fetch_assoc()){
echo "<option value=". $select['id_annuaire'] . ">" . $select['prenom'] . " " . $select['nom'] . "</option>";
}
?>
</select>
<input type ="submit" name="modifier">
</form>
<br>
<?php
if (isset($_POST['modifier'])){
//print_r($_POST);
$resultat = $mysqli->query("SELECT * FROM annuaire WHERE id_annuaire = '$_POST[id_modif]'");
while ($modif = $resultat->fetch_assoc()) {
echo '<form method="post" action="">
<label for="nom">Nom *</label><br>
<input type="text" name="nom" value="' . $modif['nom'] . '"> <br>';
echo '<label for="prenom">prenom *</label><br>
<input type="text" name="prenom" value="' . $modif['prenom'] . '"> <br>';
echo '<label for="telephone">telephone *</label><br>
<input type="text" name="telephone" value="' . $modif['telephone'] . '"> <br>';
echo '<label for="profession">profession *</label><br>
<input type="text" name="profession" value="' . $modif['profession'] . '"> <br>';
echo '<label for="ville">ville *</label><br>
<input type="text" name="ville" value="' . $modif['ville'] . '"> <br>';
echo '<label for="codepostal">codepostal *</label><br>
<input type="text" name="codepostal" value="' . $modif['codepostal'] . '"> <br>';
echo '<label for="adresse">adresse *</label><br>
<textarea name="adresse">' . $modif['adresse'] . '</textarea> <br>';
echo '<label for="date_de_naissance">Date de naissance</label><br>
<input type="date" name="date_de_naissance" value="' . $modif['date_de_naissance'] . '"><br>';
echo '<label for="sexe">sexe</label><br>
<input type="radio" name="sexe" class="sexe" value="m" checked>Homme
<input type="radio" name="sexe" classe="sexe" value="f">Femme<br>';
echo '<label for="description">description *</label><br>
<textarea name="description">' . $modif['description'] . '</textarea> <br>';
echo '<input type="submit" name="valider_modif" value="Modifier"> <br>';
}
if (isset($_POST['valider_modif'])){
echo "TEST";
}
}
?>
Your second if check is inside the other one, so it will only run when both $_POST['modifier'] and $_POST['valider_modif'] are set. But you second form does not send modifier anywhere.
You could add a hidden field to your second form:
<input type="hidden" name="modifier" value="1" />
Or if you don't want to show the second form again, move the second if outside the other.
Also, you should not use $_POST values in SQL queries directly to be safe from SQL injection. A function like mysqli_real_escape_string should be used to escape the value first.
You have 2 forms and you are not closing the 2nd with </form>
Took me a long time to go through your code. Okay. First thing. Try not to echo so much html markup. It just makes you code the clunkiest in the world. From what I gathered, the Modifier button comes up when you click on the first button. What you need to do if you want to see the TEST message is to take it out of the if statement because the Buttons are like an XOR gate. Setting the other unsets the other

Categories