Submit form of Iframe from the parent window - php

<iframe id="frame1" name="frame1" align="center" src="committee_assign1.php" height="400" width="700">
</iframe>
<center><input onClick="submitiframeform(); return false;" type="button" name="submit" value="Submit" />
<script type="text/javascript">
function submitiframeform(){
window.frames['frame1'].document.forms['fypassign'].submit();
}
</script>
The above is the main page name committee_assign.php ..
And below is the page where the iframe called committee_assign1.php.
<?php
include '../database.php';
include 'valid_login.php';
if(isset($_POST['submit'])) {
$continue = FALSE;
$i = 0;
while ($continue == FALSE) {
if (isset($_POST['id_'.$i])) {
$fypcomm = $_POST['fypcomm_'.$i];
$user = $_POST['id_'.$i];
$sql = mysql_query(" UPDATE Lecturer SET LectFypCommittee = '$fypcomm' WHERE LectID = '$user' ")
or die(mysql_error());
mysql_query($sql);
} else
{$continue = TRUE;}
$i++;
}
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.location.href='../committee/committee_assign1.php'
</SCRIPT>");
}
?>
<head>
</head>
<body>
<form id="fypassign" name="fypassign" method="post" action="" target="_self" onSubmit="">
<?php
$counter = 0;
echo "<table class ='box'>";
echo "<thead>";
echo "<tr>";
echo "<th align='left' valign='top'>"."Lecturer Name"."</th>";
echo "<th align='left' valign='top'>"."FYP Committee"."</th>";
echo "</tr>";
$sql = mysql_query(" SELECT * FROM Lecturer ORDER BY LectFypCommittee DESC, LectName ASC ") or die(mysql_error());
while($info = mysql_fetch_assoc($sql)) {
$idcount = "id_".$counter;
echo "<input type='hidden' name='$idcount' id='$idcount' value={$info['LectID']} />";
echo "<tr>";
echo "<td>";
echo $info['LectName'];
echo "</td>";
echo "<td>";
$formname = "fypcomm_".$counter;
echo "<select name='$formname'>";
//to convert the flag value to user understandable language
if ($info['LectFypCommittee'] == '0'){
$dbfyp = 'No';
}
else $dbfyp = 'Yes';
echo "<option selected='selected' value='{$info['LectFypCommittee']}'>".$dbfyp."</option>";
if ($info['LectFypCommittee'] == '0'){
echo "<option value='1'>".'Yes'."</option>";
}
else echo "<option value='0'>".'No'."</option>";
echo "</select>";
echo "</td>";
echo"</tr>";
$counter++;
}
echo "</table>";
?>
</form>
</body>
I clicked submit button at the parent page and the page refresh but the value is not update.
Can anyone here guide me on this please?
So sorry to post such long codes as I hope you guys could understand more what I am doing. TQ

You have no input in your committee_assign.php named submit:
if(isset($_POST['submit']))
You must check with something like this:
if(isset($_POST))

You are checking if submit has been posted but you dont have an input named submit. Add an input named submit with some value and check if that post submit exists. You can make it hidden if you dont want to see an extra input.
if($_POST['submit']) is checking if there is a value with key 'submit' in the post array. All the key and value in $_POST array is name and value of a form element respectively.

Related

update data when click a button on each row

I'm a newbie programmer. I need to select then update data when click a button on each row. now I can select and show data but just first update button is working. I think cause of this issue is button id is same. I can't figure out how to create unique id. I try to change id="updatedb" to id="$i" and nothing working
my script is :
$(function(){
$('#updatedb').click(function(){
var a = $('#updatedb').attr('id');
$('#textstatus').val(a);
alert ($('#updatedb').attr('id'));
});
});
and php is :
$sql = $connectdb->query($checknode);
$i = 0;
echo "<table border=1>";
if (!$sql) {
echo $connectdb->error;
else {
while ($row=$sql->fetch_row()) {
$crow = "updatedb".$i;
//echo $crow."<br />";
echo "<form method='post' action=''>";
echo '<tr><td>'.$row[0].'
</td><td>'.$row[1].'</td><td>'.$row[2].'
</td><td>'.$row[3].'</td><td>'.$row[4].'
</td><td>'.$row[5].'</td><td>
<input type="button" id="updatedb" name="updatedb" value="update">
<input type="text" id="textstatus" name="textstatus"
value="" disabled="disabled"></td></tr>';
$i++;
}
}
echo "</form>";
echo "</table>";
You can use class rather then id for button to update see the code below
jquery
$(function(){
$('.updatedb').click(function(){
var a = $(this).attr('class');
$(this).parents('tr').find('.textstatus').val(a);
alert (a);
});
});
php
$sql = $connectdb->query($checknode);
$i = 0;
echo "<table border=1>";
if (!$sql) {
echo $connectdb->error;
}else
{
while ($row=$sql->fetch_row()) {
$crow = "updatedb".$i;
//echo $crow."<br />";
echo "<form method='post' action=''>";
echo '<tr><td>'.$row[0].'
</td><td>'.$row[1].'</td><td>'.$row[2].'
</td><td>'.$row[3].'</td><td>'.$row[4].'
</td><td>'.$row[5].'</td><td>
<input type="button" class="updatedb" name="updatedb" value="update">
<input type="text" class="textstatus" name="textstatus"
value="" disabled="disabled"></td></tr>';
$i++;
}
}
echo "</form>";
echo "</table>";

Link a href to result of PHP search script

I have a simple search script like this and works fine:
<html>
<head>
<title>any</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script>
function check() {
var searchtext = document.getElementById("txt").value;
if(searchtext == '') {
alert('Enter string to search');
} else {
document.myform.submit();
}
}
function reset_table() {
document.myform.submit();
}
</script>
</head>
<body>
<?php
error_reporting(0);
require_once "config.php";
$link = mysql_connect($hostname, $username, $password);
$dbcon = mysql_select_db($dbname);
echo "<div align='center' class='resp_code'>";
echo "<h5><b>...</b></h5>";
echo "<div align='center'>";
echo "<form method='post' action='index.php' id='searchform' class='frms' name='myform'>
<input type='text' id='txt' name='name' autocomplete='off'>
<input type='submit' name='submit' value='Search' id='search' onclick='check();'>
<input type='submit' name='reset' value='Reset' id='reset' onclick='reset_table()'>
</form> ";
echo "</div>";
echo "<div align='center'>";
$search = $_POST["name"];
if(isset($_POST['name'])) {
echo "<table class='table'>
<tr>
<th>No.</th>
<th>Un.</th>
<th>img</th>
</tr>";
$qry = mysql_query('SELECT * FROM rs_posts WHERE id like "%'.$search.'%"');
$count = mysql_numrows($qry);
if($count > 0) {
if($qry) {
while($row = mysql_fetch_row($qry)) {
$num = $row['id'];
$uniq = $row['unique_id'];
$img = $row['featured_img'];
echo "<tr>
<td id='num'>$row[0]</td>
<td id='uniq'>$row[1]</td>
<td id='img'>$row[25]</td>
</tr>";
}
} else {
echo "No";
}
} else{
$op = "No Results Found";
}
}
echo "</div>";
echo "<div style='font-weight:bold;color:red;'>$op</div>";
echo "</div>";
?>
</body>
</html>
and have a link code like this:
link
I need some of search results like
<td id='img'>$row[25]</td>
link to that. What's the most simple way to do that?
If accept alert or click on RESET button it show all content of my db but I need hide them when I don't input anything or click RESET button. How can resolve I that?

PHP - Button to delete Item from MySQL

I am coding some video upload script and I am with the admin panel right now.
There I have a List with all Videos. And each video has one delete button on the right side.
When I click the button then this video should be deleted from database but its not working after I click the button nothing happens.
<?php
$query = mysql_query("SELECT * FROM `videos`");
while($row = mysql_fetch_assoc($query))
{
$id = $row['id'];
$name = $row['name'];
echo "<a href='watch.php?id=$id'>$name</a><br />
<input type='submit' name='remove' value='Delete'<br />";
}
if (isset($_POST['remove']))
{
foreach ($_POST['id'] as $the_id)
{
if (!mysql_query("DELETE FROM videos WHERE id = '$the_id'"))
{
echo mysql_error();
}
}
}
?>
Of course on the header I have the mysql connect query. This is just the php code for listing all videos and try to delete.
Here is an example of doing this with MySQLi, including binding parameters to avoid SQL injection -
if (isset($_POST['remove'])) {
$remove = $mysqli->prepare("DELETE FROM `videos` WHERE `id` = ?");
$vid_id = $_POST['vid_id'];
$remove->bind_param('s', $vid_id);
if(!$remove->execute() === true) {
echo $mysqli->error;
}
}
$query = "SELECT * FROM `videos`";
if ($result = $mysqli->query($query)) {
while($row = $result->fetch_object()){
$id = $row->id;
$name = $row->name;
echo "<a href='watch.php?id=$id'>$name</a><br />";
echo "<form name='delete_vid' method='post'>";
echo "<input type='hidden' name='vid_id' value='$id'>";
echo "<input type='submit' name='remove' value='Delete'<br />";
echo "</form>";
}
} else {
echo mysqli_error($connection);
}
$result->close();
Of course you will have to provide a $connection` to the database, but thsi should get you started not only with MySQLi but with adding a form for each video.
More on SQL Injection
Maybe the problem is in the html, each delete button must be in and independent form, with a hidden input with the id too.
echo "<a href='watch.php?id=$id'>$name</a><br />
<form method='post'><input type='hidden' value='$id'><input type='submit' name='remove' value='Delete'<br /></form>";
<form method="post" >
<?php
$query = mysql_query("SELECT * FROM `videos`");
while($row = mysql_fetch_assoc($query))
{
$id = $row['id'];
$name = $row['name'];
echo "<a href='watch.php?id=$id'>$name</a><br />
<button name='id' value='".$id."' type='submit' >Delete</button>
<br />";
}
if (!mysql_query("DELETE FROM videos WHERE id = '".$_POST['id']."'"))
{
echo mysql_error();
} else {
echo 'successfully deleted';
}
?>
</form>

previously submitted form data disappears when new form is submitted

I have multiple forms on a single page and all gets redirected to same page when form is submitted but the previously submitted form values disappears when new form is submitted.
I tried using sessions didn't worked for me what else? please help
<script language="javascript">
function row(x,y){
//var len=document.forms[x].name.value;
if(x.value.length >=3)
{
//alert("Message form no> "+y+"will be submited");
document.forms[y].submit();
}
}
</script>
</head>
<body >
<center>
<h2>Database App</h2>
<table>
<tr>
<th><lable>Name :</label></th>
<th><label>E_Id :</label></th>
<th><label>Email :</label></th>
<th><label>Other Info :</label></th></tr>
<tr>
<?php
error_reporting(E_ALL ^ E_NOTICE);
// code check for name in database and if exists,displays in table row
//for($i=0;$i<150;$i++)
//{
//$E_id=array();
if($_POST){
$i = $_GET["uid"];
//echo "fhwefwej==".$i;
$x='name'.$i;
// echo 'dasvds'.$x;
if($_POST[$x])
{
$name = strtolower($_POST[$x]);
$E_id[$i] = "";
$Email[$i] = "";
$Otherinfo[$i] = "";
$con = mysql_connect('localhost','root','') or die("npt");
$db = mysql_select_db("trainee")or die("nptdff");
$query = "Select * from reguser where fname like '".$_POST[$x]."%'";
$result = mysql_query($query);
mysql_num_rows($result);
if(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_array($result))
{
$str=$row['fname'];
$initials = strtolower(substr($str,0,3));
if($name == $initials)
{
//echo "exist"."<br>";
$E_id[$i]= $row['fname'];
$Email[$i]=$row['lastname'];
$Otherinfo[$i]=$row['address'];
break;
}
}
}
else
{
$msg[$i] = "no user with these initials";
}
mysql_close($con);
}
}
for($i=0;$i<150;$i++)
{
//session_start();
//echo session_name($i)."<br>";
echo "<form name='form$i' action='new2.php?uid=$i' method='post'>";
echo "<td><input type='text' name='name$i' id='name$i' onkeyup='row(this,$i);' /><br />";
echo "<span id='availability_status' >";
if($_POST[$x]){echo $msg[$i];}
echo "</span> </td>";
echo "<td><input type='text' name='E_id' id='E_id' value='";
if(isset($_POST[$x])){ echo $E_id[$i];}
echo "' disabled='disabled' />";
echo "</td>";
echo "<td><input type='text' name='email' id='email' value='$Email[$i]' disabled='disabled' />";
echo "</td>";
echo "<td><input type='text' name='otherinfo' id='otherinfo' value='$Otherinfo[$i]' disabled='disabled' />";
echo "</td></tr>";
echo " </form>";
}
//echo '<script language="javascript">document.getElementById(\'name0\').focus();</script>';
?>
</table>
</center>
</body>
</html>
Why don't you Use AJAX. It will help to keep you posed different form information in back-end
you can either store those information in database or in file.
It will be the best way.

Remove Delete Button for Wrong User?

I currently have a guestbook style thing that users post comments. Their username is stored in a cookie when logged on, and when a post is submitted their username is stored alongside their post in a mySQL database. I'm trying to remove the delete button next to the comment if the user logged in is not the one that posted it. Here is the nonworking code:
<?php
$username = $_COOKIE['sqlusername'];
mysqlLogin();
$sql = mysql_query("SELECT * FROM `posts`");
$sqlCnt = mysql_num_rows($sql);
if($sqlCnt != 0) {
echo "<table align='center'><tr><td class='tdno'><u><H4>Message</H4></u></td><td class='tdno'><u><H4>Poster</H4></u></td><td class='tdno'><u><H4>Time</H4></u></td></tr>";
while($row = mysql_fetch_array($sql)) {
if($row['username'] != $username) {
echo "<script type=\"text/javascript\">";
echo "document.getElementById('delete').innerHTML = \"\";";
echo "</script>";
}
$id = $row['id'];
echo "<form action='delete.php' method='POST'>";
echo "<tr><td>";
echo $row['message'];
echo "</td><td>";
echo $row['poster'];
echo "</td><td align='center' width='10'>";
echo $row['date'];
echo "<td align='left' width='1'>";
echo "<input type='hidden' name='id' value='$id'>";
echo "<span id='delete'><input type='submit' class='submit' value='Delete'></span>";
echo "</td></tr>";
echo "</form>";
}
echo "</table>";
} else {
echo "<div align='center'>Sorry, no posts found!</div>";
}
?>
Any thoughts?
Try:
if ($username == $row['poster']) {
echo "<input type='hidden' name='id' value='$id'>";
echo "<span id='delete'><input type='submit' class='submit' value='Delete'></span>";
} else {
echo " "
}
maybe its the issue with your browser and javascript. Try this :
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById(id).style.display = 'none';
}
else {
if{ // IE 4
document.all.id.style.display = 'none';
}
}
else your logic looks fine(assuming you are storing the username and posts properly in database.)

Categories