Jump to content

[SOLVED] Form in a function


Andy17

Recommended Posts

Hey guys!

 

I'm currently working on a validation script for the submitted content on my website. However, I have a problem when I wish to edit some submitted content; I have some code that I want to run only when a button is pressed and not before. This has not been a problem for me before, so I'm not entirely sure what is wrong, even though I believe it's a problem cause by mixing HTML and server-side code somehow (didn't learn too much about this yet, sorry). I just realized how this cannot be done this way, but please look at my code and see if you have any suggestions on how to do what I am looking to do.

 

<?php

if (isset($_POST['edit'])) // Just calling the function when clicking a button

{

edit_joke($number);

}


function edit_joke($number)

{


		if (isset($_POST['editbutton'])); // I want the code starting here to be run when the "editbutton" is pressed, but it
                                                          // runs no matter what. I see the problem, but not sure how to actually do this. This, together
                                                          // with my form, is the problem.

		{

                        // Getting the new info from the formI realize I could've done the below a little smarter, but that isn't my biggest problem right now 
		$newtitle = $_POST['joketitle'];
		$newjoke = $_POST['joketext'];
		$newcategory = $_POST['category'];

                        // The new info is moved to another table (this is not a problem, it works)
		$sql2 = "INSERT INTO jokes(submitter, title, joke, category, date)VALUES('$submitter', '$newtitle', '$newjoke', '$newcategory', '$date')";
		$result2 = mysql_query($sql2);


		if($result2)

		{

                        // The old info is deleted from the temporary table (this is not a problem, it works)
		$sql3 = "DELETE FROM temp_jokes WHERE title = '$title' AND submitter = '$submitter'";
		$result3 = mysql_query($sql3);

		}

		}


$number2 = $number-1; // Just because...

// Getting the information I need to edit from the table
$sql1 = "SELECT * FROM temp_jokes LIMIT {$number2}, 1";
$result = mysql_query($sql1);

if ($result)

{

		$row = mysql_fetch_array($result);

                        // Just making life easier
		$submitter = $row['submitter'];
		$title = $row['title'];
		$joke = $row['joke'];
		$category = $row['category'];
		$date = $row['date'];


		?>

		<form name="edit" method="post">

		<input type="text" name="joketitle" size="24" value="<?php echo $title; ?>">

		<br><br>

		<textarea name="joketext" maxlength="1500" style="width: 900px; height: 400px;"><?php echo $joke; ?></textarea>

		<br><br>

		<?php echo "<b>Category:</b> " . $category; ?><br>

		<select name="category">

		<OPTION SELECTED VALUE="-">Choose one</OPTION>
		<OPTION>Adult</OPTION>
		<OPTION>Alcohol</OPTION>
		<OPTION>Bar</OPTION>
		<OPTION>Blonde</OPTION>
		<OPTION>Brunette</OPTION>
		<OPTION>Classic</OPTION>
		<OPTION>Men</OPTION>
		<OPTION>Sports</OPTION>
		<OPTION>Women</OPTION></select>

		<br><br>

		<input type="submit" name="editbutton" value="Submit">

		</form>

<?php

}

}

?>

 

Basically when running the function, the fields from my form should appear and I should be able to edit the info - and when I press the "editbutton" button, it should run the code within the isset($_POST['editbutton']) brackets. Is this impossible to do in a function? Or any suggestions?

 

Sorry if my code looks very foolish to you - it does to me too, now. :)

Link to comment
https://forums.phpfreaks.com/topic/126808-solved-form-in-a-function/
Share on other sites

Sorry if my code looks very foolish to you - it does to me too, now. Smiley

 

Well, then may I suggest you tidy up you code...removing all the enters for example and post it back ? this is a bit hard to read...

 

And where is the edit button ?

if (isset($_POST['edit'])) // Just calling the function when clicking a button

 

Also, where is $date and $submitter?

 

Ow, and please escape your variables before inserting them into a db...

http://nl3.php.net/manual/en/function.mysql-real-escape-string.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.