-1

I have multiple inquiry forms all of which call the same file used for email forwarding, so it's titled emailForwarding.php. I apparently managed to separate the forms using jQuery on the front end, but the script in emailForwarding.php is processed the same number of times as the number of the inquiry forms. I just want the php script to work for the form I submit.

I tried isolating the effect of the script using .eq() and .index() and passing an argument named $arg to only trigger form submission event for the div.vendor-wrapper containing the selected form.

single.php:

echo
 '<div class="vendor-wrapper"><form method="post" action="" name="form" class="commentForm">
<textarea name="comment" placeholder="Please enter your message in the space of 300 characters and hit the Confirm button." value="" class="message" maxlength="300"></textarea>
<input name="confirm" type="button" value="Confirm">
<input class="send" name="send'.$i++.'" type="submit" value="Send">
<input type="hidden" name="position" val="">
</form></div>;

<script>
$('.confirm').click(function(){
$('.vendor-wrapper').find('.position').val('');
var index = $(this).parents('.vendor-wrapper').index()-1;
if($('.vendor-wrapper').eq(index).find('.message').val()){
$('.vendor-wrapper').eq(index).find('.confScreen').show();
$('.vendor-wrapper').eq(index).find('.position').val(index);
}
});
</script>

emailForwarding.php:

if(isset($_POST['position'])):
$arg = 'send';
$arg .= $_POST['position'];
echo "<script>console.log('PHP: ".$arg."');</script>";
if(isset($_POST[$arg])):
 if(isset($_POST['comment'])):
   $EmailCustomer = $_POST['email'] = $current_user->user_email;
//The rest of the script for email processing omitted.

The form is submitted the same number of times as the number of the forms on the page.

Fizzler
  • 45
  • 1
  • 6

2 Answers2

0

Inserting include() before tag of single.php disabled duplicate submission.

Fizzler
  • 45
  • 1
  • 6
-1

Could you provide more code? Because I was trying to reproduce the problem but could not with the provided code. As, what $_POST['position'] stands for is not clear from code.

Is the echo statement user any loop. Can you try by giving a different name to FORM?

<form method="post" action="" name="form-$i" class="commentForm">
Akansha
  • 112
  • 6
  • $_POST['position'] is supposed to get the value of the hidden input element which I accidently left out and inserted later. It is meant to show which div.vendor-wrapper the form was submitted from. I never tried out changing the name of the forms. I will try this out tomorrow. Thanx. – Fizzler Aug 22 '19 at 12:33
  • I assigned the same variable i to the
    element and now each form shows up with a unique name value in the form of 'form-[number]'. The problem still remains as to how to identify forms separately, though. I found a question recommending the use of a hidden input element or a submit type input with a unique name: https://stackoverflow.com/questions/846020/how-to-access-the-forms-name-variable-from-php but then again I already implemented both in the code shown on this post, so the page didn't help.
    – Fizzler Aug 22 '19 at 23:54