Jump to content

Calling PHP function and JavaScript function on Form Submit Not Working


max_maggot

Recommended Posts

Hi all,

The following code was working fine up until 1 hour ago and now fails to execute the PHP function call or anything within isset($_POST

​The JavaScript works successfully but I had to change the order in which form onsubmit and form action were called. As I said, I was getting the echo message back from the function until 1 hour ago. Here is the code.

 

At the start of my page with the form:

<?php
session_start();
require_once 'class.user.php';
$user_home = new USER();

if(!$user_home->is_logged_in())
{
    $user_home->redirect('signin.php');
}

$stmt = $user_home->runQuery("SELECT * FROM user WHERE id=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);

// print_r($row['learner_type']);
?>

The code to check if the submit button has been clicked

<?php
            if(isset($_POST['save_profile_changes']))
            {
                $user_home->save_user_profile();
                ?>
                        <div class='alert alert-success alert-dismissible'>
                            <button class='close' data-dismiss='alert'>×</button>
                            <strong>Profile Submitted Successfully</strong>
                        </div>

            <?php
            }
            else {
                //Left blank so it doesn't fire on form first load
            }
            ?>

and the form code

<form id="user_profile" name="user_profile" class="form-signin" onsubmit="return validateUserInput()" method="post" action="#">

                <h2 class="form-signin-heading">Update Profile</h2>

                <!--Username, Email. Unchangeable for now-->
                <div>
                    <strong>
                        <label><strong>Username: </strong></label>
                        <?php echo $row['username']; ?>
                        <br>
                        <label><strong>Email: </strong></label>
                        <?php echo $row['email']; ?>
                    </strong>
                    <br>
                    <a href="change_pass.php" style="padding-top:10px; font-weight: bolder">Change Password</a>
                    <br><br>
                </div>

                <!-- First, Last Name -->
                <div>
                    <input id="first_name" name="first_name" type="text" placeholder="First Name" class="form-control" minlength="2" maxlength="15" pattern="[A-Za-z]{2,15}" title="Only letters allowed" required>
                    <input id="last_name" name="last_name" type="text" placeholder="Last Name" class="form-control" minlength="2" maxlength="25" pattern="[A-Za-z]{2,25}" title="Only letters allowed" required>
                </div>

                <!-- File Button -->
                <div>
                    <label for="upload_photo">Upload Photo</label>
                    <input class = "form-control" id="upload_photo" name="upload_photo" type="file" accept="image/*">
                </div>

                <!-- DOB input-->
                <br>
                <div>
                    <label for="dob">Date of Birth</label>
                    <input id="dob" name="dob" type="date" class="form-control" min="1935-12-31" max="2014-12-31"required>
                </div>

                <!-- Gender -->
                <br>
                <div>
                    <label for="Gender">Gender</label><br>

                    <label class="radio-inline" for="gender-0">
                        <input type="radio" name="gender" id="gender-0" value="male" required>
                        Male
                    </label>
                    <label class="radio-inline" for="gender-1">
                        <input type="radio" name="gender" id="gender-1" value="female">
                        Female
                    </label>
                    <label class="radio-inline" for="gender-2">
                        <input type="radio" name="gender" id="gender-2" value="other">
                        Other
                    </label>
                </div>

                <!-- Region -->
                <br>
                <div>
                    <label for="country">Select Your Country</label>
                    <script type="text/javascript" src="assets/countries.js"></script>
                    <select id="country" name="country" class="form-control" required></select>
                    <script language="javascript">
                        populateCountries("country");
                    </script>
                </div>

                <!-- Occupation-->
                <br>
                <div>
                    <label for="occupations">Select Your Occupation</label>
                    <script type="text/javascript" src="assets/occupations.js"></script>
                    <select id="occupations" name="occupations" class="form-control" required></select>
                    <script language="javascript">
                        populateOccupations("occupations");
                    </script>

                </div>

                <!-- Primary Instructor Language -->
                <br>
                <div>
                    <label for="languages">Primary Language of Instruction</label>
                    <script type="text/javascript" src="assets/languages.js"></script>
                    <select id="languages" name="languages" class="form-control" required></select>
                    <script language="javascript">
                        populateLanguages("languages");
                    </script>
                </div>

                <!-- Bio -->
                <br>
                <div>
                    <label for="bio">Bio (max 200 words)</label>
                    <textarea class="form-control" rows="10" id="bio" name="bio" placeholder="Say a little about yourself." maxlength="200"> </textarea>
                </div>

                <!--Submission Buttons-->
                <hr>
                <div>
                    <button class="btn btn-primary btn-common font18" type="submit" name="save_profile_changes">Save Changes</button>
                    <a href="home.php" style="float:right; padding-top:10px;">Go Back Home</a>
                </div>
            </form>

Thanks for any help you can provide.

Link to comment
Share on other sites

If it helps, I changed the following

<form id="user_profile" name="user_profile" class="form-signin" onsubmit="return validateUserInput()" method="post" action="<?php $user_home->save_user_profile(); ?>" >

and now the PHP function fires but the javascript does not, Here is what I get at the top of my form when I load it in the browser without clicking submit

 

alert("message successfully sent")" >

Link to comment
Share on other sites

Ok, I think the function is firing because the defaults in my dropdown lists are not null e.g. Please Select Country is taken as a set value and therefore the form is processed by the PHP. I believe I'll have to check for these values in an IF statement and ignore the function call in that case i.e on first load of form.

Link to comment
Share on other sites

If none of the isset code is running then that value is not set. Meaning the button was not included with the form submission. You didn't include the source for validateUserInput.

 

If the isset code is running, which you can tell because the success message appears (or at least is returned through the AJAX), then it's the saving specifically that isn't working. Sounds like this may be the case?

 

Meanwhile

<form id="user_profile" name="user_profile" class="form-signin" onsubmit="return validateUserInput()" method="post" action="<?php $user_home->save_user_profile(); ?>" >
will definitely not work. Javascript cannot call PHP functions directly.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.