I have js file that takes user input and call php file on submit. The php file displays result of the entered data. I want to create "save button" to save these data in my database. "The save button should be in the php file"
I try for the button in my php file:
<?php
echo "<input type='button' value='Save Result'> </p>";
I try this code in the same php file to save the result:
$serverName = "Alaa";
$connectionInfo = array( "Database"=>"i2b2blast", "UID"=>"i2b2blast", "PWD"=>"demouser");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$sql = "INSERT INTO BlastQueryDim (Query_ID, QuerySeq) VALUES ('Query 2', 'q')";
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
I want when I click the button to perform the saving code.
What I try and done successfully is: creating a test button in js file and call php file onclick it. In this case, the result successfully saved in my database but, I want the save button to be in the php file.
Hope you getting my point. Thanks a lot.
Edit 1: One possible solution is to call another php file from my current php one. I don't like this because I should post a lot of parameters. Anyway, in the first php file:
echo "<form action='saveResult.php' method='post' name='Post'>";
echo "<input name='Save' type='submit' value='Save Result'> </p>";
in the second php:
<?php
if(isset($_POST['Save'])) // If the submit button was clicked
{
$serverName = "Alaa";
$connectionInfo = array( "Database"=>"i2b2blast", "UID"=>"i2b2blast", "PWD"=>"demouser");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$sql = "INSERT INTO BlastQueryDim (QueryID, QuerySeq) VALUES ('6', 'q')";
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
}
echo "<script>alert('Saved');</script>";
This work successfully but, it navigate the page to saveResult.php page. How can I execute the code of the second php "saveResult.php" in the background without redirect me?