简体   繁体   中英

Saving data from a combination of forms to a MySQL PHP database

I've successfully been able to save direct data from a form into a database, and then display this data onto a HTML page (the user's username). However, I am struggling to save data (text) that has been generated from a series of drop-down menus.

I have successfully allowed the user to generate a specific Workout Plan based on their chosen selections in four drop-down menus.

JavaScript code:

<script>
jQuery(function ($) {

    var selects = $('#select-container select'),
    results = $('#results-container > div');

    selects.change(function () {
        var values = '';
        selects.each(function () {
            values += '.' + $(this).val();
        });
        results.filter(values).show().siblings().hide();
    });

});
</script>

HTML code snippet:

    <div class="margins1">
<h2>Goal</h2>
<div id='select-container'>
<select>
<option value='none'>Select Option</option>
<option value='FatLoss'>Fat Loss</option>
<option value='LeanMuscle'>Lean Muscle</option>
<option value='SizeMass'>Size & Mass</option>
</select>
<h2>Curent Level</h2>
<select>
<option value='none'>Select Option</option>
<option value='Beginner'>Beginner</option>
<option value='Intermediate'>Intermediate</option>
<option value='Advanced'>Advanced</option>
</select>
<h2>Gym Accessibilty</h2>
<select>
<option value='none'>Select Option</option>
<option value='Yes'>Yes</option>
<option value='No'>No</option>
</select>
<h2>Days Per Week</h2>
<select>
<option value='none'>Select Option</option>
<option value='3DaySplit'>3-Day Split</option>
<option value='4DaySplit'>4-Day Split</option>
<option value='5DaySplit'>5-Day Split</option>
</select>
</div>

<div class="margins3">
<h1>Workout Plan...</h1>
<div id='results-container'>
<div class='LeanMuscle Intermediate Yes 4DaySplit'><b>Day 1: Chest & Triceps </b><br>
Dumbbell/Barbell Bench Press (3 sets, 8-12 reps)<br>
Incline Dumbbell/Barbell Bench Press (3 sets, 8-12 reps)<br>
Dumbbell/Cable Flies (3 sets, 10-15 reps)<br>
Press-ups (3 sets, until failure)
Incline Treadmill Walk (10-15 minutes, slow pace)<br>
<b>Day 2: Back & Biceps</b><br>
Dumbbell/Barbell Rows (3 sets, 8-12 reps)<br>
Pull-ups/Chin-ups (3 sets, 6-12 reps)<br>
Seated Rows (3 sets, 8-15 reps)<br>
Dumbbell/Barbell Bicep Curls (3 sets, 8-15 reps)<br>
Dumbbell Hammer Curls (3 sets, 8-15 reps)<br>
<b>Day 3: Legs</b><br>
Barbell Squats (3 sets, 8-15 reps<br>
Leg Press Machine (3 sets, 8-15 reps)<br>
Leg Extension Machine (3 sets, 8-15 reps)<br>
Leg Curl Machine (3 sets, 8-15 reps)<br>
Calf Raises (3 sets, 8-20 reps)<br>
<b>Day 4: Shoulders & Trapezius</b><br>
Dumbbell/Barbell Shoulder Press (3 sets, 8-12 reps)<br>
Barbell Shrugs (3 sets, 6-12 reps)<br>
Dumbbell Rear Delt Flies (3 sets, 8-15 reps)<br>
Barbell Upright Rows (3 sets, 6-12 reps)<br>
Incline Treadmill Walk (10-15 minutes, slow pace)
    </div>
    </div>
</div>

EDIT: PHP code for registration

    <?php

define('DB_HOST', 'localhost');
define('DB_NAME', 'test');
define('DB_USER','root');
define('DB_PASSWORD','root');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());

function NewUser()
{   
$username = $_POST['username'];
$password =  $_POST['password'];
$query = "INSERT INTO members (username,password) VALUES ('$username','$password')";
$data = mysql_query ($query)or die(mysql_error());
if($data)
{
 header("Location: Correct.php");
}
}

function SignUp()
{
  if(!empty($_POST['username'])&&!empty($_POST['password']))
  {
    $query = mysql_query("SELECT username FROM members WHERE username = '$_POST[username]'") or die(mysql_error());
    $num_rows = mysql_num_rows($query);
    if($num_rows==0) 
    {
      NewUser();
    }
    else
    {
     header("Location: UsernameExisting.html");
   }
 }
 if(empty($_POST['username']))
 {
   header("Location: MissingUsername.html");
 }
 if(empty($_POST['password']))
 {
   header("Location: MissingPassword.html");
 }
 if (empty($_POST['username'])&&empty($_POST['password'])){
  header("Location: MissingDetails.html");
}
}
if(isset($_POST['submit']))
{
  SignUp();
}
?>

As you can see, the workout plan above is formed by a combination of specific selections in the four drop-down lists. I need to save this output in the mealPlan field from the members table in my MySQL PHP database named test (which I've already made). I have tried many ways but cannot get this to work. Could anyone assist me in achieving this as simply as possible?

Your time and support is much appreciated. Many thanks.

Your select tags do not have a name attribute. Name attributes are required in order to send the data to the server.

Do some reading about html forms.

You can also submit it using JSON,

Change your HTML code to have ids associated with all select tags .

And then you can convert that in JSON like below,

        jQuery(function ($) {

var selects = $('#select-container select'),
results = $('#results-container > div');

selects.change(function () {


    var values = '';
    selects.each(function () {
        alert($(this).val());
        values += '.' + $(this).val();
    });


        var values = '{
        "goal" : $("#goal").val(),
        "current_level" : $("#current_level").val(),
        "gym_a" : $("#gym_a").val(),
        "days_P_W" : $("#days_P_W").val(),
        }';

var finalValue = eval('(' + values + ')');

});

});

here goal , current_level , gym_a etc are id's of your select tags.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM