Jump to content

conditional submit


mediasix

Recommended Posts

hi guys,

i have a page that i am developing which has a few forms on it (or plans to)

i am at the stage of adding the second form, i have a conditional 'submit' on the processing page.

but when i submit, both of the conditionals process the form attributes.

i may not be explaining very well so here is the code:

 

<html>
<head>
<title>database test</title>
</head>
<body>
<h2>currnt entries</h2>

<form action="addentry.php" method="get">
Title:
<input type="text" name="title">
Content:
<input type="text" name="content">
<input type="submit" name="add_entry" value="DO IT!">
</form>

<form action="addentry.php" method="get">
Title:
<input type="text" name="title">
Content:
<input type="text" name="content">
<input type="submit" name="add_entry_two" value="DO IT! 2">
</form>
</body>
</html>

 

<?php include("includes/mysqlconnect.php") ?>

<?php
if($_REQUEST['submit']!='add_entry')
{  
	$title=$_REQUEST['title'];
	$content=$_REQUEST['content'];
	$query ="INSERT INTO test (title,content)";
	$query.=" VALUES ('$title','$content')";
	$result=mysql_query($query);
	if ($result) echo "<b>Successfully Posted!</b>";
	else echo "<b>ERROR: unable to post.</b>";
}
if($_REQUEST['submit']!='add_entry_two')
{  
	$title=$_REQUEST['title'];
	$content=$_REQUEST['content'];
	$query ="INSERT INTO test_two (title,content)";
	$query.=" VALUES ('$title','$content')";
	$result=mysql_query($query);
	if ($result) echo "<b>Successfully Posted! TWO</b>";
	else echo "<b>ERROR: unable to post.</b>";
}
else
{	
	echo "NOT WORKING BUD";
}
?>

 

thanks in anticipation

mediasix

Link to comment
Share on other sites

Well, you're doing it wrong. You want to request 'add_entry' and 'add_entry_two', since those are the names of the submit fields. Or you could change the names to 'submit', and have different values to check for. If you stick with the former, simply do this:

 

<?php
if($_REQUEST['add_entry']) {
// bla bla
}
?>

 

If you want to check the values of the submit buttons (if that's what you tried):

 

<?php
if($_REQUEST['add_entry'] == 'DO IT!') {
// bla bla
}
?>

 

...

 

else

{

echo "NOT WORKING BUD";

}

 

haha ;D

Link to comment
Share on other sites

OK so i've got the submission sorted, but i am faced with a new problem.

i also have 2 acctions, and the first one works but the second one does nothing.

code below

thanks

mediasix

 

<?php include("includes/mysqlconnect.php") ?>

<?php
if($_REQUEST['submit']=='add_entry')
{  
	$title=$_REQUEST['title'];
	$content=$_REQUEST['content'];
	$query ="INSERT INTO test (title,content)";
	$query.=" VALUES ('$title','$content')";
	$result=mysql_query($query);
}
elseif($_REQUEST['submit']=='add_entry_two')
{  
	$title=$_REQUEST['title'];
	$content=$_REQUEST['content'];
	$query ="INSERT INTO test_two (title,content)";
	$query.=" VALUES ('$title','$content')";
	$result=mysql_query($query);
}
elseif($_REQUEST['action']="del")
{
	mysql_query("DELETE FROM test WHERE id={$_REQUEST['id']};");
}
elseif($_REQUEST['action']="del_two")
{
	mysql_query("DELETE FROM test_two WHERE id={$_REQUEST['id']};");
}	
else
{	
	echo "NOT WORKING BUD";
}
?>
<html>
<head>
<title>database test</title>
<script type="text/javascript" src="scripts/prototype-1.6.0.2.js"></script>
</head>
<body>
<div id="wrapper">
<!--ADD ENTRY FORMS-->
<h2>ADD TEST ENTRY</h2>

<form id="add_entry" action="index.php" method="get">
Title:
<input type="text" name="title">
Content:
<input type="text" name="content">
<input type="submit" name="submit" value="add_entry">
</form>

<form id="add_entry_two" action="index.php" method="get">
Title:
<input type="text" name="title">
Content:
<input type="text" name="content">
<input type="submit" name="submit" value="add_entry_two">
</form>
<!-- END OF ADD ENTRY FORMS-->

<!--CURRNET ENTRIES-->
<table border="0" cellpadding="0" cellspacing="0">
<?php
$result=mysql_query("SELECT id,title,content FROM test ORDER BY id;");


$i=0;
while( $row=mysql_fetch_array($result) )
{
	echo "<tr valign=center>";
	echo "<td class=tabval><b>".$row['id']."</b></td>";
	echo "<td class=tabval>".$row['title']." </td>";
	echo "<td class=tabval>".$row['content']." </td>";

	echo "<td class=tabval><a style=\"color:red;\" href=index.php?action=del&id=".$row['id'].">[Delete?]</a></td>";
	echo "<td class=tabval></td>";
	echo "</tr>";
	$i++;
}
?>
</table>
<!--END OFCURRNET ENTRIES-->

<!--CURRNET ENTRIES TWO-->
<table border="0" cellpadding="0" cellspacing="0">
<?php
$result=mysql_query("SELECT id,title,content FROM test_two ORDER BY id;");


$i=0;
while( $row=mysql_fetch_array($result) )
{
	echo "<tr valign=center>";
	echo "<td class=tabval><b>".$row['id']."</b></td>";
	echo "<td class=tabval>".$row['title']." </td>";
	echo "<td class=tabval>".$row['content']." </td>";

	echo "<td class=tabval><a style=\"color:red;\" href=index.php?action=del_two&id=".$row['id'].">[Delete?]</a></td>";
	echo "<td class=tabval></td>";
	echo "</tr>";
	$i++;
}
?>
</table>
<!--END OFCURRNET ENTRIES TWO-->
</div><!--END OF WRAPPER-->
</body>
</html>

Link to comment
Share on other sites

a lot of debugging later, i fixed it.

just took out the elseif.

code below.

mediasix

 

<?php include("includes/mysqlconnect.php") ?>

<?php
if($_REQUEST['submit']=='add_entry')
{  
	$title=$_REQUEST['title'];
	$content=$_REQUEST['content'];
	$query ="INSERT INTO test (title,content)";
	$query.=" VALUES ('$title','$content')";
	$result=mysql_query($query);
}
if($_REQUEST['submit']=='add_entry_two')
{  
	$title=$_REQUEST['title'];
	$content=$_REQUEST['content'];
	$query ="INSERT INTO test_two (title,content)";
	$query.=" VALUES ('$title','$content')";
	$result=mysql_query($query);
}
if($_REQUEST['action']="del_two")
{
	mysql_query("DELETE FROM `test_two` WHERE `test_two`.`id`={$_REQUEST['id']};");
}
if($_REQUEST['action']="del")
{
	mysql_query("DELETE FROM test WHERE id={$_REQUEST['id']};");
}
else
{	
	echo "NOT WORKING BUD";
}
?>

<html>
<head>
<title>database test</title>
</head>
<body>
<div id="wrapper">
<!--ADD ENTRY FORMS-->
<h2>ADD TEST ENTRY</h2>

<form id="add_entry" action="index.php" method="get">
Title:
<input type="text" name="title">
Content:
<input type="text" name="content">
<input type="submit" name="submit" value="add_entry">
</form>

<form id="add_entry_two" action="index.php" method="get">
Title:
<input type="text" name="title">
Content:
<input type="text" name="content">
<input type="submit" name="submit" value="add_entry_two">
</form>
<!-- END OF ADD ENTRY FORMS-->

<!--CURRNET ENTRIES-->
<h2>TABLE test</h2>
<table border="0" cellpadding="0" cellspacing="0">
<?php
$result=mysql_query("SELECT id,title,content FROM test ORDER BY id;");


$i=0;
while( $row=mysql_fetch_array($result) )
{
	echo "<tr valign=center>";
	echo "<td><b>".$row['id']."</b></td>";
	echo "<td>".$row['title']." </td>";
	echo "<td>".$row['content']." </td>";

	echo "<td><a style=\"color:red;\" href=index.php?action=del&id=".$row['id'].">[Delete?]</a></td>";
	echo "<td></td>";
	echo "</tr>";
	$i++;
}
?>
</table>
<!--END OFCURRNET ENTRIES-->

<!--CURRNET ENTRIES TWO-->
<h2>TABLE test_two</h2>
<table border="0" cellpadding="0" cellspacing="0">
<?php
$result=mysql_query("SELECT id,title,content FROM test_two ORDER BY id;");


$i=0;
while( $row=mysql_fetch_array($result) )
{
	echo "<tr valign=center>";
	echo "<td><b>".$row['id']."</b></td>";
	echo "<td>".$row['title']." </td>";
	echo "<td>".$row['content']." </td>";

	echo "<td><a style=\"color:red;\" href=index.php?action=del_two&id=".$row['id'].">[Delete?]</a></td>";
	echo "<td></td>";
	echo "</tr>";
	$i++;
}
?>
</table>
<!--END OFCURRNET ENTRIES TWO-->
</div><!--END OF WRAPPER-->
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.