0

I am working on this project which is going fine so far, however I have this one problem which I simply can't see how to solve.

I am having a section on a page where you can edit your information.

II am checking for $_POST on the same page to avoid redirecting the site, however it seems like the submit button aren't submitting anything.

EDIT: I DON'T get any PHP error, nothin simply happens when I press the submit button.

My PHP on the same page as the form:

require_once("inc/functions.php");

if(isset($_POST['saveButton'])){
    $emailValue = mysql_real_escape_string($_POST['email']);
    $bukserValue = mysql_real_escape_string($_POST['bukser']);
    $jakkeValue = mysql_real_escape_string($_POST['jakke-str']);
    $skoValue = mysql_real_escape_string($_POST['shoes']);
    setUserSetting($emailValue, $bukserValue, $jakkeValue, $skoValue);
};

My PHP function:

function setUserSetting($emailValue, $bukserValue, $jakkeValue, $skoValue) {
    $emailValue = strip_tags($emailValue);
    $uid = $_SESSION['usrId'];
    $sql = "UPDATE usr SET email='$emailValue', sko='$skoValue', jakke='$jakkeValue', bukser='$bukserValue' WHERE id='$uid'";

    $result=mysql_query($sql);
    if($result === FALSE) { 
        die(mysql_error());
    } else {
        $_SESSION['usrEmail'] = $emailValue;
        return "gemt";  
    };
};

And finally my html form:

                       <section id="settings" class="bg-light-gray">
    <div class="container">
        <div class="row">
            <div class="col-lg-12 text-center">
                <h2 class="section-heading">Indstillinger</h2>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12">

                <form class="form-horizontal" action="#" method="post" role="form" name="settForm">
                    <fieldset>

                <!-- Text input-->
                        <div class="form-group">
                                <label class="col-md-4 control-label" for="email">E-mail</label>  
                            <div class="col-md-4">
                                <input id="email" name="email" type="text" placeholder="E-mail" class="form-control input-md" value="<? echo getUserSetting($usrEmail, 'mail'); ?>">
                            </div>
                        </div>

                        <div class="form-group">
                                <label class="col-md-4 control-label" for="bukser">Bukser størrelse</label>
                            <div class="col-md-4">
                                <select id="bukser" name="bukser" class="form-control">
                                    <option value="C44">C44</option>
                                    <option value="C46">C46</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                          <label class="col-md-4 control-label" for="jakke-str">Jakke størrelse</label>
                            <div class="col-md-4">
                                <select id="jakke-str" name="jakke-str" class="form-control">
                                    <option value="XS">XS</option>
                                    <option value="S">S</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                          <label class="col-md-4 control-label" for="shoes">Sko størrelse</label>
                            <div class="col-md-4">
                                <select id="shoes" name="shoes" class="form-control">
                                    <option value="36">36</option>
                                    <option value="37">37</option>
                                </select>
                            </div>
                        </div>
                    </fieldset>
                <button type="submit" class="btn btn-xl text-center center-block" id="saveButton" name="saveButton">Gem ændringer</button>
            </form>
        </div>
    </div>
</div>

Emil Elkjær
  • 685
  • 1
  • 9
  • 31
  • remove the `action` totally,it'd redirect to the same page anyways..i think it's because you did `action=""` , therefore making your form look like `
    `
    –  Nov 25 '15 at 10:48
  • The thing is that it works on my login page: – Emil Elkjær Nov 25 '15 at 10:58

4 Answers4

0

Solution:

ADD value to your button! Testing it in local i saw that the button parameter is empty so isset() evalutate to false.

  <button type="submit" class="btn btn-xl text-center center-block" id="saveButton" name="saveButton" value="saveButton" >Gem ændringer</button>

As for <input type="button"> is the value attribute that carry the button value.

Additional details:

First for a better practice i will separate your form with the businnes script.

 <form class="form-horizontal" action="" method="post" role="form" name="settForm">

to

<form class="form-horizontal" action="**YOURSCRIPT.PHP**" method="post" role="form" name="settForm">

Your select is missing </select> tag

You can always clean your data from within your "controller" script, it will be a lot more maintainable. For making a good UX, you can add client side validation using javascript.

Additionally, mysql extension is deprecated. Go for mysqli or even better PDO.

Yuri Blanc
  • 636
  • 10
  • 24
0

The problem is because of the missing closing </select> tags in your code. And because of that php will give Undefined index: Sko error. So include missing tags first.

// your code

<div class="form-group">
  <label class="col-md-4 control-label" for="jakke-str">Jakke størrelse</label>
    <div class="col-md-4">
        <select id="jakke-str" name="jakke-str" class="form-control">
            <option value="XS">XS</option>
            <option value="S">S</option>
        </select>
    </div>
</div>

<div class="form-group">
  <label class="col-md-4 control-label" for="Sko">Sko størrelse</label>
    <div class="col-md-4">
        <select id="Sko" name="Sko" class="form-control">
            <option value="36">36</option>
            <option value="37">37</option>
        </select>
    </div>
</div>

// your code

Sidenote: Please don't use the mysql_ database extensions, they were deprecated in PHP 5.5.0 and were removed in PHP 7.0.0. Use mysqli or PDO extensions instead. And this is why you shouldn't use mysql_

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
0

remove mysql_real_escape_string() function from the code where you are fetching posted values

amriG
  • 60
  • 10
0

Problem was solved.

Me who oversaw a javascript error.

Note to self: Don't code while tired :D

Thanks for your suggestions though!

Emil Elkjær
  • 685
  • 1
  • 9
  • 31
  • Can you post your solution? since i found your button parameter empty in the request. Im courios about what javascript function can create this issue. – Yuri Blanc Nov 25 '15 at 11:23
  • @YuriBlanc - Nothing changed in the posted code. I just had a .js in my project interferring with the solution, therefore it didn't work :-) – Emil Elkjær Nov 25 '15 at 11:41