Display simple response after submitting form successfully

Hi Experts

We have got the following PHP page:

<?php

function sendSMS($content) {

$ch = curl_init('http://www.smsglobal.com/http-api.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
curl_close ($ch);
//return $output; - uncomment this line if you would like to see the SMSGlobal HTTP Response
}

if(isset($_POST) && !empty($_POST)) {
//Set variables
$user= 'username';
$password= 'password';
$to= $_POST['to'];
$from= $_POST['from'];
$message= $_POST['message'];

//Encode content and send to SMS Global
$content ='action=sendsms'.
'&user='.rawurlencode($user).
'&password='.rawurlencode($password).
'&to='.rawurlencode($to).
'&from='.rawurlencode($from).
'&text='.rawurlencode($message);

//Send SMS through curl
$output = sendSMS($content);
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Send SMS</title>
</head>
<body>
<?php if(isset($output)) echo $output; else { ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<h1>Send SMS</h1>
<table>
<tr>
<td>Phone Numbers (Separate with comma)</td>
<td><input type="text" name="to" /></td>
</tr>
<tr>
<td>Sender ID</td>
<td><input type="text" name="from" maxlength="11" /></td>
</tr>
<tr>
<td>Message</td>
<td><textarea name="message"></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form><?php } ?>
</body>
</html>

Open in new window


When the user has completed the form and clicked the Submit button, we would like to display a simple confirmation message to the user, something like "Thank you. Your message has been sent"

We are unsure of how to do this. Please can you assist with the correct code.

Thanks
wesmanbigmig13Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
This is one of those pieces of code that has the "code smell" indicating it was maybe copied from an untrustworthy resource on the internet.  "Everything on the internet is true," said Ben Franklin.  Going forward, please don't just pick code up and use it without understanding what it does.  You would be better off with no code than with something that doesn't work but is taking up space and time and energy!

Here is a revision of the code, set up to show a little more in the way of error reporting.  At this point in the application cycle, you really want to see all the errors.  In case you get cURL error #28, you can know that is a timeout, meaning that the resource at the cURL URL did not respond in time.
http://www.laprbass.com//RAY_temp_wesmanbigmig13.php

<?php // RAY_temp_wesmanbigmig13.php
error_reporting(E_ALL);

// A FUNCTION TO RUN A CURL POST REQUEST
function curl_post($url, $post_array=array(), $timeout=2, $error_report=TRUE)
{
    // PREPARE THE POST STRING
    $post_string = NULL;
    foreach ($post_array as $key => $val)
    {
        $post_string .= $key . '=' . urlencode($val) . '&';
    }
    $post_string = rtrim($post_string, '&');

    // PREPARE THE CURL CALL
    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL,            $url         );
    curl_setopt( $curl, CURLOPT_HEADER,         FALSE        );
    curl_setopt( $curl, CURLOPT_POST,           TRUE         );
    curl_setopt( $curl, CURLOPT_POSTFIELDS,     $post_string );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout     );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE         );

    // EXECUTE THE CURL CALL
    $htm = curl_exec($curl);
    $err = curl_errno($curl);
    $inf = curl_getinfo($curl);

    // ON FAILURE
    if (!$htm)
    {
        // PROCESS ERRORS HERE
        if ($error_report)
        {
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            echo "<pre>\n";
            var_dump($inf);
            echo "</pre>\n";
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS
    curl_close($curl);
    return $htm;
}

// LOCATION OF THE POST TARGET
$url = 'http://www.smsglobal.com/http-api.php';

// IF THERE IS A POST REQUEST TO THIS SCRIPT
if(!empty($_POST))
{
    // CREATE THE POST ARRAY
    $arr['action']   = 'sendsms';
    $arr['user']     = 'username';
    $arr['password'] = 'password';

    // INCORPORATE THE POST-METHOD REQUEST VARIABLES
    $arr['to']       = $_POST['to'];
    $arr['from']     = $_POST['from'];
    $arr['message']  = $_POST['message'];

    // MAKE THE cURL REQUEST USING THE ARRAY OF INFORMATION FIELDS
    $output = cURL_Post($url, $arr, 5);
}
// END OF PHP - DROP INTO HTML

?><!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<title>Send SMS</title>
</head>
<body>
<?php
// IF THE ACTION SCRIPT HAS BEEN RUN
if(isset($output))
{
    echo $output;
}
// IF THE ACTION SCRIPT HAS NOT YET BEEN RUN, PUT UP THE FORM
else
{ ?>
<form method="post">
<h1>Send SMS</h1>
<table>
<tr>
<td>Phone Numbers (Separate with comma)</td>
<td><input type="text" name="to" /></td>
</tr>
<tr>
<td>Sender ID</td>
<td><input type="text" name="from" maxlength="11" /></td>
</tr>
<tr>
<td>Message</td>
<td><textarea name="message"></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
<?php
} // END OF PUTTING UP THE FORM SCRIPT
?>
</body>
</html>

Open in new window

HTH, ~Ray
0
wesmanbigmig13Author Commented:
Thanks Ray, but I cannot see where in your code you have included the simple confirmation message that is shown to the user after the form is submitted, something like "Thank you. Your message has been sent" ... ?
0
Ray PaseurCommented:
Understand.  Check line 82.  That's the problem with starting in the deep end.  Please install the script and run it, then post your output here, along with the inputs you used to acquire the output.  I am sure we can help you get it.

A good place to start learning PHP is here:
http://www.amazon.com/PHP-MySQL-Web-Development-Edition/dp/0321833899
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

wesmanbigmig13Author Commented:
Hi Ray. The output when we ran the script is:

OK: 0; Sent queued message ID: 4698d1172979c312 SMSGlobalMsgID:6376786862351743

This is the HTTP Response received from the SMSGlobal server.

What we need to do is not show that response but instead be able to assign our own text to the $output variable so that our text is shown on the screen instead of the HTTP response. That's what we need to do. Thanks
0
wesmanbigmig13Author Commented:
I have solved the problem - line 11 of my originally posted code had to be replaced by the following 2 lines of code:

$output = 'Thank you. Your message has been sent.';
return $output;

Thanks for the assistance that was provided but in the end all that was needed was for the $output variable to be assigned a simple text string. Once we worked out how to do that ourselves it solved the problem.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
in the end all that was needed...
Let me ask you a rhetorical question: If that was all that was needed why didn't you just do that in the first place?  The fact is that you didn't know what was needed, and you couldn't know until you got some professional help and used some predictable processes to visualize the behavior of the script.  

The script you posted had a comment in it, on line 11, that was deliberately intended to prevent you from seeing the output.  I had to assume that you understood this comment, otherwise you would never have put that line of code in the script, right?  Until we were able to verify that the script was working (ie, we got back a predictable response from the API) there was no way of knowing what to display.  Maybe the API failed, and you wanted to be able to present an error message or try an alternate service?

If this is just for fun and doesn't really matter, then it's OK to fumble along, but you will waste a lot of your time unless you get some structured learning about PHP.  If this process has any economic value, please consider getting professional help.  As the great oil well fire fighter Red Adair said, "If you think it's expensive to hire a professional, just wait till you hire an amateur!"
0
wesmanbigmig13Author Commented:
Thanks Ray. We commented out line 11 ourselves originally so we knew it was there. We then worked out ourselves how to do the simple assignment of a text string to a variable which is all we were asking to do in the first place. I do appreciate your help, but you didn't specifically address our actual question or resolve it. We did that ourselves in the end through trial and error, and not as a result of any assistance we received from yourself or anyone else for that matter. Thanks again.
0
wesmanbigmig13Author Commented:
The solutions supplied by the experts did not address our specific question. We worked out the solution ourselves and once we put this in place we solved our own problem. Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.