Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Update one field with two buttons


[SOLVED] Update one field with two buttons

Reply
  #1 (permalink)  
Old 03-26-09, 09:20 AM
Smitty Smitty is offline
Newbie Coder
 
Join Date: Mar 2009
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Update one field with two buttons

Hello,

I have a page I wrote that displays applicant details for a site I maintain. however, I would like to add the ability to change the application status with the click of one of two buttons... Accepted or Denied.

In the end, this page will show all pending applicants only.

I know what the problem is, I just cannot find the fix to it. I need a way of getting the value of the record to be updated into my query.

Here's what I have...


Code:
<?php

require_once ('_include/conf.cfg');
require_once ('_include/header.tpl');

mysql_connect($db_host, $db_user, $db_passwd);
mysql_select_db($db_name);

$result = mysql_query("SELECT * FROM $db_name.applicants ORDER BY app_id DESC") or die(mysql_error());


while($row = mysql_fetch_array($result)){
?>
<center>
<form name="applicant" method="post" action="">
	<table width="45%" border="3" cellpadding="8" frame="box" rules="none">
		<tr>
			<td align="right" width="30%"><strong>Name:</strong></td>
			<td><?php echo $row['name'] ?></td>
			<td><?php echo $row['app_date'] ?></td>
		</tr>
		<tr>
			<td align="right" width="30%"><strong>Email:</strong></td>
			<td><?php echo $row['email'] ?></td>
		</tr>
		<tr>
			<td align="right" width="30%"><strong>Alias:</strong></td>
			<td><?php echo $row['alias'] ?></td>
		</tr>
		<tr>
			<td align="right" width="30%"><strong>Hear about us:</strong></td>
			<td><?php echo $row['referral'] ?></td>
		</tr>
		<tr>
			<td align="right" width="30%"><strong>Previous Clan:</strong></td>
			<td><?php echo $row['previous_clan'] ?></td>
		</tr>
		<tr>
			<td align="right" width="30%"><strong>Application Status:</strong></td>
			<td><?php echo $row['status'] ?></td>
		</tr>
		<tr>
			<td align="right" width="30%"><strong>Applicant IP:</strong></td>
			<td><?php echo $row['user_ip'] ?></td>
		</tr>
		<tr>
			<td colspan="3" align="center">
				<table width="100%">
					<tr>
						<td align="center" width="50%"><button name="decline" type="decline" value="<?php echo $row['app_id'] ?>"><img src="_include/images/cross.png" />Deny</button></td>
						<td align="center" width="50%"><button name="accept" type="accept" value="<?php echo $row['app_id'] ?>"><img src="_include/images/tick.png" />Accept</button></td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</form>					
</center>
<?php
$app_id = $row['app_id'];
}

if(isset($_POST['decline'])){
$decline_query="UPDATE `$db_name`.`applicants` SET `status` = 'Denied' WHERE `applicants`.`app_id` = $app_id LIMIT 1" or die("Cannot update record to denied");
mysql_query($decline_query) or die("Cannot deny");
echo $decline_query;
}

if(isset($_POST['accept'])){
$accept_query="UPDATE `$db_name`.`applicants` SET `status` = 'Accepted' WHERE `applicants`.`app_id` = $app_id LIMIT 1" or die("Cannot update record to denied");
mysql_query($accept_query) or die("Cannot accept");
echo $accept_query;
}
?>

The queries work when I hard code in an app_id, but when I try to assign the value, it only remembers the last app_id it looped through. How do I make it get the correct app id?

Here's the page almost working tr-clan.net/forms/applicants.php It is not currently excluding any particular status yet, not until I get this fixed.

Thanks in advance for any help!!
Smitty
Reply With Quote
  #2 (permalink)  
Old 03-26-09, 11:01 AM
Thyrosis's Avatar
Thyrosis Thyrosis is offline
Newbie Coder
 
Join Date: Dec 2008
Location: South UK
Posts: 66
Thanks: 2
Thanked 0 Times in 0 Posts
If I understand what you're saying, you have to put the 2 if statements that check the $_POST statusses inside the while loop. Otherwise you'll find that, like you said, it only remembers the last app_id.

PHP Code:

<?php
$app_id 
$row['app_id'];

if(isset(
$_POST['decline'])){
$decline_query="UPDATE `$db_name`.`applicants` SET `status` = 'Denied' WHERE `applicants`.`app_id` = $app_id LIMIT 1" or die("Cannot update record to denied");
mysql_query($decline_query) or die("Cannot deny");
echo 
$decline_query;
}

if(isset(
$_POST['accept'])){
$accept_query="UPDATE `$db_name`.`applicants` SET `status` = 'Accepted' WHERE `applicants`.`app_id` = $app_id LIMIT 1" or die("Cannot update record to denied");
mysql_query($accept_query) or die("Cannot accept");
echo 
$accept_query;
}
}
?>
Reply With Quote
  #3 (permalink)  
Old 03-26-09, 11:49 AM
Smitty Smitty is offline
Newbie Coder
 
Join Date: Mar 2009
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you for such a quick response.

I tried that just now, thinking oh how simple, how did I miss it?!!

But then when I ran the page, clicking on one deny or accept button executes the query for both records.

But that got me thinking. I need a way to differentiate between the forms. I needed a way to tell it to execute only one So I added the $app_id to the button name and the POST. It now works! Thank you for your help on this!


My new working code:
Code:
<?php

require_once ('_include/conf.cfg');
require_once ('_include/header.tpl');

mysql_connect($db_host, $db_user, $db_passwd);
mysql_select_db($db_name);

$result = mysql_query("SELECT * FROM $db_name.applicants ORDER BY app_id DESC") or die(mysql_error());


while($row = mysql_fetch_array($result)){
?>
<center>
<form name="applicant<?php echo $row['app_id'] ?>" method="post" action="">
	<table width="45%" border="3" cellpadding="8" frame="box" rules="none">
		<tr>
			<td align="right" width="30%"><strong>Name:</strong></td>
			<td><?php echo $row['name'] ?></td>
			<td><?php echo $row['app_date'] ?></td>
		</tr>
		<tr>
			<td align="right" width="30%"><strong>Email:</strong></td>
			<td><?php echo $row['email'] ?></td>
		</tr>
		<tr>
			<td align="right" width="30%"><strong>Alias:</strong></td>
			<td><?php echo $row['alias'] ?></td>
		</tr>
		<tr>
			<td align="right" width="30%"><strong>Hear about us:</strong></td>
			<td><?php echo $row['referral'] ?></td>
		</tr>
		<tr>
			<td align="right" width="30%"><strong>Previous Clan:</strong></td>
			<td><?php echo $row['previous_clan'] ?></td>
		</tr>
		<tr>
			<td align="right" width="30%"><strong>Application Status:</strong></td>
			<td><?php echo $row['status'] ?></td>
		</tr>
		<tr>
			<td align="right" width="30%"><strong>Applicant IP:</strong></td>
			<td><?php echo $row['user_ip'] ?></td>
		</tr>
		<tr>
			<td colspan="3" align="center">
				<table width="100%">
					<tr>
						<td align="center" width="50%"><button name="decline<?php echo $row['app_id'] ?>" type="submit" value="<?php echo $row['app_id'] ?>"><img src="_include/images/cross.png" />Deny</button></td>
						<td align="center" width="50%"><button name="accept<?php echo $row['app_id'] ?>" type="submit" value="<?php echo $row['app_id'] ?>"><img src="_include/images/tick.png" />Accept</button></td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</form>					
</center>
<?php
$app_id = $row['app_id'];

if(isset($_POST['decline'.$app_id])){
$decline_query="UPDATE `$db_name`.`applicants` SET `status` = 'Denied' WHERE `applicants`.`app_id` = $app_id LIMIT 1" or die("Cannot update record to denied");
mysql_query($decline_query) or die("Cannot deny");
echo $decline_query;
}

if(isset($_POST['accept'.$app_id])){
$accept_query="UPDATE `$db_name`.`applicants` SET `status` = 'Accepted' WHERE `applicants`.`app_id` = $app_id LIMIT 1" or die("Cannot update record to denied");
mysql_query($accept_query) or die("Cannot accept");
echo $accept_query;
}
}
?>


Now then, this leads to another question on this form (or should I create a new topic for a new question?). How do I get this sucker to reload once submitted?

I've tried
PHP Code:

<form action="<?php echo $PHP_SELF?>" method="post">
and

PHP Code:

<form action="thispage.php" method="post"

But to no avail. Ideas?
Reply With Quote
  #4 (permalink)  
Old 03-26-09, 12:28 PM
Thyrosis's Avatar
Thyrosis Thyrosis is offline
Newbie Coder
 
Join Date: Dec 2008
Location: South UK
Posts: 66
Thanks: 2
Thanked 0 Times in 0 Posts
The page refreshes as soon as you click one of the buttons, as you can see the query being displayed. I know what you mean though, the status doesn't update. What happens now is:

1. user visits page
2. user clicks deny button
3. page reloads, starts printing the form
4. at the bottom of the page, php encounters the check and deals accordingly (executes the query)
5. database is updated
6. what you need now, is indeed another refresh, or a different way of writing your form.

If you're rewriting, do it like this:

1. user visits page
2. user clicks deny button
3. page reloads, php encounters the check and deals accordingly (executes the update query)
4. SELECT query is executed
5. page building continues, starts printing the form
6. page is completely shown with new values

By the way, I don't know a way of refreshing, except for using the header("Location:...") function, but this would still need rewriting it (as it needs to be placed BEFORE any type of output (which does not exist in your scenario).

Last edited by Thyrosis; 03-26-09 at 12:30 PM.
Reply With Quote
  #5 (permalink)  
Old 03-26-09, 01:05 PM
Smitty Smitty is offline
Newbie Coder
 
Join Date: Mar 2009
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Ugh!! I was afraid of that

I think I may just add a refresh button that reloads the page It's for internal use only, so an extra button isn't that big of a deal for this purpose. Since I am donating my time on this, I don't really want to rewrite this yet again.

Thank you again for your help!!!

Smitty
Reply With Quote
  #6 (permalink)  
Old 03-26-09, 05:40 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
This will show your correct App's status, since you are making changes before populating your form.


PHP Code:



<?php

require_once ('_include/conf.cfg');
require_once (
'_include/header.tpl');

mysql_connect($db_host$db_user$db_passwd);
mysql_select_db($db_name);

//----------->New Variable passed via form!
$id $_POST['id'];

//----------->Moved from bottom of page!
if(isset($_POST['decline'.$app_id])){
$decline_query="UPDATE `$db_name`.`applicants` SET `status` = 'Denied' WHERE `applicants`.`app_id` = $id LIMIT 1" or die("Cannot update record to denied");
mysql_query($decline_query) or die("Cannot deny");
echo 
$decline_query;
}

//----------->Moved from bottom of page!
if(isset($_POST['accept'.$app_id])){
$accept_query="UPDATE `$db_name`.`applicants` SET `status` = 'Accepted' WHERE `applicants`.`app_id` = $id LIMIT 1" or die("Cannot update record to denied");
mysql_query($accept_query) or die("Cannot accept");
echo 
$accept_query;
}

$result mysql_query("SELECT * FROM $db_name.applicants ORDER BY app_id DESC") or die(mysql_error());


while(
$row mysql_fetch_array($result)){
?>
<center>
<form name="applicant<?php echo $row['app_id'?>" method="post" action="">

<!--Inserted New form element here -->
<input type="hidden" name="id" value="<?php echo $row['app_id']; ?>"/>


    <table width="45%" border="3" cellpadding="8" frame="box" rules="none">
        <tr>
            <td align="right" width="30%"><strong>Name:</strong></td>
            <td><?php echo $row['name'?></td>
            <td><?php echo $row['app_date'?></td>
        </tr>
        <tr>
            <td align="right" width="30%"><strong>Email:</strong></td>
            <td><?php echo $row['email'?></td>
        </tr>
        <tr>
            <td align="right" width="30%"><strong>Alias:</strong></td>
            <td><?php echo $row['alias'?></td>
        </tr>
        <tr>
            <td align="right" width="30%"><strong>Hear about us:</strong></td>
            <td><?php echo $row['referral'?></td>
        </tr>
        <tr>
            <td align="right" width="30%"><strong>Previous Clan:</strong></td>
            <td><?php echo $row['previous_clan'?></td>
        </tr>
        <tr>
            <td align="right" width="30%"><strong>Application Status:</strong></td>
            <td><?php echo $row['status'?></td>
        </tr>
        <tr>
            <td align="right" width="30%"><strong>Applicant IP:</strong></td>
            <td><?php echo $row['user_ip'?></td>
        </tr>
        <tr>
            <td colspan="3" align="center">
                <table width="100%">
                    <tr>
                        <td align="center" width="50%"><button name="decline<?php echo $row['app_id'?>" type="submit" value="<?php echo $row['app_id'?>"><img src="_include/images/cross.png" />Deny</button></td>
                        <td align="center" width="50%"><button name="accept<?php echo $row['app_id'?>" type="submit" value="<?php echo $row['app_id'?>"><img src="_include/images/tick.png" />Accept</button></td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</form>                    
</center>
<?php
}
?>
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Update multiple rows outside loop - need help ElvansX PHP 1 12-03-06 01:55 AM
Update total price field when choosing an item djnaf JavaScript 1 10-14-06 03:11 PM
Can't UPDATE MySQL field, not sure why... GodsHand PHP 3 04-10-06 04:15 AM
Disable form fields to be submitted RickyRod JavaScript 2 05-24-04 10:15 AM
PHP to MySQL script question...(using a field to update info in MySQL) DisneyFan25863 PHP 4 11-02-03 03:31 AM


All times are GMT -5. The time now is 08:31 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.