2

I have one php file that displays two forms. THe first Form displays data of customers from a table in mysql which also includes a checkbox. The second form contains data from another table and also includes a checkbox this form contains messages. I need to send messages selected with the checkbox to the list of customers selected in the first form.

Can I do that with pure php? or will I need to use javascript? please point me to a tutorial.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Elmer Tillett
  • 51
  • 1
  • 1
  • 4
  • you can use just php , but better to use js or jquery cos it is better to check the values of your forms inputs before sending data to server, and for tutorials just Type Javascript tutorials in google or on youtube and you will get a thousands of good results – William Kinaan May 16 '12 at 18:13
  • William, how can this be done just using php, care to elaborate ? – aziz punjani May 16 '12 at 18:15
  • @Interstellar_Coder do you want to load the customers names and the messages from your database when the page is opening ? or it is a static information ? – William Kinaan May 16 '12 at 18:18
  • 1
    @WilliamKinaan You can use JS to validate, but you should use PHP to validate even with JS. JS validation is to provide quicker/better feedback to users, Server side validation is to actually protect against bad data. – CLo May 16 '12 at 18:23
  • @Chris yes yes you right , but i am asking why he wants to use two forms? he can use just one form – William Kinaan May 16 '12 at 18:24
  • may I ask how can I implement one form? – Elmer Tillett May 16 '12 at 19:41

2 Answers2

0

you need download Jquery library...and read what is ajax

get value

var value = $("#customer").val();

we get value for selector where id="customer"

get cheked

if($('#customer').is(":checked")) { } else { }

send data on server

var value = $("#customer").val();
$.ajax({
            type: "POST",
            url: "default.php",
            data: "name="+value,
            success: function(data){
            alert('ok!') ;
            }
});

we send data on server post method...we send variable value, value = value in value id="customer"

Good luck! Sorry for my English :)

johniek_comp
  • 322
  • 3
  • 8
0

The initial problem with what you are trying to accomplish is that you cannot natively submit two forms concurrently.

Is it absolutely necessary to have two separate forms? If so, you will need to implement something like this (written by Roatin Marth) in order to copy values over from one form to another when submitting:

function form2form(formA, formB) {
   $(':input[name]', formA).each(function() {
     $('[name=' + $(this).attr('name') +']', formB).val($(this).val())
   })
}

Of course, if your business requirements do not require two separate forms, you can just place all the values into a single form and then process it with PHP. If you require validation of the form prior to submission, you will want to do that with Javascript first.

Once in PHP, you will get the values from the $_POST superglobal. You can then do what you need to do with it.

// With each customer checked, send checked messages
foreach($_POST['customers'] as $customer)
{
   // With this customer, send all messages
   foreach($_POST['messages'] as $message)
   {
      // Send $message here
   }
}
Community
  • 1
  • 1
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
  • Yes it is necessarily to have two forms, the user sending message will need to select to whom the messages will be sent, per say to which teachers the message will be sent. – Elmer Tillett May 16 '12 at 19:37
  • If you want it to visually appear as two separate forms on the page, that is a matter of some CSS styling. – Jeremy Harris May 16 '12 at 19:59