dear all,
i would like to build a php snippet able to interact with smsgateway.module; the goal is to have a submit button(or link) to let the function smsgateway_sendmessage run, but i'm actually a php beginner and i don't know how to get it works. I've tried to "invent" something, but still get problems.
it would be great if someone already dealed with a similar problem and/or could help me getting the right code.

many many many thanks in advance.

That's what i wrote down:

<?php 
global $user;
profile_load_profile($user->uid);  
$destination_number=($user->profile_cel); //i call back the mobile number from user profile
$message_body=($user->name) . ", text text text. -- staff.mysite.com "; //i define a message body
//and then try to build the form ?>
<form name="sms" method="post" action="<?php smsgateway_sendmessage( $destination_number, $message_body) ;?>">
To get your sms just click here!<br/>
<input name="set" type="submit" value="submit">
</form>

as you can see, even if it is a very simple snippet i get problems... :(
i guess i should insert the function in a separate php file because i cannot embed it in action="" attribute... but it would be "outside drupal environment" and functions would be undefined. (i've tried it).
As it appears, the script sends sms... but it happens every time the page is laoded... even if the submit button isn't clicked ever.

Moreover, i've inserted a random value to the message_body to identify it, but have an error like "cannot redeclare previously delacred function randomkeys()..." .

i use this code to generate the random string and send it:

  <?php
function randomkeys($length)
  {
   $pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
   for($i=0;$i<$length;$i++)
   {
     $key .= $pattern{rand(0,35)};
   }
   return $key;
  }

global $user;
profile_load_profile($user->uid);  
$destination_number=($user->profile_cel); //i call back the mobile number from user profile
$message_body=($user->name) . ", thank for participating. Your ID is " . $randomkeys(6) ; //i define a message body with random string of 6 car.
//and then try to build the form the same way ?>
<form name="sms" method="post" action="<?php smsgateway_sendmessage( $destination_number, $message_body) ;?>">
To get your sms just click here!<br/>
<input name="set" type="submit" value="submit">
</form>

thanks another time!!!

Comments

nevets’s picture

First lets look at why the message is sent twice without even hitting the submit key. This section

<form name="sms" method="post" action="<?php smsgateway_sendmessage( $destination_number, $message_body) ;?>">

runs the function smsgateway_sendmessage() twice, once when the teaser view of the node is generated and again when the full view is generated. Action should be a valid URL and while you can use PHP to generate the URL that takes place when the form is display.

The problem with randomKeys being redefined is again due to the teaser and full views, in this case the include is done twice. You could use include_once for this issue.

I would recommend making a small module to handle this. You will need to implement (at least) the menu hook to define a callback for the function that will produce the form, a function to produce the form and one to handle the submittion of the form.

tomsci’s picture

Cheers nevets,

i've worked it out a little bit and i did a very simple moduleimplementing the hook_block only, with a variation of the previously attached code.
it is a dummy module but it's working. i'll try to use it as a starting point to build a real module as my knowledge will improve.

Thank you very much for your support.

Tommi

Ps. evenif it's a very bad written code i post it below :

// $Id$ 0.1.1

/**
 * @sendsms.module
 * Smsgateway based module to send sms to users.
 * !! Still under developement!!
 * 
 */


/**
 * Implementation of hook_block().
 */
function promosms_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('Send sms to user. Block only module.');
    return $blocks;
  }
  else if ($op == 'configure') {
  }
  else if ($op == 'save') {
  }
  else if ($op == 'view') {
 
 //random generator function
	
	function randomkeys($length)
  {
   $pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
   for($i=0;$i<$length;$i++)
   {
     $key .= $pattern{rand(0,35)};
   }
   return $key;
  }
  
 //send message function. much much work still to do
   function invio()
	{
	global $user;
	profile_load_profile($user); 
	$destination_number=$user->{profile_mobile}; 
	$message_body=($user->name) . ", la tua promozione e stata inviata. Conserva questo messaggio. KeyId: ". randomkeys(6) ." -- staff.ritualiurbani.com ";
	if (isset($user->{profile_mobileok})&&($user->{profile_mobileok})=='1')  {smsgateway_sendmessage( $destination_number, $message_body); $s='Ti abbiamo appena spedito un sms sul tuo cellulare. Conserva il mesaggio con il KeyId.' ; return $s;	} 
	elseif (($user->{profile_mobileok})=='0') { $t='Devi impostare il tuo profilo personale per ricevere sms.'; return $t ;	} 
	}
//cases
	switch ($delta) {
      case 0:
        $block['subject'] = t('Promo Sms');
        $block['content'] = invio();
        break;
   }
    return $block;
  }
}