Jump to content

Storing filename in php


divz2

Recommended Posts

Hi,

I am struggling with a form in php. I am trying to add a form by using the include function. I want to include thsi form on all pages so it would be very useful if I can save the current page name somewhere so that I can use to validate the form.

 

For example, this is the form I am trying to include.

 

<?php

if(!empty($errors))

{

if(isset($errors['sendError']))

{

echo '<p><strong class="error">There was a problem with our system please contact us directly.</strong></p>';

}

else

{

echo '<p><strong class="error">Please check the form below for errors.</strong></p>';

}

}

?>

<form action="index.php" method="post" id="form1">

<p>

<label><?php if(isset($errors['name'])) { echo '<span class="error">'; } ?>Name<font color="red">*</font>: <?php if(isset($errors['name'])) { echo '</span>'; } ?></label>

<input id="search1" id="complete" type="text" value="<?php echo $form['name']; ?>" name="name" size="60" align="right" style="background:#EEF5A4" />

 

<label><?php if(isset($errors['company'])) { echo '<span class="error">'; } ?>Company: <?php if(isset($errors['company'])) { echo '</span>'; } ?></label>

<input id="search1" id="complete" type="text" value="<?php echo $form['company']; ?>" name="company" size="60" align="right" />

 

<label><?php if(isset($errors['phone'])) { echo '<span class="error">'; } ?>Phone Number: <?php if(isset($errors['phone'])) { echo '</span>'; } ?></label>

<input id="search1" id="complete" type="text" value="<?php echo $form['phone']; ?>" name="phone" size="60" align="right" />

 

<label><?php if(isset($errors['email'])) { echo '<span class="error">'; } ?>Email<font color="red">*</font>: <?php if(isset($errors['email'])) { echo '</span>'; } ?></label>

<input id="search1" id="complete" type="text" value="<?php echo $form['email']; ?>" name="email" size="60" align="right" style="background:#EEF5A4" />

 

<label><?php if(isset($errors['enquiry'])) { echo '<span class="error">'; } ?>Enquiry: <?php if(isset($errors['enquiry'])) { echo '</span>'; } ?></label>

<textarea id="search1" id="complete" textarea name="enquiry" rows=4 cols=40 value="<?php echo $form['enquiry']; ?>" name="enquiry" size="60" align="right"></textarea>

</p>

<p>

<input type="submit" class="formbutton" value="Submit" /> <input type="reset" class="formbutton" value="Reset" />

</p>

<p><font color="red">*</font> Denotes a required field</p>

</form>

 

Because I am going to use this form on all pages, I would like it to include whatever the filename is rather than index.php in the first line of the form so that when the user clicks submit, it will stay on the same page but validate the form based on the mandatory fields.

 

I don't know if the question is clear. I would very much appreciate any help at all.

 

Thank you very much.

Link to comment
Share on other sites

Hey,

 

After putting that all the pages, in this line use this:

 


<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" id="form1">

 

That server variable should use the current page the form is on, I'm sure as long as those variables are turned on in your php.ini file (usually are by default, as far as I know). Unless you mean to use this same validation script in all of your files and you don't want it to leave the page, which you can add target="_blank" in your form tag and that'll open in a new window leaving your other one in tact. I hope that helps!

Link to comment
Share on other sites

Hey,

 

After putting that all the pages, in this line use this:

 


<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" id="form1">

 

No, don't do that, there are know security issues around the use of $_SERVER['PHP_SELF']; Instead just either leave the action attribute blank (forms default action is to post to itself) or pop the file name in there that the form actually resides in, this makes it validation friendly, that is of course if you are bothered about that sort of thing.

 

Rw

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.