Novahq.net Forum

Novahq.net Forum (https://novahq.net/forum/index.php)
-   phphq.Net Forums (https://novahq.net/forum/forumdisplay.php?f=277)
-   -   Phmailer file attachment options (https://novahq.net/forum/showthread.php?t=42070)

Frizzo 10-18-2008 06:52 PM

Phmailer file attachment options
 
Hi!

I use the Phmailer as an contact form, and I need the user to be able to choose how many attachment fields the user needs. Currently I have it set to 10 fields, but it looks a bit too much. And i really need like 10 or 15 fields.

How do i create a drop down menu for the number of attachment fields that i want the customer to be able to choose from?

/ Frizzo from Sweden

atholon 10-19-2008 01:01 PM

http://www.webcheatsheet.com/PHP/sen...php#attachment

Frizzo 10-19-2008 01:24 PM

Im sorry, but i really don't know what to do with that. Do you care to explain? :-)

atholon 10-20-2008 06:35 PM

Sorry I misunderstood the question.

You'd do that in javascript. I will post an example in a minute.

Code:

<script language="javascript">

var attachbutton = document.getElementById("addextraattach");

if (attachbutton)
{
  attachbutton.onclick = addField;
}

function addField()
{

var attachBox = document.createElement("input");
attachBox.type = "file";
attachBox.name = "attachments[]";

var attachments = document.getElementById("attachmentdiv");
if (attachments)
{
attachments.appendChild(attachbox);
}
}
</script>

<form action="phMailer.php" method="post" enctype="multipart/form-data">
<p>Attachments
<input type="file" name="pictures[]" />
<div id="attachmentdiv"></div><input type="button" value="Add Another Attachment" id="addextraattach" />
<input type="submit" value="Send" />
</p>
</form>

atholon 10-20-2008 08:16 PM

Here we go:

Code:

<html>
<head>
<script type="text/javascript">
function init()
{
var attachbutton = document.getElementById("addextraattach");

if (attachbutton)
{
  attachbutton.onclick = addField;
}
}
function addField()
{
  allowedFiles = 10;
  if (!addField.count)
  {
    addField.count = 1;
  }
  else
  {
    addField.count++;
  }

if (addField.count < allowedFiles)
{
var boxDiv = document.createElement("div");

var box = document.createElement("input");
box.type = "file";
box.name = "attachments[]";

boxDiv.appendChild(box);

var attachments = document.getElementById("attachmentdiv");

if (attachments)
{
  attachments.appendChild(boxDiv);
}
}
else
{
    alert("You can only add "+allowedFiles+" files!");
}
}
</script>
</head>
<body onload="javascript:init();">

<form action="phMailer.php" method="post" enctype="multipart/form-data">
<div>Attachments</div>
<div>
<input type="file" name="attachments[]" />
<div id="attachmentdiv"></div><input type="button" value="Add Another Attachment" id="addextraattach" />
</div>
<span><input type="submit" value="Send" /></span>

</form>
</body>
</html>


atholon 10-21-2008 08:49 PM

Oh sorry, I have not used scott's script for a while, here's what you do:


1. Go to line 577 of phMailer.php
2.
Replace:
Code:

<?For($i=1;$i <= $allowattach; $i++) {?>
        <tr>
                <td width="30%" class="table_body">Attach File:</td>
                <td width="70%" class="table_body"><input name="attachment[]" type="file" size="30" /></td>
        </tr>
<?}?>

With:
Code:

 
<tr>
  <td width="30%" class="table_body">Attach File:</td>
  <td width="70%" class="table_body"><div id="attachmentdiv"><input name="attachment[]" type="file" size="30" /></div>
  </td>
</tr>
<tr>
  <td colspan="2" class="table_body"><input type="button" value="Add Another Attachment" id="addextraattach" />
  </td>
</tr>

3. Replace code starting on 486
Code:

<script type="text/javascript">
var error="";
e_regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;

function Checkit(theform) {
       
        if(theform.yourname.value=="") {
                error+="You did not enter your name\n";
        }
       
        if(theform.youremail.value=="") {
                error+="You did not enter your email\n";
        } else if(!e_regex.test(theform.youremail.value)) {
                error+="Invalid email address\n";
        }
               
        if(theform.yourmessage.value=="") {
                error+="You did not enter your message\n";
        }
       
        if(error) {
                alert('**The form returned the following errors:**\n\n' + error);
                error="";
                return false;
        } else {
                return true;
        }
}
</script>

With this:
Code:

<script type="text/javascript">
var error="";
e_regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;

function Checkit(theform) {
       
        if(theform.yourname.value=="") {
                error+="You did not enter your name\n";
        }
       
        if(theform.youremail.value=="") {
                error+="You did not enter your email\n";
        } else if(!e_regex.test(theform.youremail.value)) {
                error+="Invalid email address\n";
        }
               
        if(theform.yourmessage.value=="") {
                error+="You did not enter your message\n";
        }
       
        if(error) {
                alert('**The form returned the following errors:**\n\n' + error);
                error="";
                return false;
        } else {
                return true;
        }
}

function init()
{
 var attachbutton = document.getElementById("addextraattach");

 if (attachbutton)
 {
  attachbutton.onclick = addField;
 }
}
function addField()
{
  allowedFiles = <?php echo $allowattach;?> ;
  if (!addField.count)
  {
    addField.count = 1;
  }
  else
  {
    addField.count++;
  }

 if (addField.count < allowedFiles)
 {
  var boxDiv = document.createElement("div");

  var box = document.createElement("input");
  box.type = "file";
  box.name = "attachment[]";
  box.size = "30";

  boxDiv.appendChild(box);

  var attachments = document.getElementById("attachmentdiv");

  if (attachments)
  {
    attachments.appendChild(boxDiv);
  }
 }
 else
 {
    alert("You can only add "+allowedFiles+" files!");
 }
}
window.onload = init;
</script>

And you should be good to go. :)

Frizzo 10-22-2008 10:37 AM

Thanks mate, I'm going to try it out and then I'll get back to you with the result! :)

F

Frizzo 10-22-2008 02:52 PM

Thank you Atholon, it works like a charm. :)

atholon 10-22-2008 03:32 PM

No problem :)

Sorry I didn't understand it at first.

Frizzo 10-23-2008 07:58 PM

Quote:

Originally posted by atholon
No problem :)

Sorry I didn't understand it at first.

It seems like it did not work after all. When i send an email from the form, i only get one attachment, probably because I have set allowed number of attachments to 1 in the beginning of the script. And if i set it to zero, the button that you, Atholon, created for me does not show.

So, what should I do? :)

/F

atholon 10-23-2008 08:04 PM

change $allowattach="1"; to $allowattach = "10";

odd that it is a string but what the hay.

Frizzo 10-24-2008 05:38 AM

1 Attachment(s)
Quote:

Originally posted by atholon
change $allowattach="1"; to $allowattach = "10";

odd that it is a string but what the hay.

Well I did that too, but then I just got 10 fields with an "add extra attachment"-button under each field..

Here, take a look. The text is in Swedish but I tink you'll get the picture anyway.

Edit: It says, when i have added 10 fields that "You can only add 10 files!", but it does not send the extra added nine attachments..

It's hard to explain..

atholon 10-24-2008 05:32 PM

I will have to look more into how he implements it in his script.

It should only have one "add another attachment" button if you used my code.

Frizzo 10-24-2008 05:40 PM

Quote:

Originally posted by atholon
I will have to look more into how he implements it in his script.

It should only have one "add another attachment" button if you used my code.

I really appreciate the help! :) So you think you can fix it?

/F

atholon 10-24-2008 06:37 PM

OK dude, it is because I made the input name attachments[] try attachment[] I have it updated in the above code.

I also fixed some styling issues, I never really tested it out but I just gave it a shot and it is working fine. You can change same var that scott added to change the number of files that can be attached.

Frizzo 10-24-2008 06:49 PM

I thank you for your effort, but it still does not work. Looks (and works) exactly like before..

**** it, I'll get another form..

atholon 10-24-2008 06:50 PM

Try using my version and then just change it to what you want.

I will upload it in a second.

Frizzo 10-24-2008 06:55 PM

Sure!

atholon 10-24-2008 06:57 PM

http://digitalhelpfiles.com/phMailer.zip

Scott, if you want to look this over and post it on your site that's cool. I just don't want you to get pissed off for me putting it on my server.

If you're still having problems with that one I sent then you have something else messed up because I just tested it twice and it sent two attachments.

Frizzo 10-24-2008 07:11 PM

It works fine now, I must have messed something up on my own.

Thanks! :)


All times are GMT -5. The time now is 05:37 PM.

Powered by vBulletin®