Bob Mwenda
Bob Mwenda

Reputation: 89

How to save a PHP variable when a page loads twice

A user enters two dates periods on a text-box and a SQL select statement picks mobile numbers from a database entered in between the period. I want to pick and display them on a page. On the same display page, I have a text area where a user can type a message and on submit, it should be sent to these selected numbers and displayed mobile numbers. I am having a challenge on passing the $mobilenumber to the function sendbulk that is to send the message to the mobile numbers displayed by $mobilenumber variable. Everything else is okay apart from passing the $mobilenumber. I think this is because after the page loads to display the contacts selected, on the second load as you submit the $message to bulk function the value of $mobilenumber is already lost. How can I save it. Check sample code below and please advice. How do I save the $mobilenumber so that by the second load it is still there to be passed to the function sendbulk()? Anyone?

<?php



//Define variable and set to empty values
 $message =  "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

  $message = test_input($_POST['message']);
  echo "$message";
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}



$time1             = isset($_POST['t1']) ? $_POST['t1'] : 'default something missing';
$time2             = isset($_POST['t2']) ? $_POST['t2'] : 'default something missing';
   //connection

$sql =  "SELECT DISTINCT msisdn FROM customer WHERE DATE_FORMAT(time_paid, '%Y-%c-%e') BETWEEN ADDDATE('$time1',INTERVAL 0 HOUR) AND ADDDATE('$time2',INTERVAL '23:59' HOUR_MINUTE)";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

  echo " Recipients: ";  echo "$result->num_rows <br> <br>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
 $mobilenumber = $row['msisdn'];
      echo "Mobile : " . "$mobilenumber" . "<br>";

    }
} else {
    echo "No Contacts to Display";
}

$conn->close();
sendbulk($mobilenumber,$message);

?>


<center></center> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  

  <textarea name='message' rows="6" cols="60" placeholder="Please Type Your Message Here"></textarea>
  <br><br>

  <input type="submit" name="submit" value="Send Message">  
</form></center>


<?php

function sendbulk($mobilenumber,$message) {

echo "$mobilenumber";
echo "$message";



    $serviceArguments = array(
        "mobilenumber" => $mobilenumber,
        "message" => $message_sent
    );

    $client = new SoapClient("http://*******");

    $result = $client->process($serviceArguments);



    return $result;


}

Upvotes: 0

Views: 67

Answers (1)

nyumerics
nyumerics

Reputation: 6547

You use sessions.

Here is a sample code:

<?php
session_start();

if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = 0;
} else {
  $_SESSION['count'] += 1;
}

echo $_SESSION['count'];

?>

Keep reloading this file via your web server. You should see the variable incrementing.

As an alternative, you can also use $_COOKIE. The only difference is that $_SESSION is saved on the server side and not accessible on the client. To identify the client it does store a cookie for that session on the client.

$_COOKIE on the other hand is completely stored on the client and passed by the browsers to the server on every request.

Also note a caveat, don't overload your session variables or cookies as it will hit your response times.

Also note that session_start() is required in every PHP file where you want to access the session.

Upvotes: 2

Related Questions