2

I've been through a few similar questions for this, however, with my basic knowledge I haven't been able to find an answer that I can tie directly to what I'm trying to achieve.

For instance, I have found answers regarding arrays to store multiple checkbox values. However, I'm not sure if this is what I need as each of the 3 checkboxes i have are for individual columns in the database.

This is my HTML that i have currently for checkboxes etc:

<form action="addNewClient.php" method="POST">
          <div class="modal-body">
            <p>Please enter the name of the client you wish to create.</p>
            <textarea id="addSQLNoteName" placeholder="Enter client name..." name="title" maxlength="25"></textarea>
            <p>Teams Packge:</p>
            <select name="package">
              <option value="SBE">SBE</option>
              <option value="Enterprise">Enterprise</option>
            </select>
            <br />
            <input type="checkbox" name="portal" value="1"> Premium Portal
            <br />
            <input type="checkbox" name="replicated" value="1"> Replicated
            <br />
            <input type="checkbox" name="client" value="1"> IT Client
            <br />
            <textarea id="addSQLNoteName" placeholder="Important client info..." name="info" maxlength="25"></textarea>
          </div>
          <div class="modal-footer footer-sqlnotes">
            <button type="submit" class="btn btn-success">Add Client</button>
          </div>
      </form>

This is my current PHP code and ive made a few comments just showing which ones are the checkboxes:

<?php include 'connectionDetails.php'; ?>

<?php

session_start();

if (isset($_POST['title'], $_POST['package'], $_POST['portal'], $_POST['replicated'], $_POST['client'], $_POST['info']))
{
    $title = $_POST['title'];
    $package = $_POST['package']; 
    $portal = $_POST['portal'];         //Checkbox
    $replicated = $_POST['replicated']; //Checkbox
    $client = $_POST['client'];         //Checkbox
    $info = $_POST['info'];

    $stmt = "INSERT INTO Clients (Client, TeamsPackage, Rating, Pos, Neg, PremiumPortal, Replicated, ITClient, ClientInfo) VALUES (?, ?, 0, 0, 0, ?, ?, ?, ?)";
    $params = array($title, $package, $portal, $replicated, $client, $info);

    $stmt = sqlsrv_query($conn, $stmt, $params);

    if ($stmt === false) 
    {
        die( print_r(sqlsrv_errors(), true));
    }

    header('location: clients.php');
}

else
{
    echo "No Data";

}


?>

And so in my database, I have three bit field columns, if checked when submitted it will enter as a 1 otherwise it will enter as a 0.

Like I said, I apologise if another question does answer mine, I just couldn't seem to implement it over to what i am trying to achieve.

At the moment, if all 3 boxes are checked then it will add the client, otherwise it fails the isset() which is what I'm trying to get around at the moment.

9
  • do the session arrays contain value? I don't see where they've been set and assigned to something. Commented Jan 4, 2018 at 12:42
  • I think you should have used POST and not SESSION for the arrays in the isset() and it doesn't get through that conditional. It's hard to say what you want to use/do here though. Commented Jan 4, 2018 at 12:43
  • Apologies, those shouldn't be sessions i'll alter those now, pretend you didn't see that bit Commented Jan 4, 2018 at 12:45
  • I would use a ternary operator for all this instead and just use an isset() on a submit button, then the ternaries inside that. Use error reporting and check for errors on the query. Commented Jan 4, 2018 at 12:49
  • @FunkFortyNiner i'll have to look at some examples of using a ternary operator as i haven't come across that before. Thanks for the response as well. Commented Jan 4, 2018 at 12:59

2 Answers 2

2

Get rid of this entire statement:

if (isset($_POST['title'], $_POST['package'], $_POST['portal'], $_POST['replicated'], $_POST['client'], $_POST['info']))

Name your submit button:

<button type="submit" class="btn btn-success" name="submit">Add Client</button>

Then change your conditional to:

Side note: See the Edit: below.

if (isset($_POST['submit'])){

    $portal = (isset($_POST['portal'])) ? '0' : $_POST['portal'];
    $replicated = (isset($_POST['replicated'])) ? '0' : $_POST['replicated'];
    $client = (isset($_POST['client'])) ? '0' : $_POST['client'];

    // add your other form elements 

    // Perform your query here
}

Ternary operators are IMHO, what should be used here.

Using a default value of 0.

You can add the other conditionals from your original post for the other form elements.


Edit:

I made a mistake with the isset()'s for the ternaries. Those should have read as !isset() checking if they are "not" set.

So, I rewrote it as the following:

if (isset($_POST['submit'])){

     $portal = !isset($_POST['portal']) ? '0' : $_POST['portal'];
     $replicated = !isset($_POST['replicated']) ? '0' : $_POST['replicated'];
     $client = !isset($_POST['client']) ? '0' : $_POST['client'];

    // add your other form elements 

    // Perform your query here
}
Sign up to request clarification or add additional context in comments.

15 Comments

that is what i meant to do. it is much cleaner and error safe
@LuisfelipeDejesusMunoz which is what I told the OP also. I guess they were probably on the php.net website and trying to do it themselves, or they needed a slight nudge ;-) Cheers
One observation. checkboxes return on when checked, so instead $_POST['var_name']; should go 1
@LuisfelipeDejesusMunoz they return on when checked only if there is no "preset value" ;-) which they have now. The ternary default of 0 takes care of that.
@FunkFortyNiner what am i setting the default values to in the HTML for the check boxes?
|
1

Credit to @FunkFortyNiner who left me with this solution in a discussion.

if(isset($_POST['portal'])){ 

$portal = $_POST['portal']; 

}else{ 

$portal = 0; 

}

His original answer for some reason wasn't entering the correct values, however this that he suggested has worked.

Rather than using a ternary operator, just doing an isset statement on each of the check boxes and if not checked setting it to 0.

7 Comments

I find it really odd that the ternaries didn't work. Oh well, if it works now ;-)
It is strange, but like you said at least its working.
did you in fact try removing the quotes around the zeros, going from $portal = (isset($_POST['portal'])) ? '0' : $_POST['portal']; to $portal = (isset($_POST['portal'])) ? 0 : $_POST['portal']; and doing the same for the others?
I remember doing that and it didn't fix it :(
Gosh darn it Luke, I now see where I made a mistake in my ternaries. Those should have read as !isset() checking if they are "not" set. I've made an edit to my answer, under Edit:. Sorry about that; that was an oversight on my part.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.