Jump to content

Referencing a PHP page in a form submit button event.


madave111

Recommended Posts

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>";

 

 

?>

 

 

 

 

Link to comment
Share on other sites

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  :shrug:

Link to comment
Share on other sites

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'>

Link to comment
Share on other sites

:'( 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.

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.