i have small problem and i don't see any mistakes in my code and server also don't returns any errors but clouse if (isset($_POST['accept'])) doesn't recognize that button was clicked.And is it some way to get value of button id?Please Help.
include '../../config.php';
db_connect();
$zapytanie = "SELECT * FROM messages ORDER BY message_id DESC";
$r = mysql_query ($zapytanie) or die(mysql_error());
$war="L".$_SESSION['lekarz_id'];
echo $war;
if (isset($_POST['accept']))
{
echo "something";
}
else{
while ($row = mysql_fetch_array ($r))
{
if($war == $row['message_recipient'] AND !$row['message_title']=='LP' ){
echo $war;
print "<table border=2 width=98% align=center>
<tr><td><font size=1><p align=left>Od: {$row['message_recipient']}</td> <td><p align=right>Wysłana: {$row['date_of_posting']}</align></font></td></tr>
<tr><td><p align=left><font size=1>Do:{$row['message_sender']}</td><td><b><p align=right>Temat:{$row['message_title']}</align></b></td></tr>
<tr><td colspan=2><br /> {$row['message_text']}</align></font></td></tr>
</table><hr />";
}
else if($war == $row['message_recipient'] AND $row['message_title']=='LP'){
echo "
<form>
<table border=2 width=80%>
<tr>
<td>Od:Wysłana: {$row['date_of_posting']}</td>
<td><p align=center>Wysłana: {$row['date_of_posting']}</align></font></td>
</tr>
<tr>
<td><p align=center><font size=5>Do:{$row['message_sender']}</td><td><b><p align=right>Temat:{$row['message_title']}</align></b>
</td>
</tr>
<tr>
<td ><br /> <font size=5>{$row['message_text']}</align></font>
</td>
</tr>
<tr>";
$id = $row['message_sender'];
$id=substr($id, -1);
print'
<td>
<form action="messages.php" method="post">
<input type="submit" name="accept" value="accept" id=.{$id}./>
</form>
</td>
<td> </td>
</tr>
</table>
</form><hr />
';
}
}
db_close($db);
}
?>
You didn't explicitly set the form action to POST. As a result your form values willbe sent via GET.
Change:
<form>
to
<form method="POST">
Or look for for your value in the $_GET superglobal.
if (isset($_GET['accept']))
You also have a form embedded within a second form. Removing the outer form would also solve your problem.
Check for $_REQUEST['accept']. This combines $_GET, $_POST and $_COOKIE. It will contain the form value regardless of submission method.
http://www.php.net/manual/en/reserved.variables.request.php
I also have to mention, that it's not a good idea to use submit button value to send parameter. What if you need to write "Accept" instead of "accept" on your button?
if ($_POST['accept']=='accept') will fail for such button. Better practice is to use hidden field with name="accept" (<input type="hidden" name="accept" value="any_value" />) and let submit button to do what it has to do - to submit form.
Also it makes your form reusable
phpfidle
Related
I have an php form utilizing several inputs that is driving a kiosk page. If a text input is blank I want this update a separate input with the word "hidden" If there is text I would like the word "visible" to show. Currently my code works if you click submit twice but will not work on the first submit. Here is my current code:
The if function that is current working on second submit:
if (strlen($something)>0) {
$_POST['someone'] = "visible";
} else {
$_POST['someone'] = "hidden";
}
input form:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<tr>
<td>something : </td>
<td><input type="text" id="something" name="something" value="<?php echo htmlspecialchars($something); ?>"/></td>
<td></td>
</tr>
<tr>
<td></td>
<td><?php echo $_SERVER['PHP_SELF']; ?></td>
<td></td>
</tr>
<tr>
<td>someone:</td>
<td><input type="text" id="someone" name="someone" value="<?php echo htmlspecialchars($someone); ?>"/></td>
<td></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name='submit' value="Submit"/></td>
<td></td>
</tr>
</table>
</form>
Here is the update code:
$usql = "UPDATE test SET something= '".$_POST['something']."', someone= '". $someone ."' WHERE ID='a';";
Currently the "someone" input has a display of none so it cannot be seen by the user. This is not necessary but if someone could tell me how to bypass adding an input altogether and tweak the update statement itself to update something that would be great as well! Thanks!
Any help would be appreciated!
Using session variables can save the page state, even after reload.
First page save:
session_start();
$_SESSION['someone'] = $_POST['someone'];
Then:
if(isset($_SESSION['someone']))
{
if (strlen($_SESSION['someone'])>0) {
$_POST['someone'] = "visible";
} else {
$_POST['someone'] = "hidden";
}
}
I'm having some difficulty with php forms.
I have created a page called 'post_details.php' (this simply displays a photo of a product & description). Each product has a unique id
Within posts_details.php, I have used to include command to include a form. This form allows users to send me feedback regarding the product.
For some reason the form is not workin. Everytime the submit button is clicked, the alert box warns me that I need to complete the form (even if the form is complete)
The last part of line one doesn't seem to work. It's not picking up the post_id
Can anyone please help ??
post a comment
<form method="post" action="post_details.php?post= <?php echo $post_id; ?>">
<table width "600">
<tr>
<td>Your email:</td>
<td><input type="text" name="comment_email"/></td>
</tr>
<tr>
<td>Your Comment:</td>
<td><textarea name="comment" cols="35" rows="16"/></textarea></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="postcom"/></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['comment'] )) {
$comment_email = $POST['comment_email'];
$comment = $POST['comment'];
if( $comment_email=='' OR $comment=='') {
echo "<script>alert('Please complete form')</script>";
echo "<script>window.open('post_details.php?post=post_id')</script>";
exit();
}
else {
echo "complete";
}
}
?>
You have error here
if(isset($_POST['comment'] )) {
$comment_email = $POST['comment_email'];
^
$comment = $POST['comment'];
^
....
Instead of $POST it must be $_POST['comment_email'] and $_POST['comment']
Sorry I'm a bit of a noob when it comes to PHP but I just wondered if someone had an idea on how I could solve this PHP/SQL problem.
I have a PDO statement that gets all users from a database.
With the array of users from the database I create a foreach loop to display all of the users in a table which I want to use to select a specific user, enter a number in the row of the user I select, then click submit and store the users name and also the number. I will use this information to populate another database later.
My question is, I cant seem to reference the user or the number in the table to extract the user and number I enter. When I try and request the numbered entered in the index.php, it will only ever display a number if I enter a number for a the final user in the table. When I try and view the FullName it never works and I get 'Undefined index: FullName' error.
I also specified to 'POST in the form but it doesnt seem to be doing that.
Does anyone have any ideas?
Thanks
//function.php
function getName($tableName, $conn)
{
try {
$result = $conn->query("SELECT * FROM $tableName");
return ( $result->rowCount() > 0)
? $result
: false;
} catch(Exception $e) {
return false;
}
}
//form.php
<form action "index.php" method "POST" name='form1'>
<table border="1" style="width:600px">
<tr>
<th>Name</th>
<th>Number Entered</th>
<tr/>
<tr>
<?php foreach($users as $user) : ?>
<td width="30%" name="FullName">
<?php echo $user['FullName']; ?>
</td>
<td width="30%">
<input type="int" name="NumberedEntered">
</td>
</tr>
<?php endforeach; ?>
</table>
<input type="submit" value="submit"></td>
</form>
//index.php
$users = getName('users', $conn);
if ( $_REQUEST['NumberedEntered']) {
echo $_REQUEST['NumberedEntered'];
echo $_REQUEST['FullName'];
}
The variable FullName isn't transmitted by your form to index.php. Only values of form elemnts are sent. You can add a hidden form field, that contains FullName like this:
<input type="hidden" name="FullName" value="<?php echo $user['FullName']">
But your second problem is, that your foreach loop will create several input fields with the exact same name. You won't be able to recieve any of the entered numbers, except the last one. have a look at this question for possible solutions.
Update
Putting each row in individual form tags should solve your problem:
<?php foreach($users as $user) : ?>
<form action="index.php" method="POST">
<tr>
<td align="center" width="40%" >
<?php echo $user['FullName']; ?>
<input type="hidden" name="FullName" value="<?php echo $user['FullName']; ?>" />
</td>
<td width="30%">
<input name="NumberedEntered"/>
</td>
<td>
<input type="submit" value="submit"/>
</td>
</tr>
</form>
<?php endforeach; ?>
I have an html table I've updated to use checkboxes to be able to delete multiple files:
<table>
<thead>
<tr>
<th>Camera Name</th>
<th>Date Created</th>
<th>Video Size</th>
<th>Video Length</th>
<th>
<button type="submit" class="deletebutton" name="delete_video" value="Delete" title="Delete the selected videos" onClick="return confirm('Are you sure you want to delete?')">Delete</button><br>
<input type="checkbox" name="radioselectall" title="Select All" />
</th>
</tr>
</thead>
<tbody>
<?php
for($i=0;$i<$num_videos;$i++)
{
//do stuff
//Note: I'm looping here to build the table from the server
?>
<tr >
<td onclick="DoNav('<?php echo $url; ?>');">
<?php echo $result_videos[$i]["camera_name"]; ?>
</td>
<td onclick="DoNav('<?php echo $url; ?>');">
<?php echo setlocalTime($result_videos[$i]["video_datetime"]); ?>
</td>
<td onclick="DoNav('<?php echo $url; ?>');">
<?php echo ByteSize($result_videos[$i]["video_size"]); ?>
</td>
<td onclick="DoNav('<?php echo $url; ?>');">
<?php echo strTime($result_videos[$i]["video_length"]); ?>
</td>
<td>
<form name="myform" action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>" method="POST">
<input type="checkbox" name="radioselect" title="Mark this video for deletion"/>
<input type="hidden" name="video_name" value="<?php echo $result_videos[$i]["video_name"]; ?>" />
</form>
</td>
</tr>
I started with first creating some jquery code to create a select all/deselect all button in the table heading and just a test to show I can find which boxes are checked. That all works:
//selectall checkboxes - select all or deselect all if top checkbox is marked in table header
$("input[name='radioselectall']").change(function()
{
if( $(this).is(':checked') )
{
$("input[type='checkbox']","td").attr('checked',true);
}
else
{
$("input[type='checkbox']","td").attr('checked',false);
}
});
//process checkboxes - which ones are on
$(".deletebutton").click(function() {
$("input[name='radioselect']:checked").each(function(i){
alert(this.value);
});
});
So the part where I'm stuck is I don't know where to go from here. I need to pass all the video_names of all the videos selected (with the checkboxes). video_name is part of a hidden field in my form. So I need to pass that to my php function when the delete button is selected. Not really sure how to tackle this. Hope this makes sense.
Simple solution maybe:
Change your checkboxes to have a value of 1 and a name of $result_videos[$i]["video_name"]; in the table rows, make the form encapsulate the whole table.
Then on submit you can do something like:
foreach ($_POST as $key => $value) {
//Delete $key (the name) where $value == 1
}
Your approach is fine if you want to access the hidden fields through jQuery at a given point but once you submit the form and lose the DOM, the PHP processing page will have no way to relate the selected checkboxes with the hidden fields as there is no consistent naming scheme.
One approach would be to use the loop counter to suffix the two in order to pair them up:
<input type="checkbox" id="radioselect_<?= $i ?>" title="Mark this video for deletion" value="1" />
<input type="hidden" id="video_name_<?= $i ?>" value="<?php echo $result_videos[$i]["video_name"]; ?>" />
This would be good if you want to relate more than the two fields. Otherwise, you could just use the video_name as the value of the checkbox, as Ing suggested.
I have two forms in the page one is filter, second is the item list, when I apply the filter to item list form, selected values reset to default. For filter I can save selected values, because I submit this form via POST method, but other remains not submited is possible to save selected that form values after page refresh? Here is some code of second form:
<form name="forma" method="post" onMouseOver="kaina();" onSubmit="return tikrinimas()" action="pagrindinis.php?page=generuoti.php">
<table width="540" border="1" align="center">
<tr>
<td>Client:</td>
<td>MB:</td>
<td>Price:</td>
</tr>
<tr>
<td>
<?php
$query="SELECT name,surname,pers_code FROM Clients";
mysql_query("SET NAMES 'UTF8'");
$result = mysql_query ($query);
echo "<select name=Clients id='clients'>";
echo "<OPTION value=''>- Choose -</OPTION>\n";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[pers_code]>$nt[name] $nt[surname]</option>";
}
echo "</select>";
?></td>
</tr>
</form>
You need to set the selected attribute of your select element based on what is in $_POST. Something like:
$selected = $_POST['Client'];
while($nt=mysql_fetch_array($result)){
if ($selected == $nt[pers_code]) {
echo "<option value=$nt[pers_code] selected="selected">$nt[name] $nt[surname]</option>";
}
else {
echo "<option value=$nt[pers_code]>$nt[name] $nt[surname]</option>";
}
}
Also note that you should probably sanitize any values you get from $_POST.
Unfortunately there is no easy way to do this, and it's something that PHP and web developers in general have maligned for years, because repopulating form fields is never easy AND clean.
Values in the form you aren't submitting wont be sent (via GET or POST), so you're left with writing a custom workaround.
While it's a bit Q&D, I would recommend sending an Ajax call on form submit to store the values for your second form in the $_SESSION variable.
I'm sorry to say there's no easy alternative - due to the stateless nature of HTTP requests, this is something that every programmer has to struggle with.
just to break the question down to a more simplified form, let's assume we don't have the hassle of working with dropdowns. The real issue you're having is to take a value from form 1 and have it work even after form 2 has been submitted.
If you use a hidden field on form 2, of the same name as form 1, and populate it based on both form 1 and form 2's POST data, it will be "remembered"
<? // mostly html with a bit of php. ?>
<form id="f1" method="POST">
<input type ="text" name="f1val" value="<?= htmlspecialchars( array_key_exists( 'f1val', $_POST )?$_POST['f1val']:'' ); ?>">
<input type="submit">
</form>
<form id="f2" method="POST">
<input type="hidden" name="f1val" value="<?= htmlspecialchars( array_key_exists( 'f1val', $_POST )?$_POST['f1val']:'' ); ?>">
<input type ="text" name="f2val" value="<?= htmlspecialchars( array_key_exists( 'f2val', $_POST )?$_POST['f2val']:'' ); ?>">
<input type="submit">
</form>
<script type="text/javascript" src="js/jquery1.6.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#forma").submit(function(event) {
var 1stformfield = $form.find( 'input[name="misc"]' ).val();
/* stop form from submitting normally */
//event.preventDefault();
$.post("1stformsubmit.php", $("#forma").serialize());
//after it posts you can have it do whatever
//just post code here
$('#iframe1').attr('src', 'iframepage.php?client=' + 1stformfield);
window.frames["iframe1"].location.reload();
return false;
});
});
</script>
<form name="1stform" method="post" action="/">
<input type="text" name="misc" id="misc" />
<input type="submit" name="submit" id="submit" value="submit 1st form"/>
</form>
<iframe id="iframe1" src="" scrolling="no" ></iframe>
iframe page
<form name="forma" method="post" onmouseover="kaina();" action="/">
<table width="540" border="1" align="center">
<tr>
<td>Client:</td>
<td>MB:</td>
<td>Price:</td>
</tr>
<tr>
<td>
<?php
$selected = $_GET['Client'];
$query="SELECT name,surname,pers_code FROM Clients";
mysql_query("SET NAMES 'UTF8'");
$result = mysql_query ($query);
echo "<select name=Clients id='clients'>";
echo "<OPTION value=''>- Choose -</OPTION>\n";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[pers_code]>$nt[name] $nt[surname]</option>";
}
echo "</select>"; ?></td>
</tr>
</form>
This will post all inputs inside your form to the file you specify then you can have it do whatever you like, for example show a thank you message..Hope it helps.