Jump to content
Sign in to follow this  
techkid

Multiple PHP actions on one form

Recommended Posts

Hi there,

 

I am writing a website for a friend. The goal is to allow people to write and submit poetry to help with depression and anxiety, provide messages of hope and things like that. I have created most of the website, but one section has got me stumped.

 

What I want to implement is an approval process, where submitted poetry is segregated until reviewed and approved, given the proliferation of online trolls around such things. What I want to achieve is to have a form where a piece is reviewed with radio button selections for "approve" or "delete", and this is where I have stalled.

 

This is what I have created:

 

review.php

<!DOCTYPE html>
<html>
	<head>
	<title>Soul's Fire - Review Submissions</title>
	</head>
	<body background="Images/background.png">
		<center><img src="Images/heading.png"></center>
		<h1 align="center"><font color="#cc9900">Soul's Fire - Review Submissions</font></h1>
		<p align="center">Words go here</p>
		<?php
			error_reporting(E_ALL & ~(E_WARNING));
			$dbconnect=mysqli_connect("127.0.0.1","user","password","poems");
			$result=mysqli_query($dbconnect, "SELECT * FROM submissions WHERE Approved='N' ORDER BY Time_Submit LIMIT 1");
			if ($row=mysqli_fetch_array($result))
			{
			echo "<form action='adddel.php' method='post'>\n";
			echo "<div align='center'>\n";
			echo "<table border=1 width='80%'>\n";
			echo "<caption align='bottom'><p><input type='submit' name='AppDel' value='Submit Approval'></caption>\n";
			echo "<tr><th width='10%'>Author</th><th width='15%'>Title</th><th width='15%'>Time Submitted</th><th width='*'>Entry</th></tr>\n";
			do
				{
				printf("<tr><td align='center'>%s</td><td align='center'><b>%s</b></td><td align='center'>%s</td><td>%s</td></tr>\n",
				$row["Author"], $row["Title"], $row["Time_submit"], $row["Entry"]);
				}
			while ($row=mysqli_fetch_row($result));
			echo "<tr><td colspan='4' align='center'><INPUT TYPE='radio' NAME='ConApprove' VALUE='Approve'>Approve Submission
			<br><INPUT TYPE='radio' NAME='ConApprove' VALUE='Delete'>Delete Submission</td></tr>\n";
			echo "</table></div><br></form>\n";
			echo "<p align='center'><a href='#top'>Back to Top</a></p>";
			}
	   	else
			{
			echo "No entries to review.";
			}
		?>
		<div align="center">
		<A HREF="index.php">Return to the main page</A>
		</div>
		<h1 align="center"><font color="#cc9900">If you would like me to add content down here, let me know</font></h1>
    </body>
</html>

adddel.php

<?php
	$error="";
	$go=TRUE;

	$dbconnect=mysqli_connect("127.0.0.1", "user", "password", "poems");
	if(!$dbconnect)
		{
		die('Unable to connect to database.');
		}

	$AppDel=$_POST["ConApprove"];

	if($AppDel=="Approve")
		{
		$message .="This will approve the submission. Continue?\\n";
		$ApproveSet=("UPDATE submissions SET Approved='Y' WHERE ID=$row['ID']");
		}
	elseif($AppDel=="Delete")
		{
		$message .="This will delete the submission. Continue?\\n";
		$ApproveSet=("DELETE FROM submissions WHERE ID=$row['ID']");
		}
	else
		{
		$error .="Please select before continuing\\n";
		$go=FALSE;
		}

	if($go==TRUE)
		{
		echo ("<script language='JavaScript'>message('$message');</script>");
		mysqli_query($dbconnect, $ApproveSet) or die(mysqli_error());
		echo "<A HREF='review.php'>Return to review page</A>\n";
		}
	else
		{
		echo ("<script language='JavaScript'>alert('$error');</script>");
		echo ("<script language='JavaScript'>javascript:history.go(-1)</script>");
		}
?>

But when I select any option I only receive an error message (see attachment).

 

I am only just recently getting back into website design after about 15 years, so my knowledge is pretty archaic and rudimentary. I have created basic forms for this website, which work with no problem, but this one is probably too ambitious for my limited knowledge.

 

Thank you in advance.

 

post-44628-0-77195900-1504743467_thumb.png

Share this post


Link to post
Share on other sites

Ahoy! I'm pretty sure the error as stated is in line 16, which by counting shows;

$ApproveSet=("UPDATE submissions SET Approved='Y' WHERE ID=$row['ID']");

I think the problem is the extra brackets, try ...

$ApproveSet = "UPDATE submissions SET Approved = 'Y' WHERE ID = " . $row['ID'];

... then it'll become for example ...

mysqli_query($dbconnect, "UPDATE submissions SET Approved = 'Y' WHERE ID = 44")

etc

Share this post


Link to post
Share on other sites

Aha hang on $row['ID'] is not defined in adddel.php. You need to send the id of the row along with the form, add to the form something like ...

<input type="hidden" name="whichRow" value="<?php echo $row['id']; ?>">

then in adddel.php ...

$whichRow = intval($_POST['whichRow']);
Edited by BrowserBugs

Share this post


Link to post
Share on other sites

This is the revised code:

 

review.php

<!DOCTYPE html>
<html>
	<head>
	<title>Soul's Fire - Review Submissions</title>
	</head>
	<body background="Images/background.png">
		<center><img src="Images/heading.png"></center>
		<h1 align="center"><font color="#cc9900">Soul's Fire - Review Submissions</font></h1>
		<p align="center">Words go here</p>
		<?php
			error_reporting(E_ALL & ~(E_WARNING));
			$dbconnect=mysqli_connect("127.0.0.1","user","password","poems");
			$result=mysqli_query($dbconnect, "SELECT * FROM submissions WHERE Approved='N' ORDER BY Time_Submit LIMIT 1");
			if ($row=mysqli_fetch_array($result))
			{
			echo "<form action='adddel.php' method='post'>\n";
			echo "<div align='center'>\n";
			echo "<table border=1 width='80%'>\n";
			echo "<caption align='bottom'><p><input type='submit' name='AppDel' value='Submit Approval'></caption>\n";
			echo "<tr><th width='10%'>Author</th><th width='15%'>Title</th><th width='15%'>Time Submitted</th><th width='*'>Entry</th></tr>\n";
			do
				{
				printf("<tr><td align='center'>%s</td><td align='center'><b>%s</b></td><td align='center'>%s</td><td>%s</td></tr>\n",
				$row["Author"], $row["Title"], $row["Time_submit"], $row["Entry"]);
				}
			while ($row=mysqli_fetch_row($result));
			echo "<tr><td colspan='4' align='center'><INPUT TYPE='radio' NAME='ConApprove' VALUE='Approve'>Approve Submission
			<br><INPUT TYPE='radio' NAME='ConApprove' VALUE='Delete'>Delete Submission</td></tr>\n";
(*)			echo '<input type="hidden" name="whichRow" value="<?php echo $row["ID"]; ?>"';
			echo "</table></div></form>\n";
			echo "<p align='center'><a href='#top'>Back to Top</a></p>";
			}
	   	else
			{
			echo "<p align='center'>No entries to review.</p>";
			}
		?>
		<div align="center">
		<A HREF="index.php">Return to the main page</A>
		</div>
		<h1 align="center"><font color="#cc9900">If you would like me to add content down here, let me know</font></h1>
    </body>
</html>

adddel.php

<?php
	$error="";
	$go=TRUE;

	$dbconnect=mysqli_connect("127.0.0.1", "user", "password", "poems");
	if(!$dbconnect)
		{
		die('Unable to connect to database.');
		}

	$AppDel=$_POST["ConApprove"];
(*)	$whichRow = intval($_POST['whichRow']);

	if($AppDel=="Approve")
		{
		$message .="This will approve the submission. Continue?\\n";
		$ApproveSet="UPDATE submissions SET Approved='Y' WHERE ID=$row['ID']";
		}
	elseif($AppDel=="Delete")
		{
		$message .="This will delete the submission. Continue?\\n";
		$ApproveSet="DELETE FROM submissions WHERE ID=$row['ID']";
		}
	else
		{
		$error .="Please select before continuing\\n";
		$go=FALSE;
		}

	if($go==TRUE)
		{
		echo ("<script language='JavaScript'>message('$message');</script>");
		mysqli_query($dbconnect, $ApproveSet) or die(mysqli_error());
		echo "<A HREF='review.php'>Return to review page</A>\n";
		}
	else
		{
		echo ("<script language='JavaScript'>alert('$error');</script>");
		echo ("<script language='JavaScript'>javascript:history.go(-1)</script>");
		}
?>

Still I encounter the same problem. I am reasonably sure I have the syntax (or possibly placement) wrong for the highlighted lines (I added an asterisk (*) at the start of the line to highlight my placement only).

Edited by techkid

Share this post


Link to post
Share on other sites

You still have the following ...

$ApproveSet="UPDATE submissions SET Approved='Y' WHERE ID=$row['ID']";

This should now use the posted row id which is $whichRow.

Share this post


Link to post
Share on other sites

I seem to be going from bad to worse. I realised a mistake in my entry on review.php, but my attempts to correct it causes it to fail.

Here is my revised code (review.php):

<!DOCTYPE html>
<html>
	<head>
	<title>Soul's Fire - Review Submissions</title>
	</head>
	<body background="Images/background.png">
		<center><img src="Images/heading.png"></center>
		<h1 align="center"><font color="#cc9900">Soul's Fire - Review Submissions</font></h1>
		<p align="center">Words go here</p>
		<?php
			error_reporting(E_ALL & ~(E_WARNING));
			$dbconnect=mysqli_connect("127.0.0.1","David","D15cw0rld","poems");
			$result=mysqli_query($dbconnect, "SELECT * FROM submissions WHERE Approved='N' ORDER BY Time_Submit LIMIT 1");
			if ($row=mysqli_fetch_array($result))
			{
			echo "<form action='adddel.php' method='post'>\n";
			echo "<div align='center'>\n";
			echo "<table border=1 width='80%'>\n";
			echo "<caption align='bottom'><p><input type='submit' name='AppDel' value='Submit Approval'></caption>\n";
			echo "<tr><th width='10%'>Author</th><th width='15%'>Title</th><th width='15%'>Time Submitted</th><th width='*'>Entry</th></tr>\n";
			do
				{
				printf("<tr><td align='center'>%s</td><td align='center'><b>%s</b></td><td align='center'>%s</td><td>%s</td></tr>\n",
				$row["Author"], $row["Title"], $row["Time_submit"], $row["Entry"]);
				}
			while ($row=mysqli_fetch_row($result));
			echo "<tr><td colspan='4' align='center'><INPUT TYPE='radio' NAME='ConApprove' VALUE='Approve'>Approve Submission
			<br><INPUT TYPE='radio' NAME='ConApprove' VALUE='Delete'>Delete Submission
(*)			<br><INPUT TYPE='hidden' NAME='whichRow' VALUE='<?php echo $row["ID"]; ?>'></td></tr>\n";
			echo "</table></div></form>\n";
			echo "<p align='center'><a href='#top'>Back to Top</a></p>";
			}
	   	else
			{
			echo "<p align='center'>No entries to review.</p>";
			}
		?>
		<div align="center">
		<A HREF="index.php">Return to the main page</A>
		</div>
		<h1 align="center"><font color="#cc9900">If you would like me to add content down here, let me know</font></h1>
    </body>
</html>

Which generates the attached error at the marked location.

post-44628-0-73212600-1504788732_thumb.png

Share this post


Link to post
Share on other sites

I'm sure adding my form tags inside the PHP section of the script is a massive no-no, given how things are going for me here. And that, because of that, the added line you supplied is failing because of my poor use of quotation marks...

Share this post


Link to post
Share on other sites

Ok I think I need to rewind slightly. If you have one form with multiple entries and each entry has 2 options to approve and delete then the following should output your array. I've split out the html and php for easy reading and I'm using a simple list for now. The exception is instead of radio i'm using 2 buttons for each, one for delete and one for approve. The buttons value is set to the id of the record.

<form action="adddel.php" method="post">
<?php while($row = mysqli_fetch_row($result)) { ?>
<ul>
<li><?php echo $row['Title']; ?></li>
<li><?php echo $row['Author']; ?></li>
<li><?php echo $row['Entry']; ?></li>
<li><?php echo $row['ID']; ?></li>
<li><button name="del" type="submit" value="<?php echo $row['ID']; ?>">Delete</button></li>
<li><button name="app" type="submit" value="<?php echo $row['ID']; ?>">Approve</button></li>
</ul>
<?php } ?>
</form>

Now if you collect the $_POST value of which submit button was used you can determine which record and what to do with it. Below checks if a button name was pressed and the value is which id you want to do something to (assuming the ID is a number).

<?php if($_SERVER['REQUEST_METHOD']=="POST" && intval($_POST['del'])>=1) {
    $del_id = intval($_POST['del']);
} elseif($_SERVER['REQUEST_METHOD']=="POST" && intval($_POST['app'])>=1) {
    $approved_id = intval($_POST['app']);
} else {
    // Neither app was pressed or del was pressed so something is fishy.
} ?>
Edited by BrowserBugs

Share this post


Link to post
Share on other sites

I actually ended up scrapping the pages and rebuilding from scratch, trying to split the HTML from the PHP, and have made a great deal of progress. While the end result is not exactly what I want, the proof of concept is done!

 

Here is my final code:

 

review.php

<!DOCTYPE html>

<html>
<head>
<title>Soul's Fire - Visitor Contributions</title>
</head>
<body background="Images/background.png">
<center><img src="Images/heading.png"></center>
<h1 align="center"><font color="#cc9900">Soul's Fire - Visitor Contributions</font></h1>
<p align="center">Words go here</p>

<?php
error_reporting(E_ALL & ~(E_WARNING));
$dbconnect=mysqli_connect("127.0.0.1", "David", "D15cw0rld", "poems");
$result=mysqli_query($dbconnect, "SELECT * FROM submissions WHERE Approved='N' ORDER BY Time_submit LIMIT 1");
$row=mysqli_fetch_array($result);
?>

<form action="adddel.php" method="post">
<p align="center">You are reviewing entry: <input type="text" name="HEntry" value="<?php echo $row['ID']; ?>">
<table width="80%" border="1" align="center">
<caption align="bottom"><p><input type="submit" name="AppDel" value="Submit Approval"></caption>
<tr>
<th width="10%">Author</th><th width="15%">Title</th><th width="15%">Time Submitted</th><th width="*">Entry</th>
</tr>

<?php
do
{
printf("<tr><td align='center'>%s</td><td align='center'><b>%s</b></td><td align='center'>%s</td><td>%s</td></tr>\n",
$row["Author"], $row["Title"], $row["Time_submit"], $row["Entry"]);
}
while ($row=mysqli_fetch_row($result));
?>

<tr>
<td colspan='4' align='center'><INPUT TYPE='radio' NAME='ConApprove' VALUE='Approve'>Approve Submission
<br><INPUT TYPE='radio' NAME='ConApprove' VALUE='Delete'>Delete Submission</td></tr>
</table>
</form>
<p align="center"><A HREF="index.php">Return to the home page</A>
</body>
</html>

adddel.php

<?php
$error="";
$go=TRUE;
$message="";

$dbconnect=mysqli_connect("127.0.0.1", "user", "password", "poems");
if(!$dbconnect)
{
die('Unable to connect to database.');
}

$AppDel=$_POST["ConApprove"];
$ID=$_POST["HEntry"]

if($AppDel=="Approve")
{
$message .="This will approve the submission. Continue?\\n";
$ApproveSet="UPDATE submissions SET Approved='Y' WHERE ID=$ID";
}
elseif($AppDel=="Delete")
{
$message .="This will delete the submission. Continue?\\n";
$ApproveSet="DELETE FROM submissions WHERE ID=$ID";
}
else
{
$error .="Please select before continuing\\n";
$go=FALSE;
}

if($go==TRUE)
{
(*)echo ("<script language='JavaScript'>confirm('$message');</script>");
mysqli_query($dbconnect, $ApproveSet) or die(mysqli_error());
echo "<A HREF='review.php'>Return to review page</A>\n";
}
else
{
echo ("<script language='JavaScript'>alert('$error');</script>");
echo ("<script language='JavaScript'>javascript:history.go(-1)</script>");
}
?>

The only niggling detail is that the confirm box, marked with an asterisk (*), proceeds whether you press "OK" or "Cancel", but for right now, I can live with that!

 

Thank you very much for your help with this!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Lyndsey
      Hi guys,
       
      I thought it would be cool to start a topic for people to share sites they find useful for their design/development processes. Here is a list of sites I find useful and/or use regularly:
       
      General Stuff:
      http://jsfiddle.net/ - Useful for JavaScript troubleshooting. Test your code and just play around with HTML/CSS and JavaScript. If you're looking for help with a piece of code, save your Fiddle and send it to someone else who can take a look at it You can also save revisions of your Fiddle to track your progress. http://pastebin.com/ - Useful for sharing code online. Useful for when you run in to a bit of trouble with some server-side code. Instead of pasting masses of lines inside a topic, paste your code in to pastebin and get someone to have a look at it for you http://jsonlint.com/ - JSON Validator http://paletton.com/ - Color Scheme Designer. See what colours complement each other etc. Useful stuff. http://www.lipsum.com/ - Lorem Ipsum - Useful for inserting dummy text in to your web pages. http://net.tutsplus.com/category/tutorials/php/ - PHP tutorials, quizzes etc. They also do the same for JavaScript, HTML, CSS, WordPress, ASP.NET to name a few. Really good site.  
      CSS:
      http://css3generator.com/ - Generate CSS3. Box Shadow, Border Radius etc. http://border-radius.com/ - Generate border radius quickly! http://www.javascripter.net/faq/hextorgb.htm - HEX to RGB conversion. http://meyerweb.com/eric/tools/css/reset/ - CSS Reset Stylesheet. Resolve browser inconsistencies such as varying margins/padding. http://www.cssplay.co.uk - Lots of experiments with CSS, menus and layouts.  
      Responsive Design:
      http://www.getskeleton.com/ - Responsive Grid.  
      Server-Side Coding:
      http://net.tutsplus.com/tutorials/php/php-database-access-are-you-doing-it-correctly/ - How to connect to a database using PDO http://www.php.net/manual/en/index.php - PHP manual for everything PHP!  
      Form Validation (Client-Side):
      https://jqueryvalidation.org/ - (My preferred method) Simple to use. Include the necessary jQuery and Validation.js files. Simple usage example, call the function using $('#myForm').validate(); and add a class="required" to fields you want to validate Also allows you to create custom messages and make the validation more advanced! I hope these are useful! If anyone has any others to share, please do
       
      Lyndsey.
    • By Drummer
      Hi guys
       
      I've created a website which i would like to "wrap" in a members-only environment. I don't want to re-invest the wheel, but looking for a register page, login page, password reset, and all the links/instructions to hook it up to the remote server the website is currently on.
       
      Does anyone have any pointers for this type of thing? I've tried googling, but struggling tbh!
    • By rainland
      Hello all,
       
      I'm planning on making a website, which will have a calculating web form in it.
      The form will have to do various basic mathematical tasks like multiplication and division for example.
      Also a very big plus for the form would be the possibility to save and load it (I'm thinking like either a database where you could save using a specific string and load by it or save as a xml file to the local computer or something).
       
      Now the qustion is what would be the best way of implementing this kind of site/tool?
       
      I know html5 and css and am fairly adept in C but everything else (javascript, php etc.) is unknown to me. Now, learning a new language will not be a problem because I will have lots of time to develop.
       
      I was thinking WordPress because of its ease of use and ive found this plugin but I am unsure of whether it will be able to have the save/load function.
       
      What do you guys think? How would you do this? Thanks in advance for your thoughts and feel free to ask any additional questions if I've missed something.
       
      regards,
      rainland
    • By 5nake
      Still working on the site
       

       
      My name is Elvis and I self taught how to create Web Sites. I learned how to programmate in HTML, CSS, JS, PHP, MYSQL, and it’s not so hard as it sound. I created ECODEWS- Elvis Code Web Sites to share with you my knowledge in web design and development. In my blog you can find a lot of useful information, tutorials, inspiration and news.
       
      http://www.ecodews.com
  • Recently Browsing

    No registered users viewing this page.

  • Member Statistics

    • Total Members
      57,535
    • Most Online
      4,970

    Newest Member
    Rose Hale
    Joined
  • Forum Statistics

    • Total Topics
      65,732
    • Total Posts
      455,406
×