Jump to content

HTML form failing to execute php code


MyLifeForIre

Recommended Posts

when attempting to execute a form in HTML the resulting php page only echos the code instead of executing it. Any help is greatly appreciated

 

<div class="form-popup" id="myForm">
            <form action="../php/mtg2.php" class="form-container" target="_blank" id="MTG-form" >
                <h1>Give Us Your MTG Card Details</h1>
                
                    <p><label for="cardname"><b>Card Name:</b></label>
                        <input name="cardname" type="text" required placeholder="Archangel Of Thune" autocomplete="on" maxlength="30"></p>
                    <p><label for="cardcolor">Card Color:</label>
                        <input name="cardcolor" type="text" required placeholder="White/Black" autocomplete="on" maxlength="30"></p>
                    <p><label for="releaseyear">Card Release Year:</label>
                            <input name="lastname" type="text" required placeholder="Magic 14" autocomplete="on" maxlength="30"></p>
                    <p><label for="releasepack">Release Pack:</label>
                        <input name="releasepack" type="text" required placeholder="Guilds Of Ravnica" autocomplete="on" maxlength="30"></p>
                    <p><textarea name="comments" cols="36" rows="10" maxlength="150" id="comments" placeholder="Dont see one here thats hot or have a Release pack not listed? Let us know!"></textarea></p>
                        <input type="button" class="btn" value="Submit"
            
                <!-- this is the button for the add card form -->
            
                <button type="submit" class="btn" onClick="openForm">Submit</button>
                <button type="button" class="btn cancel" onClick="closeForm()">Close</button>
                  </form>
            </div>
            
              <script>
                  function openForm() {
          document.getElementById("myForm").style.display = "block";
        }

        function closeForm() {
          document.getElementById("myForm").style.display = "none";
        }
            </script>

Link to comment
Share on other sites

Does the php file your sending to start with a PHP opening tag? <?php

Are you using shorthand php openers? <? and have you enabled them in your PHP settings?

 

Link to comment
Share on other sites

23 hours ago, Funce said:

Does the php file your sending to start with a PHP opening tag? <?php

Are you using shorthand php openers? <? and have you enabled them in your PHP settings?

 

This is the start of my php page code

<html>
<body>

<h1>Thank you for your submission</h1>

<?php

//form data entry
$gamertag = $_POST['gamertag'];
$lname = $_POST['lastname'];
$fname = $_POST['firstname'];
$email = $_POST['email'];
//end form data entry

// echo statement to print form entry results
echo $gamertag;
echo "<br>";
echo "$fname $lname";
echo "<br>";
echo $email;
echo "<br>";

?>

Link to comment
Share on other sites

Just now, MyLifeForIre said:

im running the code from my webpage. so my local machine is the webserver? i guess im not sure

Webservers require specific set up, so you'd know if you had one.

You'll need a webserver to run PHP code. I recommend XAMPP to test locally as its essentially install and run. Make sure you put your files into its 'htdocs' folder, and you should be good to test. Access via 'localhost'

Link to comment
Share on other sites

  • 3 weeks later...
On 3/5/2020 at 9:12 PM, Funce said:

Webservers require specific set up, so you'd know if you had one.

You'll need a webserver to run PHP code. I recommend XAMPP to test locally as its essentially install and run. Make sure you put your files into its 'htdocs' folder, and you should be good to test. Access via 'localhost'

I finally got the PHP page to be responsive as i need it to be. the last issue im having is my form is not passing data to my database tables. i have the database set up and running, all the fields are set up as they should be and i do not get any errors when trying to submit data. it just doesn't show up

Link to comment
Share on other sites

The Following code is from one of my PHP forms. everything works up until what i have highlighted in orange. i have double checked my server variables and i am not getting any error codes, the data from my forms simply is not being passed to the associated database fields.

<html>
<body>
<link href="../css/PHP-BG.css" rel="stylesheet" type="text/css">
<h1>Thank you for your submission</h1>

<?php

//telling the time of day greeting
$time = date("H");

if ($time < "10") {
    echo "<h2>Good Morning Champion!</h2>" . "<br>";
} elseif ($time < "15") {
    echo "<h2>Good day Champion!</h2>" . "<br>";
} else {
    echo "<h2>Good Evening Champion!</h2>" . "<br>";
}
//end of time of day greeting code
    
//set all form entry data to blank    
$CharName = $CharHair = $EyeColor = $PriAttribute = $AltAttribute = $comments ="";

//form data entry
$CharName = $_REQUEST['CharName'];
$CharHair = $_REQUEST['CharHair'];
$EyeColor = $_REQUEST['EyeColor'];
$PriAttribute = $_REQUEST['PriAttribute'];
$AltAttribute = $_REQUEST['AltAttribute'];
$comments = $_REQUEST['comments'];
//end form data entry

// echo statement to print form entry results
echo $CharName;
echo "<br>";
echo "A Truly mighty Hero and worthy of your $PriAttribute Attribute. Thank you for sharing";
echo "<br>";

// This is the output that needs to be placed in the PHP files to initiate and insert data into existing tables

$servername = "xxxx";
$username = "xxxx";
$password = "xxxx";
$dbname = "xxxx";

// Create connection
$link = mysql_connect("xxxx", "xxxx", "xxxx", "xxxx");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// items need to reflect whats in database
$sql = "INSERT INTO CharBuild (CharName, HairColor, EyeColor, PriAttribute, AltAttribute)
VALUES ('Grognak', 'White', 'Ice Blue', 'Strength', 'Charisma')";

if(mysqli_query($link, $sql)){
    echo "Records inserted successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);

?> 

</body>
</html>

Link to comment
Share on other sites

So does your Database have the values 'Grognak', 'White', 'Ice Blue', 'Strength', and 'Charisma' in it? If it does, your script is currently working the way its written.

Your SQL query will need adjusted to include the variables rather than those hard coded strings, you've put in.

 

You might be interested in something called "Prepared Statements" for safe passing of data. Here's one of my examples I like to use:

<?php //This line is for highlighting on the forum, just make sure the code below is inside a php block

//PREPARED STATEMENTS - FUNCE
//INSERT EXAMPLE

$string_var = "String";
$double_var = 3.5;
$int_var = 45

$sql = "INSERT INTO table (column1, column2, column3) VALUES (?, ?, ?)";
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_bind_param($stmt, "sdi", $string_var, $double_var, $int_var);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);

Here's the Documentation on what I've used (Procedural Style is the one you want to look at)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...