madave111 Posted December 4, 2009 Share Posted December 4, 2009 Hi to all. I am having problems with a school homework that I have been assigned. I've been asked to create a PHP page that would be used to display data entered in an HTML form for verification by the user. Then, this page should use hidden fields to transfer the data to another PHP page that would perform saving to a text file. What I am doing really looks stupid, but that's what my teacher wants me to do. In case the user sees that there is something wrong with his input I would like to take notice of this and then send the new input to the PHP page that would do the saving. The aim of the exercise is for me to pretty much just _POST or _GET the hidden fields in the page to the page that would do the saving, so the part that detects change in the text boxes I am doing just to exercise myself. What I've come up with is to have global javascript variables that take in the input from the initial HTML form and then only get changed by JS functions in case any of the text boxes loose focus. Then, once the user submits this page, I would like to call another JS function that would transfer the variables to my save_data.php page. The problem is that, by hitting the submit I don't get redirected to the page that I want to. I see that basically nothing happens besides remaining in the same page and having all the text areas displayed in the URL. Is it the case that a submit button in a form can only take events in the opening <form> tag such as 'action' and 'method' ? If so, it's back to the drawing board for me... Thanks in advance!(be gentle, just starting to walk when it comes to Inet progamming). My code for this second PHP page is: <?php $valueFnam=$_POST['Fname']; $valueLnam=$_POST['Lname']; $valueEmail=$_POST['Email']; echo "<html>". "<head>". "<script type='text/javascript'>"." var firstname='<?php echo $valueFnam; ?>'; var lastname='<?php echo $valueLnam; ?>'; var email='<?php echo $valueEmail; ?>'; function fnameChange() { var firstnamechange=document.form1.FN.value; firstname=firstnamechange; } function lnameChange() { var lastnamechange=document.form1.LN.value; lastname=lastnamechange; } function emailChange() { var emailchange=document.form1.EM.value; email=emailchange; } function submitForm1() { window.location.href='save_data.php?FN=' + firstname + '&LN=' + lastname + '&EM=' + emails; } "."</script>". "</head>". "<body>". "<form id='form1'>" . Is it the case that the submit button can only take events specified in here ??? "<div style='padding-left:18em'>" . "<h4 style='padding-left:7.8em'>Please confirm</h4>". "<t style='float:left;padding-left:3.9em'>Enter your first name </t>". "<input type='text' name='Fname' id='FN' style='margin-left:0.5em;' value='$valueFnam' onBlur='fnameChange()'>". "<input type='hidden' name='FnameHidden' style='margin-left:0.5em;'value='$valueFnam'>". "</div>". "<div style='padding-left:20em'>". "<t style='float:left;padding-left:2em;margin-top:0.2em'>Enter your last name </t>". "<input type='text' name='Lname' id='LN' style='margin-left:0.5em;margin-top:0.2em' value='$valueLnam' onBlur='lnameChange()'>". "<input type='hidden' name='LnameHidden' style='margin-left:0.5em;margin-top:0.2em' value='$valueLnam'>". "</div>". "<div style='padding-left:20em'>". "<t style='float:left;margin-top:0.2em'>Enter your e-mail address </t>". "<input type='text' name='Email' id='EM' style='margin-left:0.5em;margin-top:0.2em' value='$valueEmail' onBlur='emailChange()'>". "<input type='hidden' name='EmailHidden' style='margin-left:0.5em;margin-top:0.2em' value='$valueEmail'>". "</div>". "<div style='padding-left:20em;margin-top:1em;margin-top:0.2em'>" . "<input type='submit' name='submit' style='margin-left:7.5em' onSubmit='submitForm1()'>". "</div>". "</form>". "<form action='index.html'>". "<div style='padding-left:20em;margin-top:1em;margin-top:0.2em'>" . "<input type='submit' value='Go remedy input' name='submit' style='margin-left:7.5em'>". "</div>". "</form>". "</body>". "</html>"; ?> Quote Link to comment Share on other sites More sharing options...
JustLikeIcarus Posted December 4, 2009 Share Posted December 4, 2009 Why do you need the submitForm action to run if its just changing the names of your form vars? I would remove that and change the names of your form vars to match their id's and add action="save_data.php" to your form. Quote Link to comment Share on other sites More sharing options...
JustLikeIcarus Posted December 4, 2009 Share Posted December 4, 2009 Also you can make your life alot easier by ditching all of those quotation marks. You know that you can write your code like html stuff .... <? php stuff ... ?> html stuff <? php stuff ?> right? Quote Link to comment Share on other sites More sharing options...
madave111 Posted December 4, 2009 Author Share Posted December 4, 2009 Hi! Thanks for replying. Actually I'm not trying just to rename variables' names, but rather to pass along changed variables content to another PHP page. I've also tried attaching my submitForm1() function to the opening form tag by specifying the onSubmit event, but alas the situation is the same. The function submitForm1() is just not executed Quote Link to comment Share on other sites More sharing options...
JustLikeIcarus Posted December 4, 2009 Share Posted December 4, 2009 actually i think its getting executed but not posting anything because your hijacking the submit and then changing the window.location. instead of window.location.href = .. try just using window.location = ... and make sure your using $_GET on the save_data.php script Quote Link to comment Share on other sites More sharing options...
JustLikeIcarus Posted December 4, 2009 Share Posted December 4, 2009 Better yet just set your js/hidden inputs up like this and set your form action to the page you want. function fnameChange() { document.form1.FNnew.value = document.form1.FN.value; } <input type='text' name='Fname' id='FN' style='margin-left:0.5em;' value='$valueFnam' onBlur='fnameChange()'> <input type='hidden' name='FN' id='FNnew' style='margin-left:0.5em;'value='$valueFnam'> Quote Link to comment Share on other sites More sharing options...
madave111 Posted December 4, 2009 Author Share Posted December 4, 2009 :'( Same thing. What happens exactly is that after pressing the submit button I get redirected to the very same bpage. The URL begins with a reference to the page name followed by the form elements. Why is this happening ?? i http://localhost/xampp/a4/confirm_data.php?Fname=trima+manafasdi+i+az&FnameHidden=trima+manafi+i+az&Lname=&LnameHidden=&Email=&EmailHidden= ......is what I get after submission. confirm_data.php is the same page. Quote Link to comment Share on other sites More sharing options...
JustLikeIcarus Posted December 4, 2009 Share Posted December 4, 2009 Oh Wait! your gonna need to add "return false;" to the js submit function or else it will submit to itself and the href change wont matter. Quote Link to comment Share on other sites More sharing options...
madave111 Posted December 4, 2009 Author Share Posted December 4, 2009 No luck. Tried that both with the href object and without it. the situation is still the same, the page is just getting posted back to itself. Or it's not doing nothing and all the elements from the form I see concatenated in the URL after submission is just regular form behavior. Quote Link to comment Share on other sites More sharing options...
madave111 Posted December 4, 2009 Author Share Posted December 4, 2009 Do you think it would make a difference if I separate the HTML form the php echo and just have them in separate paragraphs as you suggested? I haven't done so just yet, because I dont wanna delete all these parenthesis for nothing. Quote Link to comment Share on other sites More sharing options...
JustLikeIcarus Posted December 4, 2009 Share Posted December 4, 2009 No thats not making a difference. I would move the onSubmit call since it belongs only to the form element. input elements dont have onSubmit so i believe it only works if in the form tag Quote Link to comment Share on other sites More sharing options...
madave111 Posted December 4, 2009 Author Share Posted December 4, 2009 I already have it there after finding a page with javascript event handlers and seeing that for form objects the only handler is onSubmit. Still no luck. Quote Link to comment Share on other sites More sharing options...
madave111 Posted December 4, 2009 Author Share Posted December 4, 2009 It shouldn't make a difference though(where you place the onSubmit). The link is quite old, but I dont think stuff has changed about event handlers about those specific objects(form) that much. Here's the link: http://www.irt.org/articles/js058/#3. Quote Link to comment Share on other sites More sharing options...
JustLikeIcarus Posted December 4, 2009 Share Posted December 4, 2009 still I think your making this harder than it needs to be. I would suggest you give the form the correct action url, then set the value of the hidden elements when the others are modified. let the form submit occur normally. Does that make sense? I dont see any reason you need to specify the new location in a submit js function when you can just let the forms action do it. Quote Link to comment 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.