I have created a form that allows a user to tick a checkbox and when the user posts the form I want to somehow split the value of the checkbox after the '-' symbol and insert both values into two separate columns.
The form on page 1:
<form method="post" action="scripts/add-users-to-courses.php">
<table class="table">
<tr>
<td><strong>Course Name</strong></td>
<td><strong>Username</strong></td>
<td><strong>Add User to Course</strong></td>
</tr>
<?php
foreach($course_array as $coursetitle) {
foreach($user_array as $username) {
echo '<tr>';
echo '<td>'. $coursetitle .'</td>';
echo '<td>'. $username .'</td>';
echo '<td><input type="checkbox" name="course" value="'. $coursetitle .'-'. $username .'"></td>';
echo '</tr>';
}
}
?>
</table>
<input type="submit" class="btn btn-default" style="<?php echo $btn_custom; ?>">
</form>
The insert statement (form action):
<?php
require '../manager-session.php';
require '../../db-config.php';
include '../../distributor-config.php';
if(isset($_POST['course'])) {
$selected = explode(',', $_POST['course']);
$coursetitle = $selected[0];
$username = $selected[1];
echo 'Selected: '. $selected .'';
echo 'Course Title: '. $coursetitle .'';
echo 'Username: '. $username .'';
$stmt = $conn->prepare("INSERT INTO users_on_courses (useroncourse, course) VALUES (?, ?)");
$stmt->bind_param("ss", $username, $coursetitle);
$stmt->execute();
header('location: ../add-users-to-courses.php?page=add-users-to-courses&status=success');
exit();
}
else {
header('location: ../add-users-to-courses.php?page=add-users-to-courses&status=error');
exit();
}
$stmt->close();
$conn->close();
?>
At the moment, $selected is equal to 'Course Name - Username'. I am trying to insert the Course Name in to the course column and then obviously the username in the useroncourse column.
Is it possible to do this in the way I have started?
You should use explode()
Example -
$selected = explode('-', $_POST['course']);
$courseName = $selected[0];
$username = $selected[1];
Then you can store these variables into db.
You can use explode:
<?php
require '../manager-session.php';
require '../../db-config.php';
include '../../distributor-config.php';
if(isset($_POST['course'])) {
foreach ($_POST['course'] as $course) {
$selected = explode(',', $course);
$courseTitle = $selected[0];
$username = $selected[1];
$stmt = $conn->prepare("INSERT INTO users_on_courses (useroncourse, course) VALUES (:userOnCourse, :username)");
$stmt->bind_param("userOnCourse", $courseTitle);
$stmt->bind_param("username", $username);
$stmt->execute();
}
$stmt->close();
$conn->close();
}
?>
When you use an input field with this name="names[]"
the result you get in server side will be an array. So if you use name="course"
you are getting an array with the selected courses.
You could use explode(' - ', $courseValue);
but it might fail when the course name or the students name contains also -
.
You could use a regex pattern similiar to /^(\w)[\s+]-[\s+](\w)$/i
but its not safe either.
I suggest you this third option.
Seperate the values form beginning on.
<input type="checkbox"
name="course[]"
value="'. $coursetitle .' - '. $username .'"
data-course="' . $coursetitle . '"
data-user="' . $username . '">
Now use DOM selector to fetch the data
attributes and submit this form using Ajax.
This is also best practice. You don't want to merge already seperated, structured data and extract it afterwards with a risk of failure.