Hi All, I am currently trying to update a check box . Then pass the data back through the same page and if set update DB. The code is long so I shall be as concise as possible.

I have pulled the data from the DB and I am using a check box to submit and test if checkbox has been submitted using isset $_POST on checkbox. I have also used debug on GET and REQUEST and I am not sure why it does not work.

Any assistance would be appreciated.

Thanks in advance

 <?php 
    if (isset($_POST['checkbox'])){
        echo "SET";// udate approval
        exit();
    }
 ?>

 <td class="centre">

    <input class="checkboxnostyle" id="PO_Approved<?= $rowProduct['POID']; ?>" name="PO_Approved<?= $rowProduct['POID']; ?>" onClick="location.href='/sons/approval.php?POID=<?= $rowProduct['POID']; ?>&UserID=<?= $_SESSION['UserID']; ?>';" type="checkbox" value="PO_approved" />

 </td>
Member Avatar for diafol
onClick="location.href='/sons/approval.php?POID=<?= $rowProduct['POID']; ?>

That will pass the info via GET not POST

There is no need to pass the userid as this should be in a session variable anyway. Also, you could use ajax to update, rather than force a refresh.

isset($_POST['checkbox'])

I don't understand why you're checking for this 'name'. The name of the input is "PO_Approved...", but in your url you're only passing the names (querystring parameters): POID and UserID

Hi Diafol, thanks for your reply.
I agree the UserID is in sessions. So to test if checkbox has been submitted
I should test like so?

POID is the DB PK and I need to pass that information and how would I pass this value PO_Approved. I could place form tags around this and pass as a form, this method does not seem to apply then you check the checkbox?

I will look a ajax also.

Any furthe advice appreciated even if it is the incorrect way to do this?

Thanks

David

<?php 
    if (isset($_GET['PO_Approved'])){
        echo "SET";// udate approval
        exit();
    }
 ?>
Member Avatar for diafol

I think you misunderstood. The only data that you're passing to the server are...

POID=<?= $rowProduct['POID']; ?>&UserID=<?= $_SESSION['UserID']; ?>

So the data you can access are:

$_GET['POID'] and $_GET['UserID'] respectively.

The checkbox value is not being sent, neither is the checkbox name. So the server won't know if the checkbox is true or false (checked or unchecked) - unless you assume that it would be correct to toggle the existing DB value 0/1 -> 1/0. Which you SHOULDN'T :)

Hi Diafol,
Thanks for your reply Diafol. I am not making myself very clear and I appolgise in adavace.

I am pulling the following data from the DB POHaulageLossApproval which is an INTEGER see image

2fb614a319f75c809757bb46d04463b8

Once the checkbox has been selected I am looking to pass a value that will tell me the POID so that I can update DB value POHaulageLossApproval

I will then test for it at this point and udate the sql table.

 <?php 
   if (isset($_GET['PO_Approved'])){
         // udate POHaulageLossApproval in db
        exit();
    }
  ?>

As I am only passing the 2 parameters, how can I add a value to pass in the string to test checkbox name/value

Thanks Again

Member Avatar for diafol

Sorry am out for the evening, will come back tonight or tomorrow morning (16:45 here at the mo) if nobody posts in the meantime.

davidjennings maybe diafol will answer that but there are too many things that you are doing wrong in my opinion. This is a tip to check on them. Really what is the reason to use Get after all ? ... To make that URL accessible to everyone ? .. You can do that by applying even the simpler MVC approach in the work flow of Controllers. In your case even that excuse don't apply since you have to check that the user is logged to show that info.

Really what is the reason to use in line onclick events ? In your case you only redirect to another page ... isn't that a link ?. If it was really a first level validation through JavaScript (IF) then you could use a function for that or better a JS base framework (e.g. jQuery or yours).

NEVER RELY ON CLIENT SIDE. I am yelling I know ... but all the validations that made in client side should be made in server side as well (and maybe more depending on business model). We don't do validations in client side for data integrity we are doing them for making the user experience of our visitors better it is all about UI it has nothing to do with server side application logic.

Having said that I strongly recommend to follow any tutorial (e.g. w3schools is amateurish but is out there for long time ) with this sequence ... HTML , CSS , JavaScript , jQuery , PHP .

I didn't answered your question ... maybe diafol will but if this isn't just a simple high school project I would advise you to read my answer twice.

Member Avatar for diafol

Just to say, if you are changing anything like file contents or db data, then you should use POST.

As jkon says - get rid of all that inline js stuff. It really isn't nice. If you really must update server data on checkbox click, then I'd use Ajax.

That's an easy way to pass the state of a specific checkbox.

As you're using sessions and logins, I'm assuming that you will be validating user login against permissions / db data so that malicious users can't, for example, use something like curl or a spoofed page to mess up your data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.