Hi I put a sample code I've done below to explain my concern. Simply the code works fine except displaying the 'echo' messages in a desired location on the page. when the code is executed, the echo messages appear just on the page, but I need the message to be appear in the 'cell4' table cell.
I tried modifying the code as {echo "Name is blank!"; return false;} on the line 30, but seems that its not a correct method as no change in the result. Can someone please tell me is there a workaround for this? Thanks for looking.
Here goes the code:
<html>
<head><title>Test</title></head>
<body>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>">
<table border="1">
<thead>
<tr>
<td colspan="4">User detail</td>
</tr>
</thead>
<tr>
<td id="cell1">Name:</td>
<td id="cell2"><input type="text" name="Uname"></td>
<td id="cell3"><input type="submit" name="SendNow" value="Save"></td>
<td id="cell4"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['SendNow']))
{
$nm = $_POST['Uname'];
if($nm == "")
{echo "Name is blank!"; return false;}
else
{echo "Well done!"; return false;}
}
?>
Sorry the edited code has not appeared correct in the above text. This is how I have tried modifying : {echo "<td id='cell4'>Name is blank!</td>"; return false;}
<?php
if(isset($_POST['SendNow']))
{
$nm = $_POST['Uname'];
$message = '';
if($nm == "")
{$message = "Name is blank!";}
else
{$message = "Well done!";}
}
?>
<html>
<head><title>Test</title></head>
<body>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>">
<table border="1">
<thead>
<tr>
<td colspan="4">User detail</td>
</tr>
</thead>
<tr>
<td id="cell1">Name:</td>
<td id="cell2"><input type="text" name="Uname"></td>
<td id="cell3"><input type="submit" name="SendNow" value="Save"></td>
<td id="cell4"><?php if (!empty($message)) echo $message; ?></td>
</tr>
</table>
</form>
</body>
</html>
PHP code is inserted into the spot of the document where you execute the code. That means you need to do your processing more like this:
<?php
if (isset yada yada)
$cell_value = ...
}
?>
<body>
<table>
<tr>
<td><?php echo $cell_value ?></td>
</tr>
</table>
</body>
PHP will not "reach back" and change something that's already been output.
Related
I am having an issue on designing a web page. I want to display a POST variable inside of an HTML table.
Here is the form code from the first page:
<form action="buy.php" method="post">
<input type="text" name="uid" />
<input type="submit" value="Buy Now" />
</form>
This code works fine if I am displaying the POST variable on a normal blank PHP file.
But when I go to use it in an html table it just won't display.
Here is the table code:
<td id="bal"><?php echo $_POST['uid']; ?></td>
<td id="amt">test1</td>
<td id="type">test2</td>
The first tabledata just appears blank.
Can anyone help me fix this?
Here is the entire code in the buy.php file: http://pastebin.com/ffWAP92C
(was having trouble posting it in here )
This is what the problem looks like:
Change this <td id="bal"> <?php echo "$_POST['uid'];" ?> </td> to <td id="bal"> <?php echo $_POST['uid']; ?> </td>
Try this one,
<html>
<head>
<title></title>
<style type="text/css">
</style>
</head>
<body>
<center><h1>Purchase Account ID</h1></center>
<table border="1" style="width:100%;">
<tr>
<td><b>Account ID</b></td>
<td><b>Account Type</b></td>
<td><b>Account Price</b></td>
</tr>
<tr>
<td id="bal">
<?php
if(isset($_POST['uid']))
echo $_POST['uid'];
else
echo "Nothing";
?>
</td>
<td id="amt">test1</td>
<td id="type">test2</td>
</tr>
</table>
</body>
</html>
Hope this works.
Remove " signs inside php tag. i.e. replace
<td id="bal"> <?php echo "$_POST['uid'];" ?> </td>
with
<td id="bal"> <?php echo $_POST['uid']; ?> </td>
OR insert semicolon after " sign. i.e.
<td id="bal"> <?php echo "$_POST['uid']"; ?> </td>
you can copy past below code in a test.php file. i have updated code. It is working in my localhost.
<html>
<head>
<title></title>
<style type="text/css">
</style>
</head>
<center><h1>Purchase Account ID</h1></center>
<body>
<table border="1" style="width:100%;">
<tr>
<td><b>Account ID</b></td>
<td><b>Account Type</b></td>
<td><b>Account Price</b></td>
</tr>
<tr>
<td id="bal"> <?php if(isset($_POST['uid'])) echo $_POST['uid']; ?> </td>
<td id="amt">test1</td>
<td id="type">test2</td>
</tr>
</table>
</body>
<form action="test.php" method="post">
<input type="text" name="uid" />
<input type="submit" value="Buy Now" />
</form>
</html>
I'm trying to get a checkbox form to delete entries from the database/ftp server but I just can't get the delete button to delete something. Probably because I'm missing out on something as the error reporting keeps telling that the variable delete isn't determined but I can't find why it says that.
This is my code,
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
// Get our database connector
require("includes/conn.php");
?>
<?php
$sql="SELECT * FROM people ORDER BY ID";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Selecteer welke auto('s) verkocht zijn</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Foto</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Merkt/Type</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Beschrijving</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
echo "<div class=\"picture\">";
echo "<p>";
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo "<img src=\"content/uploads/" . $rows['filename'] . "\" alt=\"\" height=\"125\" width=\"200\" /><br />" . "<br />"; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['fname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lname']; ?></td>
</tr>
<?php
echo "</p>";
echo "</div>";
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM people WHERE id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
Another problem I'm having with that code is that I want it to delete the image from the FTP too, not only the file name entry in the database.
P.S. I know about MySQL being deprecated, I'm going edit that after I have the script working like I want it too. Didn't have the time yet to take a look at PDO and MySQLi.
Try this:
<?php
if(isset($_POST['delete'])){ // Check delete button is clicked
foreach($_POST['checkbox'] as $del_id){ // loop only checked items and delete
$sql = "DELETE FROM people WHERE id='$del_id'";
$result = mysql_query($sql);
}
}
?>
and also what ever your logic you have written for delete move to the top of the page before the html code for your desired output.
You're trying to use variable delete although it's not declared.
Try with
<?php
// Check if delete button active, start this
if($_POST['delete']){
// Delete the record
}
?>
<form action="" method="post">
<input name="delete" type="submit" id="delete" value="Delete">
</form>
I'm creating a page with a PHP form, and it works well. But the problem is when I hit the checl button check it gives answers in the white page, not in the same html tabl.
I want it to gives the submission in the same table style , as well as add a new button to the form that shows answers without taking the exercise.
And this is an example, showing want I'm trying to do:
http://www.englisch-hilfen.de/en/exercises/questions/simple_present.htm
<?php
$correctSolution1 = 'do';
$correctSolution2 = 'do';
if(isset($_POST['add']) == TRUE) {
$solution1 = $_POST['solution1'];
if(empty($solution1) == TRUE) {
echo '<span style="color: blue;">"unanswered"</span> you like lemon?</br>';
} else if ($solution1 == $correctSolution1) {
echo '<span style="color: green;">'.$solution1.'</span> you like lemon?</br>';
} else {
echo '<span style="color: red;">'.$solution1.'</span> you like lemon?</br>';
}
$solution2 = $_POST['solution2'];
if ( empty($solution2) == TRUE ) {
echo '<span style="color: blue;">"_____"</span> you like lemon? <img src="http://www.pavendors.com/images/smileyface.gif" width="16px" height="16px" border=0></br>';
} else if ( $solution2 == $correctSolution2 ) {
echo '<span style="color: green;">'.$solution2.'</span> you like lemon?</br>';
} else {
echo '<span style="color: red;">'.$solution2.'</span> you like lemon?</br>';
}
echo '<input type="button" value="Repeat test" />';
} else {
?>
<form method="post" autocomplete="off">
<table align=center width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<table align=left width="100%" bgcolor="#FFCC99" border="0" cellspacing="0" cellpadding="1">
<tr>
<td>
<table width="100%" bgcolor="#FFFFCC" cellspacing="4" cellpadding="1">
<tr></tr>
<tr><td></td></tr>
<tr>
<td>
<table> <tr><td colspan="2">1) <input type="text" name="solution1" value="" size="6" /> you <input type="text" class="ex_textfield" id="1" tabindex="1" name="solution2" value="" size="6" /> mineral water? <b><i>(to drink)</i></b></td></tr>
</table>
</td>
</tr>
<tr><td height="1"><hr></td></tr>
<tr>
<td align="left"><input class="small_button" type="submit" name="add" value="Check" />
<br>
<hr> </td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
<?php
}
?>
You should look in to embeeding php inside html:
In example above you are doing :
<?php
$correctSolution1 = 'do';
$correctSolution2 = 'do';
if(isset($_POST['add']) == TRUE) {
}
else{?>
<form>
<table></table>
</form>
<?php }?>
So you code here does not go to form and table part when you do a submit(post request) through from.
You can either add table part to the post if statement part as well like below:
<?php
$correctSolution1 = 'do';
$correctSolution2 = 'do';
if(isset($_POST['add']) == TRUE) {
//do your php stuffs here
?>
<table>
...
your html you can include values in to it like this <?php echo $solution2;?>
...
</table>
<?php
}
else{?>
<form>
<table></table>
</form>
<?php }?>
OR you can do this as well:
<?php
//php part here may be some variable assignments
$correctSolution1 = 'do';
correctSolution2 = 'do';
?>
<form>
<table>
....
<?php if(isset($_POST['add']) == TRUE) {?>
your other code here for result
<?php }?>
....
</table>
</form>
So basically you will need to understand properly about embedding php in to html.
You may look in to alternative syntax in php for statements like if, while, for, switch;
http://php.net/manual/en/control-structures.alternative-syntax.php
you can do things like:
<?php if(true):?>
//if code here
<?php endif;?>
guys I have the following script for adding, editing and deleting content in mysql, and showing it at index.php file. So here is my index.php file:
<script>function goDel()
{
var recslen = document.forms[0].length;
var checkboxes=""
for(i=1;i<recslen;i++)
{
if(document.forms[0].elements[i].checked==true)
checkboxes+= " " + document.forms[0].elements[i].name
}
if(checkboxes.length>0)
{
var con=confirm("Are you sure you want to delete");
if(con)
{
document.forms[0].action="delete.php?recsno="+checkboxes
document.forms[0].submit()
}
}
else
{
alert("No record is selected.")
}
}
function selectall()
{
// var formname=document.getElementById(formname);
var recslen = document.forms[0].length;
if(document.forms[0].topcheckbox.checked==true)
{
for(i=1;i<recslen;i++) {
document.forms[0].elements[i].checked=true;
}
}
else
{
for(i=1;i<recslen;i++)
document.forms[0].elements[i].checked=false;
}
}
</script>
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
-->
</style>
</head>
<body>
<table width="775" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td><hr size="1" noshade></td>
</tr>
<tr>
<td>
<form action="" method="post" name="" id="">
<table width="600" border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td><input name="topcheckbox" type="checkbox" class="check" id="topcheckbox" onClick="selectall();" value="ON">
Select All </td>
<td colspan="3" align="center">Add New Branch </td>
</tr>
<tr>
<td><strong>Delete</strong></td>
<td><strong>Branch Name </strong></td>
<td><strong>Short Name </strong></td>
<td><strong>Update</strong></td>
</tr>
<?
include("conn.php");
$sql="select sn,branchname,shortname from $branch order by sn";
$result=mysql_query($sql,$connection) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
?>
<tr>
<td><input name="<? echo $row['sn']; ?>" type="checkbox" class="check"></td>
<td><? echo $row['branchname']; ?></td>
<td><? echo $row['shortname']; ?></td>
<td>Update</td>
</tr>
<? } ?>
</table>
</form></td>
</tr>
</table>
Here is my delete.php file:
<?php
include("conn.php");
$recsno=$_GET["recsno"];
$data=trim($recsno);
$ex=explode(" ",$data);
$size=sizeof($ex);
for($i=0;$i<$size;$i++) {
$id=trim($ex[$i]);
$sql="delete from $branch where sn='$id'";
$result=mysql_query($sql,$connection) or die(mysql_error());
}
header("location: index.php");
?>
The problem is when I check 1 row, and click delete, it is deleting all of the rows, like I've clicked select all (which I didn't do). Thank you in advance.
It seems Delete query not getting value of $id, you have to store values in $id.
just GET id values
$id = $_REQUEST["sn"];
then your delete query
$sql="delete from $branch where sn='$id'";
I have a couple of problems. I'm creating a form inside a table, from what I understand this is not a good idea. But because of the loop I want to make sure the table header is outside so it doesn't repeat. Is there a smarter way to do this?
Also more importantly I can't seem to get the delete button to remove the correct video. It seems to delete the last one in the list. Something wrong with how I'm looping over this?
<p>
<h3>Recorded Videos</h3>
<table id="webcam-table">
<thead>
<tr>
<td>Camera Name</td>
<td>Video Size</td>
<td>Date Created</td>
<td>Video Length</td>
<td>Video Options</td>
</tr>
</thead>
for($i=0;$i<$num_videos;$i++)
{
<form action="<?php htmlentities($_SERVER['PHP_SELF']);?>" method="POST">
<input type="hidden" name="video_id" value="<?php echo $result_videos[$i]["video_id"]; ?>" />
<tbody>
<tr>
<td>
<?php echo $result_videos[$i]["camera_name"]; ?>
</td>
<td>
<?php echo $result_videos[$i]["video_size"]; ?>
</td>
<td>
<?php echo $result_videos[$i]["video_datetime"]; ?>
</td>
<td>
<?php echo $result_videos[$i]["video_length"]; ?>
</td>
<td>
<input type="submit" name="delete_video" value="Delete" onClick="javascript:return confirm('Delete this video?');"/>
</td>
</tr>
</tbody>
}
echo "</table>";
echo "</form>";
echo "</p>";
}
}
if (isset($_POST['delete_video'])) {
$video_id = $_POST['video_id'];
$query_delete_video = 'DELETE FROM `#__videos` WHERE `video_id`='.$video_id;
$db->setQuery($query_delete_video);
$db->query();
header("location: " . $_SERVER['REQUEST_URI']);
In your loop you are creating the 'form' tag. However you are not closing it each time. This is causing your deleting problem.
Move
echo "</form>";
Inside the loop.
looks good to me, the only issue I see is that the tag should be outside the loop (opening before, closing after).
Revised code
<?
if (isset($_POST['delete_video']))
{
$video_id = $_POST['video_id'];
$query_delete_video = 'DELETE FROM `#__videos` WHERE `video_id`='.$video_id;
$db->setQuery($query_delete_video);
$db->query();
header("location: " . $_SERVER['REQUEST_URI']); //you should not allow things to be echoed before a header()
}
?>
<script type="text/javascript">
function onMySubmit(video_id)
{
document.myform.video_id.value = video_id;
return confirm('Delete this video?');
}
</script>
<p>
<h3>Recorded Videos</h3>
<!-- you had <?php htmlentities(...) ?>, you should have had
<?php echo htmlentities(...) ?> or <?= ... ?> -->
<form name="myform" action="<?= htmlentities($_SERVER['PHP_SELF']);?>" method="POST">
<input type="hidden" name="video_id" value="" />
<table id="webcam-table">
<thead>
<tr>
<td>Camera Name</td>
<td>Video Size</td>
<td>Date Created</td>
<td>Video Length</td>
<td>Video Options</td>
</tr>
</thead>
<tbody>
<? for($i=0;$i < $num_videos;$i++) { ?>
<tr>
<td><?= $result_videos[$i]["camera_name"]; ?></td>
<td><?= $result_videos[$i]["video_size"]; ?></td>
<td><?= $result_videos[$i]["video_datetime"]; ?></td>
<td><?= $result_videos[$i]["video_length"]; ?></td>
<td><input type="submit" name="delete_video" value="Delete" onClick="javascript:return onMySubmit(<?=$result_videos[$i]["video_id"];?>);"/></td>
</tr>
<? } ?>
</tbody>
</table>
</form>
</p>