Web Analytics Made Easy -
StatCounter help, simple php multiplication table won't output anything - CodingForum

Announcement

Collapse
No announcement yet.

help, simple php multiplication table won't output anything

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • help, simple php multiplication table won't output anything

    hello, i am new to php.

    I've been working on a simple multiplication table but it's not outputting anything.

    I know the php is correct but the html is not working.

    Where did i go wrong?


    Here is my code

    Code:
    <html>
    <head>
    <title>A Times Table Programme</title>
    
    <?PHP
        $times = 2;
    
    if (isset($_POST['Submit1'])) {
    
    	$start = $_POST['txtStart'];
    	$end = $_POST['txtEnd'];
    	$times = $_POST['txtTimes'];
    
    	
    	for($start; $start <= $end; $start++) {
    		$answer = $start * $times;
    		print $start . " multiplied by " . $times . " = " . $answer . "<BR>";
    	}
    	
    }
    
    ?>
    
    </head>
    
    <body>
    
    
    <FORM NAME = "frmOne" Method = "POST" Action = "times.php">
    
    Start Number: <INPUT TYPE = "Text NAME" name = "txtStart" value ="1">
    End Number:  <INPUT TYPE = "Text NAME" name = "txtEnd" value ="10">
    Multiply by: <INPUT TYPE = "Text NAME" name = "txtTimes" value = <?PHP print $times; ?> >
    <P>
    <INPUT TYPE = "Submit" value="submit it">
    <P>
    
    </FORM>
    </body>
    </html>
    please help

  • #2
    Use lower case.
    Don't put submit button in <p> tags and add a name to the button:
    Code:
    <html>
    <head>
    <title>A Times Table Programme</title>
    
    <?PHP
    $times = 2;
    if (isset($_POST['Submit1'])) {
    	$start = $_POST['txtStart'];
    	$end = $_POST['txtEnd'];
    	$times = $_POST['txtTimes'];
    	for($start; $start <= $end; $start++) {
    		$answer = $start * $times;
    		print $start . " multiplied by " . $times . " = " . $answer . "<BR>";
    	}
    }
    ?>
    </head>
    
    <body>
    <form name = "frmOne" method = "POST" action = "times.php">
    Start Number: <input type = "Text NAME" name = "txtStart" value ="1"><br>
    End Number:  <input TYPE = "Text NAME" name = "txtEnd" value ="10"><br>
    Multiply by: <input type = "Text NAME" name = "txtTimes" value = <?PHP echo $times; ?> ><br>
    [COLOR="#FF0000"]<input type = "submit"[U] name="Submit1[/U]" value="submit it">[/COLOR]
    </form>
    </body>
    </html>
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

    Comment


    • #3
      1) You are doing the print inside the <head>. It has to be inside the body. Move your <php> statements after the <body> instead.
      2) validate your input data ($_POST['txtStart'], $_POST['txtEnd'], $_POST['txtTimes']). Ensure they are numeric.
      3) the $_POST data are strings. After validation, convert them to numbers for the multiply (*) to work.

      Comment


      • #4
        Don't name the submit button unless you have multiple submit buttons - some browsers don't pass the value from the submit button even if it has a name.

        To test if the form has been submitted use:

        PHP Code:
        if($_SERVER['REQUEST_METHOD'] == 'POST'
        Don't assign a second name to the $_POST values - validate them and only move the values to a new name if they are valid.
        Stephen
        Learn Modern JavaScript - http://javascriptexample.net/
        Helping others to solve their computer problem at http://www.felgall.com/

        Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

        Comment

        Working...
        X