2

This might be a stupid problem but i'm new to this (this is a homework ^^) and i can't find a solution :) i have a .php file with an html form plus some php code to execute a query and insert the values from the form in my DB. And it works, but every time the page is loaded the php code is executed and this insert in the DB a "blank" line, because obviously the form was not filled yet. This is the code

<html>
<head>
  <meta charset="utf-8">
  <meta name="generator" content="AlterVista - Editor HTML"/>
  <title></title>
</head>
<body>

<form action="myPage.php" method="post"> 
ID: <input type="text" name="id" /> <br /> 

<input type="submit" name="Submit" value="Go" /> <br />
</form>

<?php
$user = "DB";
$password = "";
$host = "";
$database = "my_DB";
$connessione = mysql_connect($host, $user, $password);

@mysql_select_db($database, $connessione) or die( "Unable to select database");

$id = $_REQUEST['id'];
$query = "INSERT INTO myTable (ID) VALUES ('".$id."')";
mysql_close();
?>

</body>
</html>

Is there a way to execute the php code only once the "Go" button on the form is executed?

4
  • That's exactly what's supposed to happen: php working before page being rendered Commented Feb 17, 2014 at 16:58
  • yeah i supposed xD, but there's a way to execute a piece of php code just "after" something happens? (like completion of form) Commented Feb 17, 2014 at 16:59
  • Check if $_POST['Submit'] isset, for example Commented Feb 17, 2014 at 17:01
  • you could use a die() statement to stop the code from executing again also it is advisable to keep your processing scripts(connection,insert) in a sepearate file Commented Feb 17, 2014 at 17:04

3 Answers 3

0

Try:

if(isset($_POST['Submit']))  {
    $user = "DB";
    $password = "";
    $host = "";
    $database = "my_DB";
    $connessione = mysql_connect($host, $user, $password);

    @mysql_select_db($database, $connessione) or die( "Unable to select database");

    $id = $_REQUEST['id'];
    $query = "INSERT INTO myTable (ID) VALUES ('".$id."')";
    mysql_query($query, $connessione);
    mysql_close();
}
Sign up to request clarification or add additional context in comments.

2 Comments

tried it but never execute the code even after the submit button is pressed!should i change other code?
EDIT: now it worked i wrote "Submit" instead of "submit"
0

PHP will work before the page is rendered. You need to set up a condition to stop the PHP you don't want running until you submit the form.

if(isset($_POST['myform'])) {
    // process the form
}else{
    // html for form goes here
}

Hope that helps.

Comments

0

Assuming the form points to the script itself, there are numerous options :) Among others:

This first example just checks if a form was posted. If a normal (GET) request is received, it will do nothing, because it will not fall into your if-clause

// your form here
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    // your php code
}

And this example checks if a variable with the name 'Submit' has been posted, and if so, if it has the value 'Go' in it. It is a slightly stricter check, but in your current example behaviour is exactly the same (so you can pretty much choose which one you like most ;))

// your form here
if(array_key_exists('Submit', $_POST) && $_POST['Submit'] == 'Go') {
    // your php code
}

1 Comment

i've tried both. The first still execute, the second never execute ;( i pasted it just after <?php if(.. and closed here }?>

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.