PDA

View Full Version : show hide issue



ianhaney
01-21-2016, 09:42 PM
Hi

I have a show hide feature working using javascript but can't work out that if I click on one link and the content shows and then click another link to show another bit of content, I would like the first lot of content to hide so it only shows one lot of content at one time, below is my script



<script>
$(document).ready(function(){
$("#hideae").click(function(){
$("p#ae").hide();
});
$("#showae").click(function(){
$("p#ae").show();
});

$("#hidefj").click(function(){
$("p#fj").hide();
});
$("#showfj").click(function(){
$("p#fj").show();
});

$("#hideko").click(function(){
$("p#ko").hide();
});
$("#showko").click(function(){
$("p#ko").show();
});

$("#hidept").click(function(){
$("p#pt").hide();
});
$("#showpt").click(function(){
$("p#pt").show();
});

$("#hideuz").click(function(){
$("p#uz").hide();
});
$("#showuz").click(function(){
$("p#uz").show();
});
});
</script>

echo '<div style="color: #000000;">';
echo '<a id="hideae" class="xlink">Hide A-E</a>';
echo '&nbsp; - &nbsp;';
echo '<a id="showae" class="xlink">Show A-E</a>';
echo '</div>';

while($row= mysqli_fetch_assoc($query)){

//echo '<div class="newboxes" id="newboxes1"><label><input class="checkbox" type="checkbox" value="'.$row['customer_phone'].'" name="customer_phone[]" id="customer_phone"/> '.$row['customer_name'].'&nbsp;-&nbsp;'.$row['customer_phone'].'</label></div>';

echo '<p id="ae"><label><input class="checkbox" type="checkbox" value="'.$row['customer_phone'].'" name="customer_phone[]" id="customer_phone"/> '.$row['customer_name'].'&nbsp;-&nbsp;'.$row['customer_phone'].'</label></p>';

}

echo '<br>';

echo '<div style="clear: both;">';

$sqlCommand2 = "SELECT id, customer_name, customer_phone FROM repairs WHERE customer_phone LIKE '%44%' AND customer_name BETWEEN 'f' AND 'jz' GROUP BY customer_name";

$query = mysqli_query($db, $sqlCommand2);

echo '<div style="color: #000000;">';
echo '<a id="hidefj" class="xlink">Hide F-J</a>';
echo '&nbsp; - &nbsp;';
echo '<a id="showfj" class="xlink">Show F-J</a>';
echo '</div>';

while($row= mysqli_fetch_assoc($query)){

echo '<p id="fj"><label><input class="checkbox" type="checkbox" value="'.$row['customer_phone'].'" name="customer_phone[]" id="customer_phone"/> '.$row['customer_name'].'&nbsp;-&nbsp;'.$row['customer_phone'].'</label></p>';

}

echo '</div>';

echo '<br>';

echo '<div style="clear: both;">';

$sqlCommand3 = "SELECT id, customer_name, customer_phone FROM repairs WHERE customer_phone LIKE '%44%' AND customer_name BETWEEN 'k' AND 'oz' GROUP BY customer_name";

$query = mysqli_query($db, $sqlCommand3);

echo '<div style="color: #000000;">';
echo '<a id="hideko" class="xlink">Hide K-O</a>';
echo '&nbsp; - &nbsp;';
echo '<a id="showko" class="xlink">Show K-O</a>';
echo '</div>';

while($row= mysqli_fetch_assoc($query)){

echo '<p id="ko"><label><input class="checkbox" type="checkbox" value="'.$row['customer_phone'].'" name="customer_phone[]" id="customer_phone"/> '.$row['customer_name'].'&nbsp;-&nbsp;'.$row['customer_phone'].'</label></p>';

}

echo '</div>';

echo '<br>';

echo '<div style="clear: both;">';

$sqlCommand4 = "SELECT id, customer_name, customer_phone FROM repairs WHERE customer_phone LIKE '%44%' AND customer_name BETWEEN 'p' AND 'tz' GROUP BY customer_name";

$query = mysqli_query($db, $sqlCommand4);

echo '<div style="color: #000000;">';
echo '<a id="hidept" class="xlink">Hide P-T</a>';
echo '&nbsp; - &nbsp;';
echo '<a id="showpt" class="xlink">Show P-T</a>';
echo '</div>';

while($row= mysqli_fetch_assoc($query)){

echo '<p id="pt"><label><input class="checkbox" type="checkbox" value="'.$row['customer_phone'].'" name="customer_phone[]" id="customer_phone"/> '.$row['customer_name'].'&nbsp;-&nbsp;'.$row['customer_phone'].'</label></p>';

}

echo '</div>';

echo '<br>';

echo '<div style="clear: both;">';

$sqlCommand5 = "SELECT id, customer_name, customer_phone FROM repairs WHERE customer_phone LIKE '%44%' AND customer_name BETWEEN 'u' AND 'zz' GROUP BY customer_name";

$query = mysqli_query($db, $sqlCommand5);

echo '<div style="color: #000000;">';
echo '<a id="hideuz" class="xlink">Hide U-Z</a>';
echo '&nbsp; - &nbsp;';
echo '<a id="showuz" class="xlink">Show U-Z</a>';
echo '</div>';

while($row= mysqli_fetch_assoc($query)){

echo '<p id="uz"><label><input class="checkbox" type="checkbox" value="'.$row['customer_phone'].'" name="customer_phone[]" id="customer_phone"/> '.$row['customer_name'].'&nbsp;-&nbsp;'.$row['customer_phone'].'</label></p>';

}

echo '</div>';


Thank you in advance

Ian

styxlawyer
01-22-2016, 02:32 PM
It's a bit clumsy, but you have no way of knowing which content is currently displayed so you need to hide all of the other content.



<script>
$(document).ready(function(){
$("#hideae").click(function(){
$("p#ae").hide();
});
$("#showae").click(function(){
$("p#ae").show();
$("p#fj").hide();
$("p#ko").hide();
$("p#pt").hide();
$("p#uz").hide();
});

$("#hidefj").click(function(){
$("p#fj").hide();
});
$("#showfj").click(function(){
$("p#fj").show();
$("p#ae").hide();
$("p#ko").hide();
$("p#pt").hide();
$("p#uz").hide();
});

$("#hideko").click(function(){
$("p#ko").hide();
});
$("#showko").click(function(){
$("p#ko").show();
$("p#ae").hide();
$("p#fj").hide();
$("p#pt").hide();
$("p#uz").hide();
});

$("#hidept").click(function(){
$("p#pt").hide();
});
$("#showpt").click(function(){
$("p#pt").show();
$("p#ae").hide();
$("p#fj").hide();
$("p#ko").hide();
$("p#uz").hide();
});

$("#hideuz").click(function(){
$("p#uz").hide();
});
$("#showuz").click(function(){
$("p#uz").show();
$("p#ae").hide();
$("p#fj").hide();
$("p#ko").hide();
$("p#pt").hide();
});
});
</script>



If you created an array of content names and kept a pointer to the currently displayed content you would only need to hide that content.

ianhaney
01-22-2016, 02:44 PM
Thank you so much, that is perfect

I have another issue that I can't seem to work out why it is doing it

If I select a customer checkbox in say the F-J group and I click show any of the other groups, it checks the first name in the group A-E so it keeps sending them a sms message as well, is it possible to check the coding and see what can be causing the issue of when I click another show link that it checks the checkbox for the first name in the Show A-E group, I can't work out why it is doing it

styxlawyer
01-22-2016, 04:20 PM
Without seeing your page I have no idea what you mean.

Here's an improved version of my previous code:



<script>
function clearCurrentContent() {
$("p#ae").hide();
$("p#fj").hide();
$("p#ko").hide();
$("p#pt").hide();
$("p#uz").hide();
}

$(document).ready(function(){
$("#hideae").click(function(){
$("p#ae").hide();
});
$("#showae").click(function(){
clearCurrentContent();
$("p#ae").show();
});

$("#hidefj").click(function(){
clearCurrentContent();
$("p#fj").hide();
});
$("#showfj").click(function(){
clearCurrentContent();
$("p#fj").show();
});

$("#hideko").click(function(){
$("p#ko").hide();
});
$("#showko").click(function(){
clearCurrentContent();
$("p#ko").show();
});

$("#hidept").click(function(){
$("p#pt").hide();
});
$("#showpt").click(function(){
clearCurrentContent();
$("p#pt").show();
});

$("#hideuz").click(function(){
$("p#uz").hide();
});
$("#showuz").click(function(){
clearCurrentContent();
$("p#uz").show();
});
});
</script>

ianhaney
01-22-2016, 04:47 PM
Thank you the above code works perfect

ahh ok the page is on our admin side but I can send a screenshot of the issue if it helps

ianhaney
01-22-2016, 04:53 PM
I have attached a screenshot of the issue, what is happening is if I click on show A-E it checks the checkbox for Bernie and then if I click Show F-J link it unchecks the checkbox for Bernie, it only happens for the first name though, it don't happen to any of the other checkboxes

so cause of the issue it sends a sms to Bernie as well as the actual person I only want to send the sms to, I thought it would be cause of a tag not closed but all the a tags and div tags etc are closed5800

styxlawyer
01-22-2016, 05:07 PM
I suspect that SMS problem is more to do with the fact that the jQuery ".hide" method only hides the appropriate section (similar to CSS "display:none;"). You should either add a callback function to the ".hide" call or add some code to the "clearCurrentContent()" function to clear all the checkboxes after hiding the section.

See here for more info:

http://www.w3schools.com/jquery/eff_hide.asp

ianhaney
01-22-2016, 06:43 PM
I'm not sure if clearCurrentContent() would work cause don't want to clear the checkboxes after hiding a section as that will uncheck any boxes I check myself that I want to send a sms to, I just need a way so that the very first checkbox is not checked every time I click show on any of the other groups

ianhaney
01-22-2016, 06:46 PM
Sorry has gone over my head

ianhaney
01-22-2016, 07:05 PM
I really don't get it, I wonder if it is a php issue that for some reason is sending the sms to the very first checkbox as well and not a javascript issue but am not sure, I am just taking a guess

styxlawyer
01-22-2016, 08:33 PM
I'm not sure if clearCurrentContent() would work cause don't want to clear the checkboxes after hiding a section as that will uncheck any boxes I check myself that I want to send a sms to, I just need a way so that the very first checkbox is not checked every time I click show on any of the other groups

No, you're right. I didn't fully understand the way you are using the page.


I really don't get it, I wonder if it is a php issue that for some reason is sending the sms to the very first checkbox as well and not a javascript issue but am not sure, I am just taking a guess

Is your jQuery script inside the <?php ... ?> tags in your page? If so, I wonder if there might be a problem with the $ sign which indicates the start of a variable name in PHP. You could try substituting the long name "jQuery" for all the "$" shorthand inside the <script> ... </script> tags, or simply move the whole script outside the <?php ... ?> tags.

ianhaney
01-22-2016, 08:54 PM
the only part inside the php tags in the links themselves as per below



echo '<div style="color: #000000;">';
echo '<a id="hideae" class="xlink">Hide A-E</a>';
echo '&nbsp; - &nbsp;';
echo '<a id="showae" class="xlink">Show A-E</a>';
echo '</div>';


The jquery script is betwen the <head> and </head> tags, could that be why?

I just thought you mentioning the $ sign, I wonder if it is coflicting with my check user login script as has a $ in it as per below



<?
session_start();

if($_SESSION['user']==''){
header("Location:index.php");
}else{
include("config.php");
$sql=$dbh->prepare("SELECT * FROM users WHERE id=?");
$sql->execute(array($_SESSION['user']));
while($r=$sql->fetch()){
echo "<div class='home-content'>";
echo "<center><h2>Hello, ".$r['username']."</h2>";
echo "<a href='logout.php'>Log Out</a></center>";
echo "</div>";
echo "<br>";
}
}
?>

ianhaney
01-22-2016, 08:58 PM
I changed all the jquery $ signs to jquery but now the jquery hide/show links are not working, have I done it right?



<script>
function clearCurrentContent() {
jquery("p#ae").hide();
jquery("p#fj").hide();
jquery("p#ko").hide();
jquery("p#pt").hide();
jquery("p#uz").hide();
}

jquery(document).ready(function(){
jquery("#hideae").click(function(){
jquery("p#ae").hide();
});
jquery("#showae").click(function(){
clearCurrentContent();
jquery("p#ae").show();
});

jquery("#hidefj").click(function(){
clearCurrentContent();
jquery("p#fj").hide();
});
jquery("#showfj").click(function(){
clearCurrentContent();
jquery("p#fj").show();
});

jquery("#hideko").click(function(){
jquery("p#ko").hide();
});
jquery("#showko").click(function(){
clearCurrentContent();
jquery("p#ko").show();
});

jquery("#hidept").click(function(){
jquery("p#pt").hide();
});
jquery("#showpt").click(function(){
clearCurrentContent();
jquery("p#pt").show();
});

jquery("#hideuz").click(function(){
jquery("p#uz").hide();
});
jquery("#showuz").click(function(){
clearCurrentContent();
jquery("p#uz").show();
});

});
</script>

styxlawyer
01-22-2016, 09:23 PM
No, it's case sensitive and should be "jQuery". I think there's also a problem with your PHP code here:



echo '<p id="ae"><label><input class="checkbox" type="checkbox" value="'.$row['customer_phone'].'" name="customer_phone[]" id="customer_phone"/> '.$row['customer_name'].'&nbsp;-&nbsp;'.$row['customer_phone'].'</label></p>';


It appears to be serving the same "id" for all the customer phone numbers. All "id" attributes in any HTML page should be unique.

ianhaney
01-22-2016, 09:33 PM
sussed it, I changed to jQuery and also noticed I did not have a closed <label> tag and with them two changes it is working perfect now and only sending the sms messages to the checkboxes that I select

Thank you so much for all your help, I really appreciate it

styxlawyer
01-22-2016, 11:13 PM
You're welcome. Did you fix the repeated "id" problem? Assuming that all your customer phone numbers are different, you could change this code:



echo '<p id="ae"><label><input class="checkbox" type="checkbox" value="'.$row['customer_phone'].'" name="customer_phone[]" id="customer_phone"/> '.$row['customer_name'].'&nbsp;-&nbsp;'.$row['customer_phone'].'</label></p>';

to this:



echo '<p id="ae"><label><input class="checkbox" type="checkbox" value="'.$row['customer_phone'].'" name="customer_phone[]" id="phone'.$row['customer_phone'].'"/> '.$row['customer_name'].'&nbsp;-&nbsp;'.$row['customer_phone'].'</label></p>';

which should make all the "id" attributes unique.

It's not always easy to debug PHP generated pages, but using the W3.org validator may help. Just save the HTML source of the served page in a file and upload it here:

https://validator.w3.org

ianhaney
01-23-2016, 12:32 AM
Oops not yet I'll do that first thing in the morning as is midnight here I'm good old uk

Will I need to change anything in the Php form processing script if I change the id part so is unique?

styxlawyer
01-23-2016, 01:42 PM
Oops not yet I'll do that first thing in the morning as is midnight here I'm good old uk

Will I need to change anything in the Php form processing script if I change the id part so is unique?

I'm in the UK too! Sorry about the delay in replying but I was rowing this morning.

I haven't seen your PHP form processing script so I don't know if there is anything which will fall over if you change the id of each entry. Every id in an HTML page must be unique so if your PHP script uses the ids in the current page it will probably fail. Also any JavaScript or jQuery calls which select elements by id will fail. If you're not using the id in either PHP or jQuery there won't be a problem, the page will simply fail validation.

If you're not using the id in any of the scripts, then it's redundant and can be deleted.

Beverleyh
01-23-2016, 01:50 PM
I think you'll be using the name attribute as a field identifier in PHP processing so you should be good, but to be sure, yes a copy of the processing script would be helpful.

ianhaney
01-23-2016, 07:01 PM
Hi

So sorry for my late reply, belwo is the script from the page with the form and the script within the php page that processes the form



<?php

$db = mysqli_connect("" , "", "") or die("Check connection parameters!");
// Optionally skip select_db and use: mysqli_connect(host,user,pass,dbname)
mysqli_select_db($db,"") or die(mysqli_error($db));

if (mysqli_connect_error()) {
die ('Failed to connect to MySQL');

}

echo '<div style="width: 400px; float: left;">';
echo 'CHOOSE THE CUSTOMER BELOW TO SEND A SMS TO NOTIFY OF THEIR COMPLETED PC/LAPTOP REPAIR OR TO SEND MARKETING SMS TEXTS.';
echo '<br><br>';
echo '<form method="post" action="send-sms-complete-repairs.php">';
echo '<select name="from">';
echo '<option value="">From</option>';
echo '<option value="IT&nbsp;Done&nbsp;Right">IT Done Right</option>';
echo '</select>';

echo '<br><br>';

echo '<label><input type="text" name="manual_phone"></label>';

$sqlCommand = "SELECT id, customer_name, customer_phone FROM repairs WHERE customer_phone LIKE '%44%' AND customer_name BETWEEN 'a' AND 'ez' GROUP BY customer_name";

$query = mysqli_query($db, $sqlCommand);

echo '<br><br>';

echo '<div style="color: #000000;">';
echo '<a id="hideae" class="xlink">Hide A-E</a>';
echo '&nbsp; - &nbsp;';
echo '<a id="showae" class="xlink">Show A-E</a>';
echo '</div>';

while($row= mysqli_fetch_assoc($query)){

echo '<p id="ae"><label><input class="checkbox" type="checkbox" value="'.$row['customer_phone'].'" name="customer_phone[]" id="customer_phone"/> '.$row['customer_name'].'&nbsp;-&nbsp;'.$row['customer_phone'].'</label></p>';

}

echo '<br>';

echo '<div style="clear: both;">';

$sqlCommand2 = "SELECT id, customer_name, customer_phone FROM repairs WHERE customer_phone LIKE '%44%' AND customer_name BETWEEN 'f' AND 'jz' GROUP BY customer_name";

$query = mysqli_query($db, $sqlCommand2);

echo '<div style="color: #000000;">';
echo '<a id="hidefj" class="xlink">Hide F-J</a>';
echo '&nbsp; - &nbsp;';
echo '<a id="showfj" class="xlink">Show F-J</a>';
echo '</div>';

while($row= mysqli_fetch_assoc($query)){

echo '<p id="fj"><label><input class="checkbox" type="checkbox" value="'.$row['customer_phone'].'" name="customer_phone[]" id="customer_phone"/> '.$row['customer_name'].'&nbsp;-&nbsp;'.$row['customer_phone'].'</label></p>';

}

echo '</div>';

echo '<br>';

echo '<div style="clear: both;">';

$sqlCommand3 = "SELECT id, customer_name, customer_phone FROM repairs WHERE customer_phone LIKE '%44%' AND customer_name BETWEEN 'k' AND 'oz' GROUP BY customer_name";

$query = mysqli_query($db, $sqlCommand3);

echo '<div style="color: #000000;">';
echo '<a id="hideko" class="xlink">Hide K-O</a>';
echo '&nbsp; - &nbsp;';
echo '<a id="showko" class="xlink">Show K-O</a>';
echo '</div>';

while($row= mysqli_fetch_assoc($query)){

echo '<p id="ko"><label><input class="checkbox" type="checkbox" value="'.$row['customer_phone'].'" name="customer_phone[]" id="customer_phone"/> '.$row['customer_name'].'&nbsp;-&nbsp;'.$row['customer_phone'].'</label></p>';

}

echo '</div>';

echo '<br>';

echo '<div style="clear: both;">';

$sqlCommand4 = "SELECT id, customer_name, customer_phone FROM repairs WHERE customer_phone LIKE '%44%' AND customer_name BETWEEN 'p' AND 'tz' GROUP BY customer_name";

$query = mysqli_query($db, $sqlCommand4);

echo '<div style="color: #000000;">';
echo '<a id="hidept" class="xlink">Hide P-T</a>';
echo '&nbsp; - &nbsp;';
echo '<a id="showpt" class="xlink">Show P-T</a>';
echo '</div>';

while($row= mysqli_fetch_assoc($query)){

echo '<p id="pt"><label><input class="checkbox" type="checkbox" value="'.$row['customer_phone'].'" name="customer_phone[]" id="customer_phone"/> '.$row['customer_name'].'&nbsp;-&nbsp;'.$row['customer_phone'].'</label></p>';

}

echo '</div>';

echo '<br>';

echo '<div style="clear: both;">';

$sqlCommand5 = "SELECT id, customer_name, customer_phone FROM repairs WHERE customer_phone LIKE '%44%' AND customer_name BETWEEN 'u' AND 'zz' GROUP BY customer_name";

$query = mysqli_query($db, $sqlCommand5);

echo '<div style="color: #000000;">';
echo '<a id="hideuz" class="xlink">Hide U-Z</a>';
echo '&nbsp; - &nbsp;';
echo '<a id="showuz" class="xlink">Show U-Z</a>';
echo '</div>';

while($row= mysqli_fetch_assoc($query)){

echo '<p id="uz"><label><input class="checkbox" type="checkbox" value="'.$row['customer_phone'].'" name="customer_phone[]" id="customer_phone"/> '.$row['customer_name'].'&nbsp;-&nbsp;'.$row['customer_phone'].'</label></p>';

}

echo '</div>';

/*echo '<select name="customer_phone" id="id">';
echo '<option value="">Choose the mobile number</option>';
while($row= mysqli_fetch_assoc($query)){

echo '<option value="'.$row['customer_phone'].'">'.$row['customer_name'].'&nbsp;-&nbsp;'.$row['customer_phone'].'</option>';

}

echo '</select>';*/

echo '<br><br>';

echo '<label style="clear: both;">SMS Message</label>';
echo '<br>';
echo '<textarea name="smsmessage"></textarea>';

echo '<input type="submit" name="submit" value="Send SMS">';

echo '</form>';
echo '</div>';
?>




<?php

function sendSMS($username, $password, $customer_phone, $message, $originator) {
$URL = 'http://api.textmarketer.co.uk/gateway/'."?username=$username&password=$password&option=xml";
$URL .= "&to=$customer_phone&message=".urlencode($message).'&orig='.urlencode($originator);
$fp = fopen($URL, 'r');

return fread($fp, 1024);
}

$db = mysqli_connect("" , "", "") or die("Check connection parameters!");
// Optionally skip select_db and use: mysqli_connect(host,user,pass,dbname)
mysqli_select_db($db,"") or die(mysqli_error($db));

if (mysqli_connect_error()) {
die ('Failed to connect to MySQL');
}

$id = $db->real_escape_string($_POST['id']);

$sqlCommand = "SELECT id, customer_name, customer_phone FROM repairs WHERE id = '$id'";

$query = mysqli_query($db, $sqlCommand);

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

$from = '';
$manual_phone = $db->real_escape_string($_POST['manual_phone']);
$customer_phone = $db->real_escape_string($_POST['customer_phone']);
$smsmessage = $db->real_escape_string($_POST['smsmessage']);

foreach ($_POST['customer_phone'] as $customer_phone) {
// Example of use
$response = sendSMS('', '', $customer_phone, $smsmessage, $from);
}
if (isset($manual_phone)) {
sendSMS('', '', $manual_phone, $smsmessage, $from);
}

//var_dump($response);
echo "<div style='float: left;'>";
echo "SMS Sent Successfully";
} else {
echo "SMS Failed to Send";
}

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

$customer_phone = $db->real_escape_string($_POST['customer_phone']);
$smsmessage = $db->real_escape_string($_POST['smsmessage']);

foreach ($_POST['customer_phone'] as $customer_phone) {
$smsquery = "INSERT INTO sentsms (customer_phone, smsmessage) VALUES ('" . $customer_phone . "', '" . $smsmessage . "')";

$result = mysqli_query($db, $smsquery);
echo "<br><br>";
echo "Added to Database";
}
} else {
echo "Failed to add to database";
echo "</div>";
}
?>

styxlawyer
01-23-2016, 07:52 PM
In the first listing, you haven't included the jQuery or JavaScript code which makes the Ajax request to the server. That's the important bit, as we need to know whether or not the ids (which are all set to "customer_phone") are used in building the Ajax request string. My guess would be that they are not, as you say the page is working, so the small section of code which creates multiple identical ids can probably be deleted from the first listing.

One general comment. I'm not sure why you are sending the customers' phone numbers to the display page and then sending them back again to the processing page. I would have thought that just the customer id and name from the database would be sufficient information to display on the first page. The form processing code can retrieve the relevant information from the database directly.

ianhaney
01-23-2016, 07:59 PM
Ahh so sorry, I have added below the jquery/javascript code that makes the ajax request to the server, so sorry



<script>

function clearCurrentContent() {
jQuery("p#ae").hide();
jQuery("p#fj").hide();
jQuery("p#ko").hide();
jQuery("p#pt").hide();
jQuery("p#uz").hide();
}

jQuery(document).ready(function(){
jQuery("#hideae").click(function(){
jQuery("p#ae").hide();
});
jQuery("#showae").click(function(){
clearCurrentContent();
jQuery("p#ae").show();
});

jQuery("#hidefj").click(function(){
clearCurrentContent();
jQuery("p#fj").hide();
});
jQuery("#showfj").click(function(){
clearCurrentContent();
jQuery("p#fj").show();
});

jQuery("#hideko").click(function(){
jQuery("p#ko").hide();
});
jQuery("#showko").click(function(){
clearCurrentContent();
jQuery("p#ko").show();
});

jQuery("#hidept").click(function(){
jQuery("p#pt").hide();
});
jQuery("#showpt").click(function(){
clearCurrentContent();
jQuery("p#pt").show();
});

jQuery("#hideuz").click(function(){
jQuery("p#uz").hide();
});
jQuery("#showuz").click(function(){
clearCurrentContent();
jQuery("p#uz").show();
});

});
</script>


I am not sure to be honest regarding the customers phone numbers to the display page and then sending them back again, I was just trying to get it working

styxlawyer
01-23-2016, 08:35 PM
Sorry, no JavaScript required, I've just looked more closely and figured out how the first page works. I also see that all the <p> tags within a name group have identical ids ("ae", "fj" etc) but I don't think you need those either.

Leave it with me for a while.

styxlawyer
01-24-2016, 09:18 AM
Every paragraph in each block had the same id, so I have removed the paragraph ids and replaced them with a surrounding <div>. I've also edited the Javascript to reflect that change and shuffled the buttons around a bit. As I didn't have access to your database I've commented out all the SQL related stuff, but it should be easy to restore.

In the SMS sending file I have commented out most of your existing code and added a few lines to show what has been received from the Form2.php page. It should be very easy to make it all work now.

The files are too long to insert so are in the attached zip file.

Hope that helps.


5805

ianhaney
01-24-2016, 11:23 AM
Hi

Sorry fot my late reply

Thank you so much, really appreciate it

styxlawyer
01-24-2016, 11:51 AM
You're welcome. Let me know if there's anything which doesn't work as you expect.

ianhaney
01-24-2016, 12:32 PM
Thank you so much, will do

So just to confirm I just need to uncomment parts of the code for the db related part and also for the sms sending function?

Is that right?

styxlawyer
01-24-2016, 12:53 PM
Yes. I have used the "ID" and user name fields from your database and created blocks of PHP code. Once you have uncommented that code it should read the database and create paragraphs identical to the ones in the file (which you need to delete). Look at the source of the served page (CTRL + U on your browser) and checkbox paragraphs should look similar to those on the current page.

You will have to edit the second file to expect the "ID" back for each checkbox in the form rather than the customer phone number. Then you will have to extract the appropriate phone number from the database before sending the sms.

ianhaney
01-24-2016, 01:34 PM
Sorry has gone bit over my head about needing to delete ones in the file and editing the second file to expect the ID back for each checkbox rather than the customer phone number

styxlawyer
01-24-2016, 02:35 PM
The paragraphs in the first file which look like this:



<p><label><input class="checkbox" type="checkbox" name="cust_id[]" value="1"/>Alan</label></p>
.
.
<p><label><input class="checkbox" type="checkbox" name="cust_id[]" value="4"/>David</label></p>


... are just dummy data for testing and will have to be deleted. They should be replaced when you have uncommented the PHP database code and run the PHP file with your own database. Make sure that happens.

Then you need to look at the PHP code in the second file and make sure it's going to extract the correct phone number from the database.

The point of using the customer id from the database is that it's the only field which is guaranteed to be unique. Phone number could be duplicated if you have more than one repair from the same customer.

ianhaney
01-24-2016, 02:37 PM
Ahh ok, I'll see what I can do with it and hopefully get it right and post a update if I get it right or need help, that ok?

styxlawyer
01-24-2016, 02:39 PM
Yes, that's fine.

styxlawyer
01-25-2016, 07:38 AM
I think these two files should do what you are trying to achieve. You just need to edit the sendSms() function in the second one.

form.php



<?php

$dbConn = mysqli_connect("localhost" , "root", "", "repairs_test") or die("Check connection parameters!");

function retrieveData($db, $command) {
$r = mysqli_query($db, $command);
if($r) {
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){
echo '<p><label><input class="checkbox" type="checkbox" name="cust_id[]" value="'.$row['id'].'"/>'.$row['customer_name'].'</label></p>';
}
mysqli_free_result($r);
} else {
echo '<p>'.mysqli_error($db). '<br /><br />Query: '.$command.'</p>';
}
}
?>

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>

<script type="text/javascript" src="jquery.min.js"></script>

<script type="text/javascript">

function clearCurrentContent() {
jQuery("div#ae").hide();
jQuery("div#fj").hide();
jQuery("div#ko").hide();
jQuery("div#pt").hide();
jQuery("div#uz").hide();
}

jQuery(document).ready(function(){

jQuery("#hideall").click(function(){
clearCurrentContent();
});

jQuery("#showae").click(function(){
clearCurrentContent();
jQuery("div#ae").show();
});

jQuery("#showfj").click(function(){
clearCurrentContent();
jQuery("div#fj").show();
});

jQuery("#showko").click(function(){
clearCurrentContent();
jQuery("div#ko").show();
});

jQuery("#showpt").click(function(){
clearCurrentContent();
jQuery("div#pt").show();
});

jQuery("#showuz").click(function(){
clearCurrentContent();
jQuery("div#uz").show();
});

});
</script>

</head>
<body>

<div style="width: 400px; float: left;">
CHOOSE THE CUSTOMER BELOW TO SEND A SMS TO NOTIFY OF THEIR COMPLETED PC/LAPTOP REPAIR OR TO SEND MARKETING SMS TEXTS.
<br><br>
<form method="post" action="send-sms-complete-repairs.php">
<select name="from">
<option value="Default">From</option>
<option value="IT&nbsp;Done&nbsp;Good">IT Done Good</option>
</select>

<p><label><input type="text" name="manual_phone">Manual phone: </label></p>

<div style="display:table;color: #000000;margin:1em auto 0 auto;">
<a id="showae" class="xlink">Show A-E</a>&nbsp;-&nbsp;
<a id="showfj" class="xlink">Show F-J</a>&nbsp;-&nbsp;
<a id="showko" class="xlink">Show K-O</a>&nbsp;-&nbsp;
<a id="showpt" class="xlink">Show P-T</a>&nbsp;-&nbsp;
<a id="showuz" class="xlink">Show U-Z</a>
</div>

<div style="display:table;color: #ff0000;margin:1em auto 0 auto;">
<a id="hideall" class="xlink">Hide All</a>
</div>

<div style="display:block;color: #000000;margin-top:2em;">
<div id="ae" style="">

<?php
$sqlCommand = "SELECT id, customer_name FROM repairs WHERE customer_phone LIKE '%44%' AND customer_name BETWEEN 'a' AND 'ez' GROUP BY customer_name";

retrieveData($dbConn, $sqlCommand);

?>
</div>

<div id="fj" style="display:none;">

<?php
$sqlCommand = "SELECT id, customer_name FROM repairs WHERE customer_phone LIKE '%44%' AND customer_name BETWEEN 'f' AND 'jz' GROUP BY customer_name";

retrieveData($dbConn, $sqlCommand);

?>
</div>

<div id="ko" style="display:none;">

<?php
$sqlCommand = "SELECT id, customer_name FROM repairs WHERE customer_phone LIKE '%44%' AND customer_name BETWEEN 'k' AND 'oz' GROUP BY customer_name";

retrieveData($dbConn, $sqlCommand);

?>
</div>

<div id="pt" style="display:none;">

<?php
$sqlCommand = "SELECT id, customer_name FROM repairs WHERE customer_phone LIKE '%44%' AND customer_name BETWEEN 'p' AND 'tz' GROUP BY customer_name";

retrieveData($dbConn, $sqlCommand);

?>
</div>

<div id="uz" style="display:none;">

<?php
$sqlCommand = "SELECT id, customer_name FROM repairs WHERE customer_phone LIKE '%44%' AND customer_name BETWEEN 'u' AND 'zz' GROUP BY customer_name";

retrieveData($dbConn, $sqlCommand);

?>
</div>
</div>

<label style="clear: both;">SMS Message</label>
<textarea name="smsmessage" style="margin-top:1em;"></textarea>
<input type="submit" name="submit" value="Send SMS" />
</form>
</div>

</body>
</html>



send-sms-complete-repairs.php



<?php

$dbConn = mysqli_connect("localhost" , "root", "", "repairs_test") or die("Check connection parameters!");

/*
function sendSMS($username, $password, $customer_phone, $message, $originator, $customer_name = 'Nobody') {
$URL = 'http://api.textmarketer.co.uk/gateway/'."?username=$username&password=$password&option=xml";
$URL .= "&to=$customer_phone&message=".urlencode($message).'&orig='.urlencode($originator);
$fp = fopen($URL, 'r');

return fread($fp, 1024);
}
*/

function sendSMS($username, $password, $phone, $message, $originator) {
echo '<p>On phone number: '.$phone.'</p><p>From: '.$originator.'</p>';
echo '<p>'.$message.'</p>';
return true;
}

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

$from = $dbConn->real_escape_string($_POST['from']);
$manual_phone = $dbConn->real_escape_string($_POST['manual_phone']);
$smsmessage = $dbConn->real_escape_string($_POST['smsmessage']);

$cust_id = $_POST['cust_id']; // Clean up the array.
$y = 0;
foreach($cust_id as $x) {
$cust_id[$y] = $dbConn->real_escape_string($x);
$y++;
}

if ($manual_phone != "") {
$response = sendSMS('', '', $manual_phone, $smsmessage, $from);

} else {
foreach ($cust_id as $id) {
$sqlCommand1 = "SELECT id, customer_name, customer_phone FROM repairs WHERE id = '$id'";
$r1 = mysqli_query($dbConn, $sqlCommand1);
if($r1) {
$row = mysqli_fetch_array($r1, MYSQLI_ASSOC);
mysqli_free_result($r1);
echo '<p>The following message was sent to: '.$row['customer_name'].'</p>';
$response = sendSMS('', '', $row['customer_phone'], $smsmessage, $from);
if($response) {
echo '<p>SMS Sent Successfully</p>';

$sqlCommand2 = "INSERT INTO sentsms (customer_phone, smsmessage) VALUES ('" . $row['customer_phone'] . "', '" . $smsmessage . "')";
$r2 = mysqli_query($dbConn, $sqlCommand2);
if($r2) {
echo '<p>SMS message added to database.</p>';
} else {
echo '<p style="color:red;">FAILED to add SMS message to database.</p>';
}

} else {
echo '<p>SMS Failed to Send</p>';
}
} else {
echo '<p>'.mysqli_error($dbConn). '<br />Query: '.$sqlCommand1.'</p>';
}
echo '<p>======================================</p>';
}
}
} // end of 'if (isset($_POST["submit"]))'.

?>

ianhaney
01-25-2016, 09:23 AM
Thank you so much, will have a go at that today within the next 2 hours

Thank you so much, can you help me please with my other issue, I made a new post yesterday

styxlawyer
01-25-2016, 11:22 AM
I'll have a look. BTW you'll also have to change the database connection in the first line of code in each of those files.

ianhaney
01-25-2016, 11:55 AM
Thank you I'll remember to change the db credentials as well, the other post is titled js progress bar issue

I think that issue is to do with a js file as stated in the post, will see what I mean in the post