0

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?

1 Answer 1

1

you have to create a form for your button.

<form method="post" name = "post">
 //rest
</form>

then in php code use this

if($_SERVER['REQUEST_METHOD'] === 'POST'){

 //execute code when button is clicked

}

Important

a form can be submitted in other ways (such as pressing Enter in a text box). So Checking for a submit button field in the request is not that reliable.

$_POST By just using this expression you can assert that:

1.The form is submitted via POST

2.At least one field has been submitted

that's why i prefer request_method instead of checking

<?php
if(isset($_POST["submit"]))
{
//php code
}
Sign up to request clarification or add additional context in comments.

4 Comments

if($_SERVER['REQUEST_METHOD'] === 'POST'), POST refer to form name or form method? because i have different form post in the same php page
in this case the method post/get we choose post
The result is saved to my database but, I did not click save button!
@Alaa it should only send information when button is clicked. I think rest of code might be wrong?

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.