Jump to content

Dom Interaction


mostafatalebi

Recommended Posts

Hello everybody

 

I don't know if I have set the title of this thread truly or not, but I go on

 

I have a HTML form page with a div for error next to each for element.

 

divs are filled with a php code echoing an empty variable. so up to here nothing happens inside the div.

 

but as the form is processed further into another file which is a form-processor, in case of error, how I can set a value for those variables inside the div? because all of this processes does not happen in one single page, and happens withing two pages (HTML form, form processor script), How I can send data back to HTML page where there are PHP codes inside the div and set a value such as "Required!" or "Please do not leave empty" etc. for thos variables?

 

Is it correct to use $_SESSION? Thanks

Link to comment
Share on other sites

Perhaps instead of sending it back to the HTML page you could just have one page called submit.php. Then you check isset($_POST) - if it is do the actions and validation - if its not then display the form (action='submit.php').

 

If you then want to change content if the validation or actions fail you can just echo out the form on that same page & fill the divs with the $variables you want.

 

Sorry if I didn't understand your question right, if this isn't right just post a response and try to make it a little clearer as to what you want.

 

Timothy

Link to comment
Share on other sites

You are right buddy, but what about server-side specific actions such as prevention from double-username registration? I definitely need some sort of PHP-HTML interation and the problem is how I can then send back data from form processor to HTML page. My friend is right about one single page which contains all the code, but what about those situations such as mine where there are two files, which however, is common.

Link to comment
Share on other sites

You are right buddy, but what about server-side specific actions such as prevention from double-username registration? I definitely need some sort of PHP-HTML interation and the problem is how I can then send back data from form processor to HTML page. My friend is right about one single page which contains all the code, but what about those situations such as mine where there are two files, which however, is common.

 

I'm not sure I follow your description in regards to "multiple files", but I will say that the "php-html" interaction you're asking about is provided by Ajax.  You write javascript code which sends specific form data to a php script which can then do the validation and return a message.  Your ultimate processing script should not depend on the "pre-validation" done in javascript functions or the ajax function, but it does handle common tasks like verfication that a "new" username has not been used already.

Link to comment
Share on other sites

A simple solution would be to write a validate function in javascript that sets the values like "Required!" or "Please do not leave empty" before the form gets submitted to php.

 

Extremely bad practice. A user can edit the Javascript or disable Javascript thus allowing them to submit anything, always have server-side validation.

 

I think the OP is simply asking how to relay error messages back to a submission page from a processing page. Your thoughts on using $_SESSIONS is bang on. You can set an error message using sessions and retrieve it after you've redirected to the form page.

Link to comment
Share on other sites

Generally the easiest way to handle form validation/errors is to have the same script do both present the form and process the form.  You can use the include function to separate these steps into separate physical files, but have it be one file for the purposes of the request.

 

Then you would structure the code like this:

<?php

if ($form_is_submitted){ //Something to check if it was submitted
  $Defaults=$Errors=array();

  //Validate form fields
  if (count($Errors)==0){
      //No errors so process the form
      doSomething();
  }
}

?>
...
<input type="text" name="fname" value="<?php echo htmlentities(isset($Defaults['fname'])?$Defaults['fname']:''); ?>">
<span class="errorMessage"><?php echo isset($Errors['fname'])?$Errors['fname']:''; ?></span>

 

 

If for some reason you don't want to structure things into one file like that though, storing the errors in $_SESSION and redirecting back would work fine.

Link to comment
Share on other sites

Thanks I have done this but unfortunately it does not work. I first did this:

 

if(isset($_POST['submit']))
{
if($_POST['input'] == "")
{
   $alert['input'] = "Required";
include(HTML); //  and in html in error place there is the above variable ($alert['input'])
}
else
{
Do the mysql.
}
}
else
{
include(HTML);
}

Link to comment
Share on other sites

I have. It does not work means that it leaves the variables empty as they are, it does not assign them with value "required" as I have stated; something is skipped. Everything works as it should, only the variables are left empty

 

Personally, I've no idea what "it leaves the variables empty" means either. All you've said is you tried someone's suggestion and it didn't work giving incomplete code as your example. Explain clearly and fully exactly what you've done and what errors your experiencing so people can give you a straight answer instead of beating around the bush.

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.