Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Radio buttons - how to?


GwilliamP

Recommended Posts

I have a HEAVILY modified 2.2 rc2a shop that I will not be upgrading till 3.x is working. There does not seem to be a simple survey contribution that meets my requirements so I decided to make one. I have made a copy of the 'Contact Us' page with all the required files modified to make it work. The input area uses a text area field which is created as follows;

 

<td><?php echo tep_draw_textarea_field('enquiry', 'soft', 15); ?></td>

 

This text is added to the email sent to the store owner (me) on clicking the Continue button

 

I would like to add some Radio Buttons (set of 5) but have not seen osC use them anywhere. Can anyone suggest a method of creating these and also adding the result to the email?

 

If I get this working I would like to offer this as a contribution. Contributors here will be credited.

Link to comment
Share on other sites

Thanks Jim. I dug about and found the function in html_output.php. It is hardly documented for a PHP NooB.

 

I have managed to create a set of 5 buttons that appear to work using the following.

            	<td><?php
             	echo tep_draw_radio_field('button1', $value = '5', $checked = false, $parameters = '5');
             	echo tep_draw_radio_field('button1', $value = '4', $checked = false, $parameters = '4');
             	echo tep_draw_radio_field('button1', $value = '3', $checked = false, $parameters = '3');
             	echo tep_draw_radio_field('button1', $value = '2', $checked = false, $parameters = '2');
             	echo tep_draw_radio_field('button1', $value = '1', $checked = false, $parameters = '1');
				?></td>

It would be nice to know what $parameters is for. I have modified the email line to include the results as follows.

tep_mail(STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, EMAIL_SUBJECT, "Message sender: ".$name." (".$email_address.")<br><br>".$enquiry." Button 1 ".$value, 'Survey Form', STORE_OWNER_EMAIL_ADDRESS);

Unfortunately the email returns with a button value of 1 no matter what I click on. Any idea why?

Link to comment
Share on other sites

osCommerce has virtually no documentation, including in the code. This makes learning it a lot more interesting. Hang in there; it will eventually become clear. Well, clearer.

 

$parameters is there for adding additional parameters to the HTML that the function spits out, such as ' class="radio_button"'. You don't need them unless you are doing something special, such as styling the buttons with CSS or Jquery. My example adds a CSS class that can be styled using normal CSS or modified with Javascript/Jquery.

 

When calling the function, put only the values in the arguments. Try this:

                <td><?php
               echo tep_draw_radio_field('button1', '5', false);
               echo tep_draw_radio_field('button1', '4', false);
               echo tep_draw_radio_field('button1', '3', false);
               echo tep_draw_radio_field('button1', '2', false);
               echo tep_draw_radio_field('button1', '1', false);
                               ?></td>

Then for the email:

$value = (int)$_POST['button1'];
tep_mail(STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, EMAIL_SUBJECT, "Message sender: ".$name." (".$email_address.")<br><br>".$enquiry." Button 1: ".$value, 'Survey Form', STORE_OWNER_EMAIL_ADDRESS);

Regards

Jim

See my profile for a list of my addons and ways to get support.

Link to comment
Share on other sites

Thanks Jim - that did the trick!

 

I had worked out the correct use of tep_draw_radio_field by looking again at the function and experimenting while waiting. What I had not done was work out the line before the tep_mail section.

 

From all of this I have now worked out how to add as many sets of radio buttons as I need and email the results. Without the $value = (int)$_POST['button1']; line it would not have been possible. I can't say I fully understand that line but I get the basic idea enough to duplicate/modify it to work with multiple sets.

 

Now all I have to do is formulate the content of the survey and make it available to my customers. Once completed it will be included in the next monthly newsletter. I intend to modify the 'success' message to include a discount code for those that take the time to complete the survey.

 

If I publish the finished project as a contribution I will definitely credit you for making it possible.

Link to comment
Share on other sites

The radio buttons, like all other form fields, are submitted to the page specified in the <form> tag, using the method specified in that tag. In this case, and in most osCommerce forms, the form contents are submitted to the page the form is on, using the POST method. You can, therefore, retrieve the values anywhere on that page (after the form is submitted) by using $_POST['foo'], where foo is the name of the field. In your case, the buttons are named 'button1'.

 

I added (int) to sanitize the values. You should always clean your values to prevent hacking. Your radio buttons always have an integer value, so (int) will remove anything that is not an integer. Of course integers are safe in most cases.

 

I set it equal to $value because you had used that in your mail() code. I would normally use something less generic, both to make the code more readable and to avoid interfering with the same variable used elsewhere in the code.

 

Regards

Jim

See my profile for a list of my addons and ways to get support.

Link to comment
Share on other sites

  • 2 weeks later...

Jim, sorry for the long delay in responding. 'Life' happened.

 

I have nearly finished the survey page and will be creating a contribution shortly. I will definitely include credit for you unless you do not wish this. I am currently tidying up the code/page and adding documentation. I will create instructions on installing and customising it to individual needs.

 

If I get time before I need to make it public on Jane's website I intend to look into making some of the responses 'required' and the mechanism for dealing with those elements if unfilled.

 

Regards,

Paul.

Link to comment
Share on other sites

I have no objection to credit for what I did, which is mainly teaching. I much prefer teaching people like you rather than simply handing somebody a ready-made chunk of code. Spreading knowledge is always a good thing.

 

Regards

Jim

See my profile for a list of my addons and ways to get support.

Link to comment
Share on other sites

I much prefer teaching people like you rather than simply handing somebody a ready-made chunk of code.

 

A cheeky question?

 

After about an hour of Googling I have settled on the following 'chunk of code' as a starting point for what I have in mind. Before I bust my brain making it work with the code I now have, is this a reasonable place to start or would you suggest a different method? This is a RAW rip from this page. There is a LOT to be changed but the basic mechanism looks OK to me.

<?php
/*
*  Specify the field names that are in the form. This is meant
*  for security so that someone can't send whatever they want
*  to the form.
*/
$allowedFields = array(
'Name',
'Email',
'URL',
'Company',
);

// Specify the field names that you want to require...
$requiredFields = array(
'Name',
'Email',
'URL',
);

// Loop through the $_POST array, which comes from the form...
$errors = array();
foreach($_POST AS $key => $value)
{
// first need to make sure this is an allowed field
if(in_array($key, $allowedFields))
{
   	$key = $value;

   	// is this a required field?
   	if(in_array($key, $requiredFields) && $value == '')
   	{
       	$errors[] = "The field $key is required.";
   	}
}
}

// were there any errors?
if(count($errors) > 0)
{
$errorString = '<p>There was an error processing the form.</p>';
$errorString .= '<ul>';
foreach($errors as $error)
{
   	$errorString .= "<li>$error</li>";
}
$errorString .= '</ul>';

// display the previous form
include 'index.php';
}
else
{
// At this point you can send out an email or do whatever you want
// with the data...

// each allowed form field name is now a php variable that you can access

// display the thank you page
header("Location: thanks.html");
}

Link to comment
Share on other sites

There is a lot of good work in that code. I would suggest some improvements:

1. For required fields, check that the data is valid rather than just checking that something is there.

2. With or without errors, this should redirect to the original page. What is displayed on that page can depend on whether it has errors or not.

3. Use the osCommerce error handler.

 

The stock osC Contact Us page is not as clean, but still has a lot of good code you can copy.

 

Regards

Jim

See my profile for a list of my addons and ways to get support.

Link to comment
Share on other sites

There is a lot of good work in that code. I would suggest some improvements:

1. For required fields, check that the data is valid rather than just checking that something is there.

2. With or without errors, this should redirect to the original page. What is displayed on that page can depend on whether it has errors or not.

3. Use the osCommerce error handler.

 

The stock osC Contact Us page is not as clean, but still has a lot of good code you can copy.

 

Regards

Jim

 

I used the Contact Us page as a start. I have copied/modified it. I also made a includes/languages/english/survey.php from the contact file there.

 

1 - let me walk before running LOL! Apart from 1 free text area (len>1 ?) I only want to make sure all the radio buttons are used. They return int values of 1-5 so x>0 should do for them.

2 - I intended to change "include 'index.php';" to "include 'survey.php';" and see what happened. Not a clue what to expect but hoping to re-display the page with entries still in place.

3 - ? The only one I have seen is the email address checker.

 

If you are interested, this is a link to the survey page but I am currently breaking it regularly!

 

Paul.

Link to comment
Share on other sites

Proceed at your own pace. I was only offering suggestions.

 

Post here if you have any more questions. I'll try to help as I can.

 

Regards

Jim

See my profile for a list of my addons and ways to get support.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...