Jump to content

Recommended Posts

Hi

 

I have a form with a select menu that is populated by data pulled in from the db and I select a mobile number that I want to send a sms text to but it don't seem to be working, I am using text marketer and their api script. I can't seem to work out why the sms is not being sent, I am not getting any script errors on either file, below is the send-sms-message.php script that has the form in

<?php

ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);

$db = mysqli_connect("localhost" , "", "") 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');
}

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

$query = mysqli_query($db, $sqlCommand) or die (mysqli_error($db));

echo '<form action="send-sms.php" method="post">';
echo '<select>';
echo '<option value="">Choose the mobile number</option>';
while($row= mysqli_fetch_assoc($query)){
echo '<option value="'.$row['customer_name'].$row['customer_phone'].'">'.$row['customer_name'].' - '.$row['customer_phone'].'</option>';
}
echo '</select>';

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

?>

Could it be missing a name on select tag so would be <select name="namehere">, sorry just trying to work it out and had that thought pop in my head

 

below is the script from the form action script send-sms.php

<?php

ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);

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

$db = mysqli_connect("localhost" , "", "") 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');
}

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

$query = mysqli_query($db, $sqlCommand) or die (mysqli_error($db));

//fetch the data from the database 
while ($row = mysqli_fetch_array($query)) {

$mobnumber = $row['customer_phone'];
$country_code = '44';

$mobnumber = str_replace(' ', '', $row['customer_phone']);
$mobnumber = substr_replace($mobnumber, ''.$country_code, 0, ($mobnumber[0] == '0'));

//var_dump($mobnumber);

}

$message = "Name:".$row['customer_name'] . ' ' . 'Your PC/Laptop is ready for collection';
    
    // Example of use
$response = sendSMS('DJkGc7', '97q84F', $mobnumber, "Your PC/Laptop is ready for collection", 'ITDoneRight');
        var_dump($response);
?>
Link to comment
https://forums.phpfreaks.com/topic/300256-send-sms-scripts-issue/
Share on other sites

Sorry, if I hard code the mobile number in as per the script below,  the sms works and comes through but need it to send the sms to the mobile number I select from the select menu on the send-sms-message.php page

 



$response = sendSMS('DJkGc7', '97q84F', '447538503276', "Your PC/Laptop is ready for collection", 'ITDoneRight');


 

Can anyone help me please as am really stuck with it

UPDATE:

 

Hi, sorry got a update, it seems to be working now but I think it is looping cause of the while code in the send-sms-message.php and send-sms.php scripts and sending to all numbers and not just the one I choose within the select menu, is it possible just to check the code please

 

below is the send-sms-message.php script

 



<?php

$db = mysqli_connect("localhost" , "", "") 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');
}

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

$query = mysqli_query($db, $sqlCommand) or die (mysqli_error($db));

echo '<form action="send-sms.php" method="post">';
echo '<select name="customer_phone">';
echo '<option value="">Choose the mobile number</option>';
while($row= mysqli_fetch_assoc($query)){

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

}
echo '</select>';

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

?>


below is the send-sms.php script

 



<?php

ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);

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("localhost" , "", "") 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');
}

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

$query = mysqli_query($db, $sqlCommand) or die (mysqli_error($db));

//fetch the data from the database
while ($row = mysqli_fetch_array($query)) {

$customer_phone = $row['customer_phone'];

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

//var_dump($customer_phone);

}

// Example of use
$response = sendSMS('DJkGc7', '97q84F', $customer_phone, 'Your PC/Laptop is ready for collection', 'ITDoneRight');
//var_dump($response);
}
?>

your code doesn't have anything in it to just send to one selected phone number. you are querying for and looping over all the `repairs` db table data in the second piece of code.

 

1) your select/option menu should use the id as the value that will be submitted. not the customer phone number. this will make it easier to validate the submitted data.

 

2) you would use the submitted id to query for the actual phone number, then retrieve just that single row to get the phone number.

 

3) why are you even looping in the second piece of code?

 

4) do you have any security in place to control who can access either of the two pages you have shown? as it stands now, anyone who can visit these pages can cause a message to be sent to any phone number stored in your data.

 

are you even looking at your code? the computer can only do what the code you write tells it to do. if you are querying for the wrong data, in this case all the data, and looping over data, when you should be fetching at most one row, that's on you.

Edited by mac_gyver

Hi

 

Thank you for the reply, I have added a WHERE clause in now and listened to the suggesstions you provided and changed the select option value to id instead of customer_phone

 

So in send-sms.php I don't need the while line of code in there, is that right?

 

I will be putting security in once is all working to only admin staff can login, actually I will put that in now

I am getting a undefined variable: smsid error on my page

 

below is the script from my send-sms-message.php

 



<?
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>";
}
}
?>

<?php include("nav-menu.php"); ?>

<?php

$db = mysqli_connect("localhost" , "", "") 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');
}

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

$query = mysqli_query($db, $sqlCommand) or die (mysqli_error($db));

echo '<form action="send-sms.php" method="post">';
echo '<select name="customer_phone">';
echo '<option value="">Choose the mobile number</option>';
while($row= mysqli_fetch_assoc($query)){

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

}
echo '</select>';

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

?>


 

I made it $smsid as $id is already being used by the checking of the user being logged in or not or can it be used as is a different db table as thought using $id twice might confuse the script if used in different db tables

I have a update

 

I am not getting any errors now but no data is in the select option menu, below is my send-sms-message.php script

 



<?php

$db = mysqli_connect("localhost" , "", "") 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');
}

$smsid = (isset($_GET['id']) ? $_GET['id'] : null);

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

$query = mysqli_query($db, $sqlCommand) or die (mysqli_error($db));

echo '<form action="send-sms.php" method="post">';
echo '<select>';
echo '<option value="">Choose the mobile number</option>';
while($row= mysqli_fetch_assoc($query)){

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

}
echo '</select>';

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

?>

I have altered the coding slightly to try and get it working but got a undefined index: id error

 

below is the script for send-sms-message.php that the error is on

 



<?php

$db = mysqli_connect("localhost" , "", "") 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');
}

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

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

$query = mysqli_query($db, $sqlCommand) or die (mysqli_error($db));

echo '<form method="get" action="send-sms.php">';
echo '<select>';
echo '<option value="">Choose the mobile number</option>';
while($row= mysqli_fetch_assoc($query)){

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

}
echo '</select>';

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

?>

there's something faulty with your method of learning and programming. you have hundreds and hundreds of forum posts around the web, yet, you don't seem to have learned any of the methods that are being used, that would allow you create (or debug problems with) your own code. each of your posts are missing basic 'how to' building block information that you should have learned along the way.

 

it doesn't even appear like you understand what each of your files are trying to accomplish. if you did, you wouldn't have changed the query in the code that's building the select/option menu.

 

a large amount of generalization, continuity, and consistence is required in programming. you need find a different approach that will allow you to learn the meaning of what you are doing, so that anything you learn can be reused the next time you do something similar.

I couldn't agree with mac_gyver more. He said it in a way better tone than I would have projected.

 

Ian(?) - you are prolific in your posts but as said already you don't seem to be learning anything. Why is that? Are you familiar with the PHP manual at least? Or perhaps some other books/manual/references that you are using to build your knowledge and skillset? If not - you apparently need to do so. The "dive right in" approach is not working for you.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.