Help with HTML FORM Syntax when using an image as the submit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andreea115
Forum Newbie
Posts: 9
Joined: Tue Aug 23, 2011 6:18 am
Location: United Kingdom

Help with HTML FORM Syntax when using an image as the submit

Post by andreea115 »

hi everyone.

i have two question regarding the correct HTML form SYNTAX for an image submit button for my PHP script.

QUESTION ONE:

i have a script that allows members of a site to send 'set saved messages ' to other members. all they need to do is to press onto an icon

( its a email icon) and the 'set saved message' is sent.

the problem is that i dont know how to access this message once it is saved. sorry if my explanation is not very clear. it might be easier if i show u.

Code: Select all


// THIS IS THE SAVED MESSAGE PLACED INTO A VARIABLE  

$MESSAGE =  "this is my saved messaged"; 


// start of the If condition to receive the sent message. 

    if(isset($_POST['image']))
     
    //Start of  conditional  that test whether the form was submitted .    
    {
          
            require_once ('db_fns.php'); // Connect to the db.
    
          //process the text-input field

       if(!empty($_POST['savedmessageidd'])) {
        $savedmessage =   mysqli_real_escape_string($dbc, trim( $_POST['savedmessageidd']));
    
     echo "this is the saved message  $savedmessage "; 
      
      }
                
     
      
      //process the hidden filed text
      if(!empty($_POST['savedmessageid'])) {
        $savedmessageid =   mysqli_real_escape_string($dbc, trim( $_POST['savedmessageid']));
    
      echo "this is the saved message id    $savedmessageid "; 
      
      }


    
    }//End of conditioanal that test whether the form was submitted .
        
        

//START OF THE FORM 


<form action= "  "      method="post" name="form" >

<input  type="image"  name="savedmessage" src="pics/icons/iconemail4.jpg" 
   width="30" height="30" alt="aupair from   "  value="image"       /> Send Saved Email </a>    
    
    <input type="hidden" name="message "    value=" <?php echo $MESSAGE ?>" />
    
    <input type="hidden" name="savedmessageid"    value="17" />


what i attempted to do above is to send two seperate pieces of data to the server i.e;
1. the $MESSAGE
2. THE NUMBER 17


i am able to retreive this information. the problem i have however is how to access that information via the IF condition i.e

if(isset($_POST['image']))

i understand that normallly ( had i usd a submit button rather than the image, ) the if condition would have been;

if(isset($_POST['submit']))


but now that i am using an image, what should the if test be? i tried using the


if(isset($_POST['image']))

but it does not work . please advise where i am going wrong.


ALSO, did i use the correct method to send the information. you will note that i used two seperate hidden filed imput to send the seperate information. should i instead have sent it via a

value="message"

within the


<input type="image" name="savedmessage" src="pics/icons/iconemail4.jpg"
width="30" height="30" alt="aupair from " value="image" /> Send Saved Email </a>


i.e



<input type="image" name="savedmessage" src="pics/icons/iconemail4.jpg"
width="30" height="30" alt="aupair from " <?php echo $MESSAGE ?> /> Send Saved Email </a>


i tried to send a value this way but it did not work, hence my resorting to using two seperate hidden filed imputs types .

QUESTION TWO

you would have noted that after the



<input type="image" name="savedmessage" /> Send Saved Email </a>


i had a text "Send Saved Email"

how do i do it so that if the mouse scroll over both the image or the text "send saved email" that the form is submitted.

i am not sure of the best way to achieve this.

thank you for your kind assistance.


warm regards

Andreea
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: Help with HTML FORM Syntax when using an image as the su

Post by maxx99 »

Lets stop on Quetsion 1 :)
Your code is a mess. Check this
http://framework.zend.com/manual/en/cod ... style.html

Whats in your db_fns.php

Where do you save these messages?
andreea115
Forum Newbie
Posts: 9
Joined: Tue Aug 23, 2011 6:18 am
Location: United Kingdom

Re: Help with HTML FORM Syntax when using an image as the su

Post by andreea115 »

hello everyone

i am writing in reply to the kind responce from Maxx99 who asked two questions in responce to my query.

QUESTION ONE Whats in your db_fns.php

the db_fns.php is my database connection function. i enclose the connection below ;

Code: Select all



// Make the connection:
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

if (!$dbc) {
	trigger_error ('Could not connect to MySQL: ' . mysqli_connect_error() );
}





QUESTION TWO: Where do you save these messages?

They are saved in a mysql database. i enclose below the SQL used to create the database and storage facility;

Code: Select all


       
      
USE world;
CREATE TABLE massages(
 
                 receiver_id int(11) NOT NULL ,
		 sender_id   int(11) NOT NULL ,
                 messages varchar(255) NOT NULL,
		 date_sent varchar(255) NOT NULL,
		 receiver-mem-type   varchar(30) NOT NULL,              
              
              
  INDEX massage  (receiver_id) 
   
              
                ) ";
               
                       
// the 'message'  column is the  column for the actual messages that the users sends out. 
// the  'receiver-mem-type' only refers to the type of membership of the site of the user who received the message. 



i hope this is helpful.

i have been advised that my code is messy. i will read the recommended reading for this and responce shorty.

in the mean time, may i again thank everyone who has taken an interest in helping me with this issue. i am still quite new to php and make a lot of mistakes. but i am extremly willing and kean to learn.

warm regards

Andreea
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Help with HTML FORM Syntax when using an image as the su

Post by mikeashfield »

I've attempted to clean up the code for you, but I can't actually see where the form is getting it's values from? As it's submitting to itself. NOTE: I got rid of $MESSAGE as you declare it, but don't use it at any point (unless this is not the full code excerpt.)

Code: Select all

<?php
    if (isset(isset($_POST['image'])&&isset($_POST['savedmessageidd'])&&strlen($_POST['savedmessageidd'])>0) {
        require_once('/db_fns.php');
        $savedmessageidd=$_POST['savedmessageidd'];
        $savedmessage=mysqli_real_escape_string($dbc, trim($savedmessageidd));
        echo "The saved message is: ".$savedmessage;
    } elseif (isset($_POST['image'])&&isset($_POST['savedmessageidd'])&&!strlen($_POST['savedmessageidd'])>1) {
        //Invalid SavedMessageIDD - It is less than 1 char long. 
    } else {
        //No values were passed from the form or the SavedMessageIDD was less than or equal to 1 char long.
    }
?>
andreea115
Forum Newbie
Posts: 9
Joined: Tue Aug 23, 2011 6:18 am
Location: United Kingdom

Re: Help with HTML FORM Syntax when using an image as the su

Post by andreea115 »

hi again everyone

i write in responce to the help given Mikeashfield . the question raized was

"where the form is getting it's values from"



the values come from teh followng imput fields

Code: Select all

     
<?php

		
	$MESSAGE = "ThIS IS THE SAVED MESSAGE";	
		
		
 
  
   <input  type="image"  name="savedmessageidd" src="pics/icons/iconemail4.jpg" 
   width="30" height="30" alt="   "  value="image"       /> Send Saved Email </a>
    
    
    <input type="hidden" name="savedmessageidd"    value="<?php echo $MESSAGE ?>" />
    
    <input type="hidden" name="savedmessageid"    value="17" />
    ?>
you will note that i am trying to send two values to teh database;
1. the saved message i.e $MESSAGE = "ThIS IS THE SAVED MESSAGE";
2. the value 17.

you will also note that the problem is that the imput filed is linked to a pictures. so , its only when the user clicks onto the picture that the form upload is triggered. the message is a saved message, so the user does not need to type anything in.

i have tried useding the amended 'If clause' below but no value is being received. i have no idea where i have gone wrong and why its not receiving any data.
the only thing that i can thing of is that the
(isset($_POST['image'])

is not receiving any value

Code: Select all

 
<?php
    if (isset(isset($_POST['image'])&&isset($_POST['savedmessageidd'])&&strlen($_POST['savedmessageidd'])>0) {
        require_once('/db_fns.php');
        $savedmessageidd=$_POST['savedmessageidd'];
        $savedmessage=mysqli_real_escape_string($dbc, trim($savedmessageidd));
        echo "The saved message is: ".$savedmessage;
    } elseif (isset($_POST['image'])&&isset($_POST['savedmessageidd'])&&!strlen($_POST['savedmessageidd'])>1) {
        //Invalid SavedMessageIDD - It is less than 1 char long.
    } else {
        //No values were passed from the form or the SavedMessageIDD was less than or equal to 1 char long.
    }
 
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Help with HTML FORM Syntax when using an image as the su

Post by mikeashfield »

I think what would be better is if you could explain clearly, what exactly you are trying to do with the script. Be very concise and clear; the more we know the better we can help you :) I think the code needs re-writing.
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: Help with HTML FORM Syntax when using an image as the su

Post by maxx99 »

First of all, are you aware that

Code: Select all

$savedmessage=mysqli_real_escape_string($dbc, trim($savedmessageidd));
is not saving the message?
Here's a reference and example how you use it with queries:
http://php.net/manual/en/mysqli.real-escape-string.php

Thats the important part:

Code: Select all

$mysqli->query(...);
Post Reply