mostafatalebi Posted December 29, 2012 Share Posted December 29, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/272503-dom-interaction/ Share on other sites More sharing options...
timothyarden Posted December 30, 2012 Share Posted December 30, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/272503-dom-interaction/#findComment-1402106 Share on other sites More sharing options...
SofWare Posted December 30, 2012 Share Posted December 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272503-dom-interaction/#findComment-1402142 Share on other sites More sharing options...
mostafatalebi Posted December 30, 2012 Author Share Posted December 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272503-dom-interaction/#findComment-1402147 Share on other sites More sharing options...
gizmola Posted December 30, 2012 Share Posted December 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272503-dom-interaction/#findComment-1402160 Share on other sites More sharing options...
mostafatalebi Posted December 30, 2012 Author Share Posted December 30, 2012 Javascript? I prefer to have the most basic functions done without any possibility of malfunctioning. If I use JS, then what happens to those users having their browser JS turned off? Quote Link to comment https://forums.phpfreaks.com/topic/272503-dom-interaction/#findComment-1402167 Share on other sites More sharing options...
cpd Posted December 30, 2012 Share Posted December 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272503-dom-interaction/#findComment-1402196 Share on other sites More sharing options...
kicken Posted December 30, 2012 Share Posted December 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272503-dom-interaction/#findComment-1402198 Share on other sites More sharing options...
mostafatalebi Posted December 30, 2012 Author Share Posted December 30, 2012 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); } Quote Link to comment https://forums.phpfreaks.com/topic/272503-dom-interaction/#findComment-1402201 Share on other sites More sharing options...
cpd Posted December 30, 2012 Share Posted December 30, 2012 Define "it does not work". Have you taken any steps to find out why; otherwise known as debugging? Quote Link to comment https://forums.phpfreaks.com/topic/272503-dom-interaction/#findComment-1402205 Share on other sites More sharing options...
mostafatalebi Posted December 30, 2012 Author Share Posted December 30, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/272503-dom-interaction/#findComment-1402223 Share on other sites More sharing options...
cpd Posted December 30, 2012 Share Posted December 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272503-dom-interaction/#findComment-1402240 Share on other sites More sharing options...
mostafatalebi Posted December 31, 2012 Author Share Posted December 31, 2012 I have worked it out. the problem was many number associative arrays that I assign them values. Now with direst variable nothing goes wrong. Quote Link to comment https://forums.phpfreaks.com/topic/272503-dom-interaction/#findComment-1402472 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.