I have an HTML form that is echoed from my php script. When I try to pass a hidden variable through the script, the code does not work as intended.
$threadNumber=$row["Tid"];
echo "<center><b>Message# ", $messageNumber , ": </b><br>";
echo $row["Mtitle"] , " in Thread# " , $row["Tid"] , "<br>";
echo "The Message was Written on " , $row["Mdate"] , '<br>';
echo $row["Mbody"];
echo "<br><br>";
echo '<form action="messageReply.php" method="post">';
echo '<textarea name="reply" rows=5 cols=30 placeholder="Reply to the Message?"></textarea>';
echo '<input type="hidden" name="Mtitle" value="<?php echo $row["Mtitle"] ?>">';
echo '<input type="submit" value="Send Message">';
echo '</form>';
The result looks like this:
and when I try to read $_POST["Mtitle"] in the messageReply.php script, I get an error saying such an index does not exist.
Try this:
echo '<input type="hidden" name="Mtitle" value='.$row['Mtitle'].'>';
Your Complete Code (Modified):
$threadNumber=$row["Tid"];
echo "<center><b>Message# ", $messageNumber , ": </b><br>";
echo $row["Mtitle"] , " in Thread# " , $row["Tid"] , "<br>";
echo "The Message was Written on " , $row["Mdate"] , '<br>';
echo $row["Mbody"];
echo "<br><br>";
echo '<form action="messageReply.php" method="post">';
echo '<textarea name="reply" rows=5 cols=30 placeholder="Reply to the Message?"></textarea>';
echo '<input type="hidden" name="Mtitle" value='.$row['Mtitle'].'>';
echo '<input type="submit" value="Send Message">';
echo '</form>';
Note: You are using php in php. Please change it below code:
<?php
echo '<input type="hidden" name="Mtitle" value="'.$row["Mtitle"].'">';
?>
Related
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
In the code bellow, I'm able to fetch values from the database. Now the retrieved values need to be passed to another form.
PHP:
<?php
require('administrator/connect-db.php');
$www_root = 'http://localhost/secure/cem/administrator/profile/';
$qry = mysql_query("SELECT * from dealer_package_details");
if(!$qry){echo mysql_error();}else{
while($row = mysql_fetch_array($qry)){
echo "<div class='col-sm-4 sm-margin-b-50'>";
echo "<form action='test.php'>";
echo "<div class='margin-b-20'>";
echo "<div class='wow zoomIn' data-wow-duration='.3' data-wow-delay='.1s'>";
echo '<img class="img-responsive" name="pport" src="', $www_root, '/', $row['pport'], '" alt="', $row['pport'], '"/>';
echo "</div>";
echo "</div>";
echo "<h3><a href='#' name='name_of_the_product'>" . $row['package_id'].$row['name_of_the_product']."</a></h3>";
echo "<input type='submit' name='submit' value='book now'>";
echo "</div>";
}
}
?>
The following code describes the second form where the values need to be passed.
HTML:
<form method="POST" action="insrt.php">
<input name="name_of_the_product" type="text" class="form-control name_of_the_product" value="<?php echo $_POST["name_of_the_product"]; ?>" readonly/>
<input name="package_id" type="text" class="form-control package_id" value="<?php echo $_POST["package_id"]; ?>" readonly/>
<input type="submit" name="submit" value="Save">
I suggest to use input hidden.
PHP:
...
echo "<input type='hidden' name='description' value='".$row['description]."' />";
...
HTML:
...
<input name="description" type="text" value="<?php echo $_POST['description']; ?>">
...
I am having a table with <input type="text" name="' . $r['0'] . '" value="' . $r['0'] . '"
populated from data that i fetch from database like this:
echo '<form id="actions" name="nonValidMainForm" method="post"><table border="2" width="100%">';
echo "<tr><td><b>Index</b></td><td><b>Email </b></td> <td><b>Date</b></td> <td><b>Name</b></td> <td><b>Surname</b></td> <td><b>Telephone Number</b></td> <td><b>Street Adress</b></td><br/>";
while($r = mysql_fetch_array($result)) {
$my[] = $r['0'];
echo '<tr>';
echo '<td>'.$roww++.'</td>';
echo '<td>
<input size="50%" type="text" name="' . $r['0'] . '" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</td>';
echo '<td>'.$r['1'].'</td>';
echo '<td>'.$r['2'].'</td>';
echo '<td>'.$r['3'].'</td>';
echo '<td>'.$r['4'].'</td>';
echo '<td>'.$r['5'].'</td>';
echo '</tr>';
}
echo "<pre>";
print_r($my);
echo "</pre>";
if(isset($_POST['unsubscribe'])){
foreach($my as $key=>$value){
$email = $value;
}
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
echo '<button style="position:fixed;bottom:5;left:5;">Change</button>';
echo '</table></form>';
The table looks like this:
I have tried this:
if(isset($_POST['unsubscribe'])){
$email = $POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
But the value is empty
So each time i press unsubscribe button the corresponding email to be deleted. How is this possible?
Your form has many elements with the same name. How can the browser determine which element's value to send to the server when the form is posted? Generally the last one takes precedence, but I suspect that behavior may be undefined and browser-specific.
If each individual table row needs to be a separately post-able form, then each row needs its own form:
echo '<td>
<form method="POST" action="somePage.php">
<input size="50%" type="text" name="email" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</form>
</td>';
That way when the browser posts the form to the server, it knows specifically which email and unsubscribe elements to use. Since there's only one of each for that form.
You have to wrap your inputs in a <form> tag.
echo '<form>';
while($r = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'.$roww++.'</td>';
echo '<td>
<input size="50%" type="text" name="email" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</td>';
echo '<td>'.$r['1'].'</td>';
echo '<td>'.$r['2'].'</td>';
echo '<td>'.$r['3'].'</td>';
echo '<td>'.$r['4'].'</td>';
echo '<td>'.$r['5'].'</td>';
echo '</tr>';
}
echo '</form>';
if(isset($_POST['unsubscribe'])){
$email = $POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
Based on your code above it looks like it's a syntax error. Try the update below
if(isset($_POST['unsubscribe'])){
$email = $_POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' ); </script>";
}
<form method="POST" action="include/crud.php" enctype="multipart/form-data" >
<?php
foreach (LoadAnouncements() as $value){
/*echo "<div id='bb'></div>";*/
echo "<hr/>";
echo $value['searchresultwhat'];
echo "<br/>\n";
echo $value['searchresultwhen'];
echo "<br/>\n";
echo $value['searchresultwhere'];
echo "<hr/>";
/*echo "<div id='bb'></div>";*/
}
?>
</form>
I have this form that show an echo is there a way that I can add the attribute name in this echo so I can use it to query the database? I search for ideas if putting attribute to echo is possible but I haven't found anything yet any suggestion is appreciated
You are looking it?
<input type="text" name="searchresultwhat[]" value="<?php echo $value['searchresultwhat']; ?>" />
<input type="text" name="searchresultwhen[]" value="<?php echo $value['searchresultwhen']; ?>" />
<input type="text" name="searchresultwhere[]" value="<?php echo $value['searchresultwhere']; ?>" />
Edited
<?php
echo '<table>';
echo '<tr><th>What </th><th> When</th><th> Where</th><tr>';
foreach (LoadAnouncements() as $value){
echo "<tr><td>".$value['searchresultwhat']."</td>
<td>"$value['searchresultwhen']."</td>
<td>".$value['searchresultwhere']."</td>
</tr>";
}
echo '</table>';
?>
Do you want to add attribute to certain value in PHP? But you can't do it because $value['searchresultwhat'] and others of the kind are simple strings - as is! They can't send separated data (or attributes) with themself.
EDIT:
You can send seprated data about certain value using array:
$value['searchresultwhat'] = array("name", "value");
$value['searchresultwhen'] = array("name2", "2014-09-01");
$value['searchresultwhere'] = array("name3", "kittens");
echo $value['searchresultwhat'][0] . " is " . $value['searchresultwhat'][1];
echo $value['searchresultwhen'][0] . " = " . $value['searchresultwhen'][1];
echo $value['searchresultwhere'][0] . " has " . $value['searchresultwhere'][1];
I have som problem writing back to table.
I connect to table as wanted and get the information output correctly. The input correctedby, and the checkboxes is saving as i want.
but i dont know how to write the mysql_fetch_assoc back to table
I have this kode;
<?php
session_start();
$_SESSION['mypassword']="myusername";
echo "Logged in as:<br>" .$_SESSION['myusername'];
include "header.inc.php";
include "funksjoner.inc.php";
//in this file the connetion to server
$connection= kobleTil(); //trenger ikke oppgi databasenavn
//Steg 2: SQL-query
$sql = "SELECT * FROM oppgave WHERE modulid=1 AND resultat is NULL ORDER BY RAND() LIMIT 1";
$result = mysql_query($sql, $connection);
echo "<hr>";
while ($nextrow= mysql_fetch_assoc($result)){
echo "answer: " . $nextrow['answer'];
echo "<br>Modulid: " . $nextrow['modulid'];
echo "<br>student: " . $nextrow['studentid'];
echo "<br>";
}
echo '<form name="input" action="tilretting.php" method="get">';
echo'<input type="text" name="correctedby" value="'.$_SESSION['myusername'].'">';
echo 'Not approved<input type="checkbox" name="resultat" value="0">';
echo 'Approved<input type="checkbox" name="resultat" value="1">';
echo '<input type="text" name="studentid" value="dont know how to write correct here">';
echo '<input class="levermodulknapp" type="submit" name="lever1" value="Lever modul 1">';
echo "</form>";
echo "<hr>";
?>
how can I get the form to get the value from mysql_fetch_assoc into the form?
Is the mysql_fetch_assoc the right thing to use?
Very gratefull for any tip!
$result = mysql_query($sql, $connection);
echo "<hr>";
while ($nextrow= mysql_fetch_assoc($result)){
echo "answer: " . $nextrow['answer'];
echo "<br>Modulid: " . $nextrow['modulid'];
echo "<br>student: " . $nextrow['studentid'];
echo "<br>";
echo '<form name="input" action="tilretting.php" method="get">';
echo'<input type="text" name="correctedby" value="'.$_SESSION['myusername'].'">';
echo 'Not approved<input type="checkbox" name="resultat" value="0">';
echo 'Approved<input type="checkbox" name="resultat" value="1">';
echo '<input type="text" name="studentid" value="'.$nextrow['columnName'].'">';
echo '<input class="levermodulknapp" type="submit" name="lever1" value="Lever modul 1">';
echo "</form>";
echo "<hr>";
}
or
$data = null;
$result = mysql_query($sql, $connection);
echo "<hr>";
while ($nextrow= mysql_fetch_assoc($result)){
echo "answer: " . $nextrow['answer'];
echo "<br>Modulid: " . $nextrow['modulid'];
echo "<br>student: " . $nextrow['studentid'];
echo "<br>";
$data = $nextrow['colunmName'];
}
echo '<form name="input" action="tilretting.php" method="get">';
echo'<input type="text" name="correctedby" value="'.$_SESSION['myusername'].'">';
echo 'Not approved<input type="checkbox" name="resultat" value="0">';
echo 'Approved<input type="checkbox" name="resultat" value="1">';
echo '<input type="text" name="studentid" value="'.$data.'">';
echo '<input class="levermodulknapp" type="submit" name="lever1" value="Lever modul 1">';
echo "</form>";
echo "<hr>";
}
?>