Jump to content

Can you change the ouput of DIE to redisplay form


AdRock

Recommended Posts

I would like to know if it is possible to change what is output when using the DIE function.

 

At the moment I have just got an error message but this doesn't help becuase I have a form which is cleared when i go back from recieving the error message.

 

I have validation that makes sure something is entered in the text boxes and if any fields are missing it displays the form again complete with any entered fields.

 

I would like to know if there is alternative to using DIE so that I can echo the form again in case the user doesn't enter a certain field.

 

The part of my code i want to check is the resizing of an image.  If the user doesn't select a resizing option then it goes to this code below but I would like to redisplay the form again so the user doesn't lose everything they have entered

 

//The code which resizes an image goes here
else { 
        die ('<strong>You didn\'t specify any resizing options.</strong>'); 
    }

 

Here is the code for my form complete with my validation

 

$form="		
<form enctype=\"multipart/form-data\" method=\"post\" action=\"$self\">

    <p class=\"style1\">Please enter a title for the news article.
    <input class=\"form\" type=\"text\" title=\"Please enter a title for the news article\" name=\"txtName\" value=\"$name\" size=\"30\"/></p>

    <p class=\"style1\">Please enter the content for the news article.
    <textarea class=\"form\" title=\"Please enter the content for the news article\" name=\"txtMessage\" rows=\"25\" cols=\"30\">$message</textarea></p>

    <fieldset>
    <legend><b>Image Dimensions</b></legend>

    	<div><label for \34image\">Image:</label>
    	<input type=\"file\" name=\"image\"></br></div>

    	<div><label for \"percent\">Resize to:</label>
	<input type=\"text\" name=\"percent\" size=\"1\" /> % (percentage)</div>

    	<div><label for \"new_width\">OR new width:</label>
    	<input type=\"text\" name=\"new_width\" size=\"1\" /> pixels (height will be calculated automatically)</br></div>
    
    	<div><label for \"new_height\">OR new height:</label>
    	<input type=\"text\" name=\"new_height\" size=\"1\" /> pixels (width will be calculated automatically)  </br></div>

<div><label class=\"left\" for \"both\">OR new height and new width:</label>
<input type=\"text\" name=\"width\" size=\"1\" /> pixels</br>
     
<input type=\"text\" name=\"width\" size=\"1\" /> pixels</br></div>
    </fieldset>
    <p class=\"style3\">
    <input class=\"submit-button\" style=\"margin-left:0\" type=\"Submit\" name=\"submit\" value=\"Submit\">
</form>";

if($submit)
{
    $valid=true;

if( !$name )
{ 
    $errmsg.="Please enter a title for the news article:<br>";
    $valid=false;
}

if( !$message)
{
    $errmsg.="Please enter the content for the news article:<br>";
    $valid=false;
}
}

if( $valid !=true )
    {
echo( "<span style=\"font-weight: bold; color:red;\">".$errmsg."</span>" . $form );
    }

else {

 

Also is it possible to do something similar when using the mysql_query function?

I often make a function, that shows a form with an error message and then dies.

Example:

 

<?php
function die_form($err = "")
{
if($err != "")
	echo "<b>Error: ".$err."</b>";
echo "<form action=\"someaction.php\" method=\"POST\">";
echo "<input type=\"submit\" name=\"submit\" value=\"next page\">";
die();
}
?>

 

As you can see, the $err variable is optional- you can call the function without an error to just display the form by calling die_form() and if you want to output an error, you pass it: die_form("Fill everything!");

 

Orio.

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.