Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As many have already mentioned there are a few errors in your code. My solution to your question offers a different approach.</p> <p>Rather than calling the InsertData() method you can do something else in the one php document.</p> <p>This is done by using an if/else statement, assigning 'submit' to the name value of the submit button as well as the special PHP variable $_SERVER['PHP_SELF'].</p> <p>First, the server checks if the form has been submitted.<br /> When the page is first loaded this will return false and the form is displayed for them to fill out</p> <p>When the user clicks the submit button the form reloads the page and runs the PHP script again. This time the if statement returns true as the form HAS been submitted so it executes the part of the script that inserts the data in MSSQL and displays the successful message.</p> <p><strong>Check out the code below:</strong></p> <pre><code>&lt;?php if (isset($_POST['submit'])){ //connect to the database $link = mssql_connect('db','123','test'); //display error if database cannot be accessed if (!$link || !mssql_select_db('php', $link)) { die('Unable to connect or select database!'); } //assign form input to variables $txtName = $_POST['txtName']; $txtWebsite = $_POST['txtWebsite']; //SQL query to insert variables above into table $sql = " INSERT INTO customer ([Name],[Website])VALUES('$txtName','$txtWebsite')"; $result = mssql_query($sql, $link); //if the query cant be executed if(!$result) { echo mssql_error(); exit; } // close the connection mssql_free_result($result); mssql_close(); echo "Data successfully inserted"; } else { ?&gt; &lt;form name="input" action="$_SERVER['PHP_SELF']" method="POST"&gt; &lt;table border="0" cellpadding="0" cellspacing="0" width="100%"&gt; &lt;tr&gt; &lt;td colspan="3" height="10" valign="top" width="98%"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td width="22%"&gt;Name:&lt;/td&gt; &lt;td width="60%"&gt;&lt;INPUT type="text" name="txtName" id="txtName" Width="200px" MaxLength="30" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Company Website:&lt;/td&gt; &lt;td&gt;&lt;INPUT type="text" name="txtWebsite" id="txtWebsite" width="200px" MaxLength="200" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="button" name="submit" value="submit" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;?php } ?&gt; </code></pre> <p>Give that a try. There's some great PHP tutorials that go over the fundamentals here: <a href="http://devzone.zend.com/article/627" rel="nofollow">http://devzone.zend.com/article/627</a></p> <p>Hope that all helped.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload