Link to home
Create AccountLog in
Avatar of EICT
EICTFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How to check form submitted using Javascript and not PHP, then action PHP if submitted.

Hi,

I have a PHP form which refreshes  using Javascript as a way of passing values from one element of the form to another when the first element changes.  This presents a problem because I want to submit the form for action using a button, but I can't because the form has already autosubmitted as a way of refreshing its self to pass the element values.

In short I need to check that the form has been submitted without using the following PHP:
<input type="submit" name="submit" value="Update Client Record" />
<input type="hidden" name="submitted" value="TRUE" />
if (isset($_POST['submitted'])) { do somthing }


my code has the following elements:
-------------------------------------------------------------

echo '
<script language="JavaScript">

function autoSubmit()    <!-- This autoSubmit passes the value selected from the first element in the form to the second -->
{
    var formObject = document.forms[\'theForm\'];
    formObject.submit();
}    

function process(){
document.theForm.submit();    <!-- This is the bit I need to change-->
}


</script>';

If (...The form is submitted using the process() function run some PHP..)  <! And I need this bit -->

if (isset($_POST['movecat']))
{ // Form has been submitted.
      $movecat = $_POST['movecat'];
}

<form name="theForm" method="post">
<select name="movecat" onChange="autoSubmit();">........</select>
<select name="subcat" onChange="autoSubmit();">  
//....OPTION selected using $movecat variable....
</select>

<input type="button" value="Save Changes" onClick="process(); />

---------------------------------------------------------------------------

I need to change the way the process() funtion works.

something like:
If (theForm.submit == TRUE)
{ do my PHP }

I've no idea how to actually write this because it mixes javascript and php.


I hope this makes sense!!

Thanks for your help
Avatar of Sudhindra A N
Sudhindra A N
Flag of India image

It is not possible to check in javascript whether form is submitted, you have  to use PHP.

Since you have two actions that submits the form to itself, it is better to have a hidden form variable to check whether form is submitted via process() function or autoSubmit() function.

<input type="hidden" name="postFrom" value="auto" />

and in the javascript autoSubmit() function add
formObject.postFrom="auto";

and in the javascript process() funtion add line
document.theForm.postFrom="proc";

values you can change according to your needs.
and in PHP code
check for the value
if(isset($_POST['postFrom']) && $_POST['postFrom'] == 'proc')){
//do process PHP stuff.
}
in the same way you can check for "auto" and execute PHP script conditionally on submit.
I believe you may be able to add an onClick action to the submit button.  The onClick would populate a field in the form.  Your PHP script could test for this field and ignore the data until it was present.

But that said, have you looked for an acceptable design in the jQuery libraries?  There are a lot of smart things in jQuery.  Just a thought... ~Ray
Avatar of EICT

ASKER

Thanks ansudhindra for your quick reply.

I've been playing with what you recommended and I just cannot get it to work.

I have to put the .submit() javascript statements before your code (as below) else the form will not submit at all.

echo '
<script language="JavaScript">

function autoSubmit()
{
    var formObject = document.forms[\'theForm\'];
    formObject.submit();
    formObject.postFrom="auto";
}

function process()
{
document.theForm.submit();
document.theForm.postFrom="proc";

}
</script>';

I've attached a highlighted code version for you to see.

Thanks again
pdpspecific-iss63.php
highlighted-code.doc
Avatar of EICT

ASKER

Hi Ray,

I don't use JQuery or know that much about it. So far I've just hardcoded PHP with some Javascript thrown in.

If you don't mind can you quickly tell me abit about JQuery and how useful it is.

Thanks
Matt
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
What was wrong with the answer?  jQuery is what I tell my students to use after they get a foundation in the principles of JavaScript.  It really is the correct and professional way to do what you're trying to do.  And it is much easier than trying to write your own JS libraries!
https://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=26