Need a Suggestion

anuj_web

New Member
Messages
145
Reaction score
0
Points
0
Hi,

I want to validate data types of info entered by the user in an HTML form...

I want to know whether it is better to validate in that form itself(using php)
or validate in the PHP file to which the values are being sent

In the fomer case,Whenever i click submit...then everytime the file will send the values to itself then how will i transfer the control to the second PHP file ??

OK will a flag work here ,but how can I call a submit action of a form outside ??
 
Last edited:

konekt

New Member
Messages
100
Reaction score
0
Points
0
You can include one in the other and just send the variables via function. If you do this, it won't matter where you validate.
 
Last edited:

federico_casares

New Member
Messages
49
Reaction score
0
Points
0
Usually the better approach to validation is to validate the data client-side using Javascript (for a quicker and more practical UI) and then make the same validations server-side through PHP. This way you can bring the user a nice and fast experience and you keep your server and data secure (remember that Javascript validations can be avoided just by turning off JS). Regarding the server-side validation issue, I think that the better solution would be to send the form to the page itself and then make the validations on it... Maybe you can have something like this in the very beginning of the script:

PHP:
if(isset($_POST["Submit"])) {
  // Validate the data...
  if(!$errors) {
    // Insert into DB, etc
  } else {
    // Show error message
  }
}

That is, if you aren't using some kind of MVC framework approach. If you're into MVC, then you should check your framework documentation. =)

I hope I've helped you. And remember, if this information was useful to you, you can donate! :)
 
Last edited:

anuj_web

New Member
Messages
145
Reaction score
0
Points
0
Hi ,

again I need some suggestion....

I am making a website ,it has diff registration pages for diff kind of users(3 nos)..I want to insert their data to MySQL thru PHP....I want to ask whether to use ths PHP code in the same page or would it be better if I use a diff page...I thought i'll make a global variable that is set differently at diff registration pages..and use a common PHP page that contains the code for inserting into those tables ,depending on the value set on the global variable diff code will be chosen to update (since all 3 tables have diff fields) from inside an " if " block

Which approach do you prefer ?? the first or the second one ??


also... the webpage i made in my pc looks fine on mine..but when i open it on other pc its widht increases ..showing blank space..how to remove this anomaly ??

One more thing ....when we include a file in to another ..can u please explain the relationship that is created b/w the 2 files.. ??
 
Last edited:

federico_casares

New Member
Messages
49
Reaction score
0
Points
0
If the 3 different kind of users have different fields and a user can't be in more than one category, then you should create 3 diff PHP pages and 3 diff mySQL tables. One PHP page with its script inserts data in one table, other PHP page in other table, and so on... So I believe the approach of using a common script for inserting isn't valid, because you have diff fields, diff tables, etc. This way you can also have some more separation and make an easier to mantain code. But, if you notice that you're repeating an important part of your code in all 3 scripts, then you'll have to consider the unique PHP script approach. It all depends...

For example: if the 3 forms have 6 fields in common and just 2 that are different making a unique script with a switch statement would be a good idea. But if they're totally different, you will be better making a separate script for each one...

BTW, including a file is exactly the same that copypasting that file in the line you are calling the include. =)

Hope it helps! =)
 

anuj_web

New Member
Messages
145
Reaction score
0
Points
0
that wuz very quick to reply... :-O

I said I will use an if block to check the value of the global variable

like if its value is 1 then use the code for inserting into table1 and so on.....

how about dat ??
 
Last edited:

federico_casares

New Member
Messages
49
Reaction score
0
Points
0
And what do you want the global variable for? Maybe you can just check what fields you have in the $_POST array, or use a hidden field. =)
 

anuj_web

New Member
Messages
145
Reaction score
0
Points
0
I just want it to use as a flag maybe..that points what part of code i want PHP to use
 
Last edited:

federico_casares

New Member
Messages
49
Reaction score
0
Points
0
Yes, but you could check what fields in the $_POST array are set, or you could use a hidden field inside the form... That way you don't have to mess with global vars...
 

anuj_web

New Member
Messages
145
Reaction score
0
Points
0
it would be a lot easier to check a single variable than to check an array :D
thats y...
please correct me if i am wrong
 

federico_casares

New Member
Messages
49
Reaction score
0
Points
0
Mmm... Perhaps you could do:

PHP:
if(isset($_POST["type"]) && $_POST["type"]==1) {
  // Registration type 1
}

Being type a hidden field in the form. I prefer not to mess with global variables, but yours is a completely valid approach, nonetheless!
 

anuj_web

New Member
Messages
145
Reaction score
0
Points
0
the webpage i made in my pc looks fine on mine..but when i open it on other pc its widht increases ..showing blank space..how to remove this anomaly ??
 
Top