0

I've got multiple forms like this on one page:

<form method="post" action="#">
    <input type="hidden" name="product_id" value="'.$item['articlenumber'].'" />
    <input type="text" name="update_quantity" value="'.$pg->quantity.'" />
    <input type="hidden" name="packing" value="'.$item['packing'].'" />
    <input type="hidden" name="unit" value="'.$item['unit'].'" />
    <input type="submit" name="addtocart" value="Update" />
</form>

And I've got one submit button at the bottom:

<input name='placeorder' type='submit' value='Place order' />

How can I check all forms when I press the submit button? I need to validate that the input that was given is the correct quantity.

UPDATE

I now got all the values from the forms in JavaScript and the validation is correct. Now I want to store the variables into PHP SESSIONS. I saw the answer from Ben and that would work if the values where in PHP, they are now in JavaScript. I need them on other pages so I thought Sessions would be the best thing here (if not, other suggestions are welcome).

I saw this answer and there they say it is not possible on one page. I understand that because PHP is server side and Javascript client side. Is this the only possible way to send Javascript variables to PHP?

Community
  • 1
  • 1
Tom Spee
  • 9,209
  • 5
  • 31
  • 49

3 Answers3

0

Alright, first you should remove the type='submit' on the input. So change it to :

<input name='placeorder' value='Place order' />

Then you just need to add a javascript function to validate.

$(document).ready(function() {
  $("input [name='placeorder']").click(function() {
    var formOK = true;
    $("form input").each(function(index, element) {
      var value = $(element).val();
      if (value != "OK") {
        formOK = false;
      }
    });
    if (formOK) {
      submitForms();
    } else {
      alert("Form Input Inccorect");
    }
  });
});

function submitForms() {
  $("#form1").add("#form2").submit();
}
Jay S.
  • 1,318
  • 11
  • 29
  • This looks promissing, pitty I can check in about 8 hours. I would like to store each of the input values in PHP a $_SESSION, is that possible as well? – Tom Spee Dec 20 '14 at 05:19
  • Yeah you definitely could, although from my experience, session variables often get lost. It may just be me though. What would you want to store in the session? And how will you be using it? Maybe there is an alternative. If you do want to go with session variables though, Ben Allington's response sounds right to me. @TomSpee – Jay S. Dec 20 '14 at 06:12
  • Your answer helped me in the right direction. I now got all the values in Javascript. Ben's answer would be right if the values were in PHP but they are not. I'm now trying to send the Javascript values to PHP sessions, I updated my answer. – Tom Spee Dec 20 '14 at 19:11
0

With regards to your question about storing form data in a session variable Try something along these lines:

session_start();

if (isset($_POST['placeorder'])) { 
$_session['ProductID'] = $_POST['product_id'];
$_session['UpdateQuantity'] = $_POST['update_quantity'];
$_session['Packing'] = $_POST['packing'];
$_session['Unit'] = $_POST['unit'];
} 

Hope that helps

0

Both answers helped me a little bit but I needed to combine them so I ended up with a solution like the following. I got the values from the inputs by their unique name like this:

var sizeS25 = parseInt($("input[name='size-s25']").val(), 10) || 0;
var sizeM25 = parseInt($("input[name='size-m25']").val(), 10) || 0;

Then I send the variables to a PHP file:

window.location.href = "phpsessions.php?sizeS25=" + sizeS25 + "&sizeM25=" + sizeM25;

In the PHP file I set the variables as sessions:

$_SESSION['sizeS25'] = $_GET['sizeS25'];
$_SESSION['sizeM25'] = $_GET['sizeM25'];
Tom Spee
  • 9,209
  • 5
  • 31
  • 49
  • Yes you could do it this way, but if you only want to save the values to Session, you should just POST the values instead of redirecting the page and accessing them through GET – Jay S. Dec 20 '14 at 22:30