NameSilo

Two simple questions [Php/js]

Spacemail by SpaceshipSpacemail by Spaceship
Watch

Albino

Munky DesignsEstablished Member
Impact
17
hey

right, simple things.

1. Form validation.

I have my form validated by javascript, then by php (incase the user does not have JS enabled). however, when validaed by the server, I link back to the registration page, and all the data they entered is gone. what is the best way to store the day (seeing as $_POST only works across one page change). Putting it in the url is abit dogy, as if there is a lot of text, it can mess up. the only other way I can think of is storing it in a session. Then I could output the value, and also maybe store the error and display it. is this the best way?

2. Checking if JS is enabled.

I know you can use get_browser() to see if the browser is capable of using JS, but is there an easy way to see if it is enabled? The only way I have found is to have a js redirect in the deader, which adds a GET value to the url, and you can check to see if you can actually GET it. Im not too keen on this though, I don't like things in my url if I can help it. I mainly need this to hide certain things, such as the check name function, etc

Cheers +rep or any help :)
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
Generally, I will use POST for forms, not get. The action of the form is set to post to the same URL that the form is on.

On the server, if a form has been submitted, then it is processed, if not, then the form is displayed. If processing fails due to validation, then error messages are set and the form is displayed.

In the form, check to see if your form fields exist in the POST, and if so, use the values from that in your input tags value attributes. ( be sure to encode or strip any html characters and tags )

You could have JS set the value of a form element. Then in your code that processes the form post, you can check the value of that field to determine wether or not JS is enabled.
 
Last edited:
0
•••
sorry I meant post, not get. not sure why I put get.

I think you have misunderstood me (or im bad at explaining).

When you submit a form, all the variables are passed to a different page, for validation. if one doesn't validate, it sends you back to the form. However, because you have gone from form->validation page->form, you can not use POST as you have gone more than one page.
 
0
•••
Albino said:
When you submit a form, all the variables are passed to a different page, for validation.

ah, but not if you submit to the same page. e.g. the form is on myform.php and the processing code is also on myform.php.

some pseudo code:

PHP:
file: myform.php
<?php

$errors = [];

if ( isset( $_POST['btnSubmit'] ) )
{

$errors = validateform();

if( count( $errors ) > 0 )
{
//display error messages
}else{
//redirect to success page
}

}


?>

<form action="myform.php" method="post">

<input name="mytext" value="<?php echo isset( $_POST['mytext'] ) ? $_POST['mytext'] : ''; ?>">

<input type="submit" name="btnSubmit">

</form>
 
1
•••
that is true, but id rather not do that, as ive set all my forms up to redirect to a separate page

I tihnk I'll just go with the sessions

thanks for your help though :)
 
0
•••
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back