1

I am having trouble that will change the value of a hidden input type. I have a search that has multiple checkboxes created with a PHP while loop. I want to pass the items that are checked into a variable in PHP once submitted. This is the form with checkboxes and hidden value.

$search_sql = "SELECT * FROM `company` WHERE `groups` = '$groups' AND (`companyname` LIKE '%$search%' OR `directurl` LIKE '%$search%' OR `email` LIKE '%$search%' OR `phone` LIKE '%$search%' OR `groups` LIKE '%$search%' OR `notes` LIKE '%$search%')"; 
                $result = mysql_query($search_sql) or die(mysql_error());
                while($row = mysql_fetch_array($result))
                {   

                    //remove the http from the links
                    $website  = $row['directurl'];
                    $website = str_replace("http//", "", "$website");
                    $website = str_replace("http://", "", "$website");
                    $website = str_replace("https://", "", "$website");
                    $website = str_replace("https//", "", "$website");


                    //button to change colour

                    //display contacted companies
                    if($row['contact'] == 0){
                    //not contacted
                    $changecolor = '1';
                    $contacted = '<tr class="nocontact border">';
                    $button = '<td>
                    <form name="change_colour" method="post">
                    <input type="hidden" name="id" value="' . $row['id'] . '">
                    <input type="hidden" name="change_colour" value="' . $changecolor . '">
                    <input type="hidden" name="search" value="' . $row['companyname'] . '">
                    <input type="hidden" name="groupselected" value="' . $groups . '">
                    <input type="submit" value="C"></form>';

                    }
                    //contacted
                    elseif($row['contact'] == 1){
                    $changecolor = '2';
                    $contacted = '<tr class="contact border">';
                    $button = '<td>
                    <form name="change_colour" method="post">
                    <input type="hidden" name="id" value="' . $row['id'] . '">
                    <input type="hidden" name="change_colour" value="' . $changecolor . '">
                    <input type="hidden" name="search" value="' . $row['companyname'] . '">
                    <input type="hidden" name="groupselected" value="' . $groups . '">
                    <input type="submit" value="C"></form>';

                    }
                    //positive feedback
                    elseif($row['contact'] == 2){
                    $changecolor = '3';
                    $contacted = '<tr class="positive border">';
                    $button = '<td>
                    <form name="change_colour" method="post">
                    <input type="hidden" name="id" value="' . $row['id'] . '">
                    <input type="hidden" name="change_colour" value="' . $changecolor . '">
                    <input type="hidden" name="search" value="' . $row['companyname'] . '">
                    <input type="hidden" name="groupselected" value="' . $groups . '">
                    <input type="submit" value="C"></form>';

                    }
                    //negative feedback
                    elseif($row['contact'] == 3){
                    $changecolor = '0';
                    $contacted = '<tr class="negative border">';
                    $button = '<td>
                    <form name="change_colour" method="post">
                    <input type="hidden" name="id" value="' . $row['id'] . '">
                    <input type="hidden" name="change_colour" value="' . $changecolor . '">
                    <input type="hidden" name="search" value="' . $row['companyname'] . '">
                    <input type="hidden" name="groupselected" value="' . $groups . '">
                    <input type="submit" value="C"></form>';

                    }
                    else {echo 'error with the display';}

                    //button to select companies

                    $select_comp = '<form name="select_comp" method="post">
                    <input type="checkbox" id="' . $row['id'] . '" name="id" onclick="compTrig(' . $row['id'] . ')">
                    <input type="hidden" id="return_comp" name="return_comp" value="me">
                    <input type="submit" name="select_comp" value="Selected"></form></td>';

                        //Display link to website if available
                        if ($website !== ''){
                            $webdisplay = $contacted . $button . $select_comp . '<td><a target="_blank" href="http://' . $website . '">' . $row['companyname'] . '</a></td>';
                        }
                        else{
                            $webdisplay = $contacted . $button . '<td class="red">' . $row['companyname'] . '</td>';
                        }

                        //check if email has been submitted
                        if ($row['email'] !== ''){
                            $email = '<td><a href="mailto:' . $row['email'] . '">Email</a></td>';

                        }
                        else{
                            $email = '<td class="red">None</td>';
                        }

                    //display company details
                    echo $webdisplay;
                    echo '<td>' . $row['contactname'] . '</td>';
                    echo $email;    
                    echo '<td>' . $row['town'] . '</td>';
                    echo '<td>' . $row['phone'] . '</td>';
                    echo '<td>' . $row['notes'] . '</td></tr>';
                    $companyfind = $row['id'];
                }

and the Javascript below:

var select_comp = new Array();  
var element = document.getElementById('return_comp');


function compTrig(clickedid) 
    { 
      if (document.getElementById(clickedid).checked == false) 
      {
        return false;
      } 
          else 
              {
                select_comp.push(clickedid);

            var clicked = clickedid;
            var box= confirm(select_comp);
            if (box==true)

                return true;
            else

               document.getElementById(clickedid).checked = false;
               var index = select_comp.indexOf(clicked);
                   if (index > -1) 
                    {
                    select_comp.splice(index, 1);           
                    }

            }
select_comp.toString();
element.value(select_comp);
}

I can get a confirmation box to display the correct values but i cannot change the value of the hidden value which would work. I was thinking that it may be because the form is in a while loop and the form should be outside of the loop. Any help of this matter will be greatly appreciated.

5
  • thanks Voodoo417 I have added the whole while loop above, the form is under //button to select companies Commented Feb 2, 2014 at 0:11
  • there are dozens of forms. is that correct? Commented Feb 2, 2014 at 0:12
  • Yeah that's correct, the others can be ignored they just change the background colour and update the database. //button to select companies is the one not changing the value using the javascript function. Was asked to show the whole while loop. Thanks Commented Feb 2, 2014 at 0:17
  • 1
    “I want to pass the items that are checked into a variable in PHP once submitted” – why would you need JavaScript for that? Simply submit the from, then you’ll get the values server-side … Commented Feb 2, 2014 at 0:19
  • How would I do that without javascript? Do I not need a value from the checked items Commented Feb 2, 2014 at 0:24

1 Answer 1

1

You're doing this the opposite way 'round how it should be done and exposing too much information to be tampered with in the front-end. It's also causing a lot of unnecessary redundancy (redundancy is the programmer's mortal enemy). No Javascript is needed for this task.

All you should be passing to the browser is the relevant company information to populate a single form; then building the appropriate output in PHP after submission.

search_results.php

echo "<form name=\"select_comp\" method=\"POST\" action=\"select_comp.php\">";

$search_sql = "
SELECT id FROM `company` WHERE `groups` = '$groups'
AND (`companyname` LIKE '%$search%'
OR `directurl` LIKE '%$search%'
OR `email` LIKE '%$search%'
OR `phone` LIKE '%$search%'
OR `groups` LIKE '%$search%'
OR `notes` LIKE '%$search%')"; 

$result = mysql_query($search_sql) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
    echo "<input type=\"checkbox\" name=\"id[{$row['id']}]\" value=\"1\"> {$row['company_name']}<br>";
    // echo other company details
}

echo '
<input type="submit" value="C">
</form>';

This will give you an array of selected checkboxes in the $_POST variable:

select_comp.php

// $_POST['id'] will contain an array of selected checkboxes
// implode array into a comma-separated list for use with MySQL IN operator
// escaping a string where the numbers have been tampered with will cause an error, but it will be safe from injection
$sql = "
SELECT * FROM `company` WHERE `id`
IN (" . mysql_real_escape_string(implode(',', $_POST['id'])) . ")";
$res = mysql_query($sql);
while($row = mysql_fetch_array($res)) {
        switch ($row['id']) {
                // first company
                case '0':
                        // build styling here
                break;

                // second company
                case '1':
                        // build more styling here
                break;

                // and so on
        }

        // output HTML with styling information computed above
}

But, what you should really be doing is setting the styling dynamically with values entered into the database and/or determined by file naming convention (while involving absolute validation of dynamically-generated styling specifications, of course).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks recovering Nerdaholic for your answer, I have now changed my script with a few functions that calculates differently that what I was achieving in this question. I agree my script is not best practice but it is only for myself to make use of. Appreciate the time spent and the correct way of completing this task and has helped me understand for future projects. Thank again

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.