PHP variables and form tags


#1
I am at my wit's end. You can see what all I've tried. How do I send my $epub and $mobi variables on to the next page?

<?php
 if ($ebook == "y")
 		{		
		echo "<br>$epub"; // just to prove these two variables are set
		echo "<br>$mobi"; // which they are
		
		 echo "<br><br>Order ebook from publisher:";
		 echo "<form action='html%20backup/ebook_order_form.php' method='post'>";
		 
		 /*
		 echo "<input type='radio' name='booktype' value='".'$epub'."' checked>ePub<br>";
  	 echo "<input type='radio' name='booktype' value='".'$emobi'."'>Mobi<br>";
		 */  //shows $emobi
		 
		 /*
		 echo "<input type='radio' name='booktype' value='\'$epub\' checked>ePub<br>";
  	 echo "<input type='radio' name='booktype' value='\'$emobi\'>Mobi<br>";
		 */ // shows \
		 
		 /*
		 echo '<input type="radio" name="booktype" value="$epub" checked>ePub<br>';
  	 echo '<input type="radio" name="booktype" value="$emobi">Mobi<br>';
		 */ //shows $emobi
		 
		 /*
 		 echo '<input type="radio" name="booktype" value=$epub checked>ePub<br>';
  	 echo '<input type="radio" name="booktype" value=$emobi>Mobi<br>';
		 */ //shows $emobi
		 
		 /*
  	 echo "<input type='radio' name='booktype' value='echo \"$epub\";' checked>ePub<br>";
  	 echo "<input type='radio' name='booktype' value='echo \"$emobi\";'>Mobi<br>";
		 */ // shows echo "";
		 
		 
		 /*
     echo "<input type='radio' name='booktype' value=$epub checked>ePub<br>";
  	 echo "<input type='radio' name='booktype' value=$emobi>Mobi<br>";
		 */ // shows nothing
		 
		 /*
		 echo "<input type='radio' name='booktype' value='$epub' checked>ePub<br>";
  	 echo "<input type='radio' name='booktype' value='$emobi'>Mobi<br>";
		 */ //shows nothing
		 
		 /*
		 echo "<input type='radio' name='booktype' value='"$epub"' checked>ePub<br>";
  	 echo "<input type='radio' name='booktype' value='"$emobi"'>Mobi<br>";
		 */ //shows nothing
		 
		 /*
		 echo "<input type='radio' name='booktype' value='<?php echo "$epub";?>' checked>ePub<br>";
  	 echo "<input type='radio' name='booktype' value='<?php echo "$emobi";?>'>Mobi<br>";
		 */ //breaks page
		 
		 /*
		 echo "<input type='radio' name='booktype' value=<?php echo "$epub";?> checked>ePub<br>";
  	 echo "<input type='radio' name='booktype' value=<?php echo "$emobi";?>>Mobi<br>";
		 */ //breaks page
		  
		 echo "<input type='submit' value='Submit'>";
		 echo "</form>";
 
}

?>

#2

Lets start with the REAL problem you need to solve instead of your attempt at solving it. Describe the overall task at hand that code needs to be written for.

It would seem you want a user to be able to order an ebook. If so, where is the book data coming from?


#3

It’s to order books, yes. The files exist on the server. I just am trying to pass a number in that variable to pull the book info from a database. The number is set on one page and then sent to another via the form. Only it doesn’t work.


#4

Do you have the code on Github or some other repo? I would like to see everything you are working with. The task is trivial but your attempt has complicated it.

EDIT:
Here is a basic example

<?php
$error = [];
$show_error = false;

if ($_SERVER['REQUEST_METHOD'] == 'POST')
	{
	if (empty($_POST['booktype']))
		{
		$error[] = 'Must select book Type';
		}

	if ($error)
		{
		$show_error = true;
		}
	  else
		{
		// Process Order
		echo 'Order Proccessed Here for ' . $_POST['booktype'];
            // Do some header redirect or something
		}
	}

if ($show_error)
	{
	// Display Errors
	print_r($error);
	}
?>
<form method="post">
<input name="booktype" type="radio" value="Book A">
<input name="submit" type="submit" value="Submit">
</form>

#5

It’s on a customer’s website. The variables $epub and $mobi are set before this code snippet. All I need to know is the syntax for passing the variable to another webpage. The other page accesses $_POST[booktype] and it is empty.

pseudocode:

x=1
y=2

booktype value= 1 or 2, depending on which radio choice is selected

second page should be able to access $_POST[booktype] and see if it contains 1 or 2.

The code you wrote is fine, but insufficient. It does not pass $_POST[booktype]. It passes “Book A”.

You are providing an answer to a different question.


#6

So change Book A to a 1 or 2 then…

If you have a proper form, the form results will be available in the POST array.


#7
I can't send a 1 or a 2. I don't know what is in that variable at the time the form is created. The variable sent depends on which radio box the user checks. So the item $_POST[booktype] will contain the value of one of two variables. Either $epub OR $mobi. 

I KNOW there is a problem with the form. That's the whole reason I'm asking for help! Am I not allowed to set a radio choice to a variable? Am I not allowed to put a variable into the $_POST[booktype]? 

How do I get the value contained in either $epub or $mobi into $_POST[booktype]?

Here is stripped down code with all my errors edited out:
This syntax for the radio buttons does not work.

<?php
 if ($ebook == "y")
 	{
         echo "<form action='html%20backup/ebook_order_form.php' method='post'>";
	 echo "<input type='radio' name='booktype' value=<?php echo "$epub";?> checked>ePub<br>";
  	 echo "<input type='radio' name='booktype' value=<?php echo "$emobi";?>>Mobi<br>";
	 echo "<input type='submit' value='Submit'>";
	 echo "</form>";
        }
?>

#8

You are trying to echo inside an echo. Dont.

CORRECTED CODE

 <?php
    $ebook = 'y';
    $epub  = 1;
    $emobi = 2;

     if ($ebook == "y")
       {
        echo "<form method='post'>";
        echo "<input type='radio' name='booktype' value='$epub' checked>ePub<br>";
        echo "<input type='radio' name='booktype' value='$emobi'>Mobi<br>";
        echo "<input type='submit' value='Submit'>";
        echo "</form>";
        }

    print_r($_POST);
    ?>

#9

I KNOW! I already said it doesn’t work! WHAT WILL WORK???


#10

Can someone PLEASE give me the syntax to fill radio values with variables and then pass them in the POST form?


#11

What is the problem? I just posted the CORRECTED CODE and told you EXACTLY what was wrong with what you posted. Did you even try to run it? Did you look to see what I changed?


#12

Yes I did. And did you look at my original commented code to see I have already TRIED that syntax?

/*
		 echo "<input type='radio' name='booktype' value='$epub' checked>ePub<br>";
  	 echo "<input type='radio' name='booktype' value='$emobi'>Mobi<br>";
		 */ //shows nothing

#13

Did you run my corrected code or not. It works. If it doesnt when you implement it in your script then your variables are not set. Your code also has no checks to make sure they are set. View the page source. Do you see values in the radio buttons? If they are missing, your problem is before this code.

Since you have refused to post the code that shows how they get set, neither I nor anyone else can help any further. I gave you a standalone corrected code block that works as it should. This is not a complicated task.


#14

THANK YOU! I finally got it to work. Please excuse my extreme frustration.