0

I'm creating a PHP form for an RSVP, and I'm trying to declare multiple $_POST to 1 isset variable, but I keep getting errors in my code, this is what I'm trying to do:

    $attendance = isset( $_POST['template-contactform-event'] ) ? $_POST['template-contactform-event'] : '';

    $guestname1 = isset( $_POST['template-contactform-guestname1'], $_POST['template-contactform-guestname1r'] ) ? $_POST['template-contactform-guestname1'], $_POST['template-contactform-guestname1r'] : '';

So my error is occurring on the second declaration line $guestname1 I think its because of the second coma after the isset, but I'm not sure how I would declare this? The first line ($attendance) is what I'm trying to achieve but with two $_POST for 1 variable.

1
  • What is your error message? Also, try not to use ternary while debugging, you can always write a ternary afterwards Commented Feb 6, 2017 at 10:47

3 Answers 3

2

While isset() can take multiple parameters, the inline iif() cannot.

Try this instead, I concatenated the two parameters.

<?php

$attendance = isset( $_POST['template-contactform-event'] ) ? $_POST['template-contactform-event'] : '';

$guestname1 = isset( $_POST['template-contactform-guestname1'], $_POST['template-contactform-guestname1r'] ) ? 
                $_POST['template-contactform-guestname1'].' '.$_POST['template-contactform-guestname1r'] : '';
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help, this worked for me! Submitting this as answered as example was provided that works.
2

The first statement is right. But the second one has an error with the second comma.

If you take a look a the way ternary are supposed to be build:

$val = my_test?result_if_true:result_if_false;

You can do isset( $_POST['template-contactform-guestname1'], $_POST['template-contactform-guestname1r'] ) because it will return a boolean.

But not write : $_POST['template-contactform-guestname1'], $_POST['template-contactform-guestname1r'] has the second argument. Because ternary aren't expecting this comma.

If you want more informations I've found this article https://davidwalsh.name/php-shorthand-if-else-ternary-operators

1 Comment

Thanks for this +!
1

You are assigning 2 values to one variable, that doesn't work, you will have to use an array.

And also isset() isn't the way to check if a user has inputted values to a form, because if a user submits a blank form then isset($_POST['smth']) will return true because the indexes are present in the superglobal $_POST and its value is set to empty string. Use empty() instead and an array to store 2 values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.