I was wondering if someone could help me make a change, I want to disable an input according to the option chosen.
When selecting “PESSOA FISICA”, it disables the input of the “PESSOA JURIDICA”, and vice versa.
Another issue is that to submit the form and a validation error occurs, it is not with the person chosen fields open. That is, if I choose the “PESSOA JURIDICA”, submit the form and an error occurs, it returns to the fields of the “PESSOA FISICA”, is not in the fields of “PESSOA JURIDICA”. I know I have to create a function type checked, but do not know how to do this.
I do not have much knowledge in java, just the basics. Could anyone help me do this? take much of this change.
Are you sure?
This means that the users won’t have the ability to alter their choice once they have made it.
For example, if they click on “PESSOA FISICA”, that disables “PESSOA JURIDICA”.
Then, if they change their mind and want to choose “PESSOA JURIDICA” instead, they can’t, as it is disabled.
If this is really what you want to do, let me know and I’ll show you how, but be warned - it is not very user friendly.
This is something you have to do in PHP.
First, move the radio buttons inside the <form> tag.
Then you will be able to retrieve the values for the radio buttons in your script like this:
$tipoDeCadastro = $_POST['selecionar'];
You only want to do this once the form has actually been submitted, so do this:
$tipoDeCadastro ='';
if ($_SERVER['REQUEST_METHOD'] == "POST"){
$tipoDeCadastro = $_POST['selecionar'];
}
Then, you can add this to your HTML to set the buttons accordingly when the form is re-rendered after a submission:
I’ve actually managed to make the script work, to disable the fields.
I’m having trouble just keeping the selections that the user chose intact.
A friend of mine told me the following: " Are you doing a server-side (PHP) validation only without client-side (JS) validation? I suggest you do both as much as possible. With JS validation, the page will never be refreshed and the selections will be kept intact. "
I could not answer he do not know what is server-side. There is no way to do this with jquery?
Really? Would you mind posting your code so I can see what you ended up with?
Ok, well normally when you submit a form you post the form data to a script on a server.
If you use JavaScript to validate the form data before it is sent, this is called client-side validation.
The advantage of this method is that it is quick and, as your friend says, in the event of validation failing no action is taken, meaning that the page does not refresh and the data the user entered stays in the form fields.
The disadvantage of this method, is that it is simple to disable JavaScript in the browser, meaning that client-side validation can easily be sidestepped by a malicious user.
That’s why, you should also use server-side validation.
When your form data reaches the server, it will be available in the superglobal $_POST array. You can use this to assign your form data to variables and validate it accordingly.
This method is however slower and has the added disadvantage that following the user pressing submit, the page will be refreshed and if validation fails, the form will be re-rendered and the user’s input will be lost.
To compensate for this, a common practice is to re-echo the values the user entered back to the form (as I demonstrated in my previous post).
Of course you can do this using JavaScript, but as your friend said, it is better to do both, just to be on the safe side.
If there is anything specific I can help you with in this respect, just let me know.
In fact it will only disable the fields that will appear when selecting a specific option, depending on the option chosen then it will disable the fields opposite.
Well, actually I do not need the value to remain intact in the fields, because the system that work already possess it.
What I need is that when the user chooses the option of input “radio”, this option remains if the page was updated. I have included the code as you said, but refresh the page to trigger the submit, he not remained with the option checked.
You will notice several changes to your code, namely that I removed the inline JavaScript from the form and that I removed the table you were using for layout purposes.
This really makes your code easier to maintain and much more accessible.
If you have any questions to any of this, let me know.