ajax update php variable from input box without submitting [closed]
Frage
So i am trying to type something in input box which i want to be dynamically stored in a variable. And then there is a hyperlink next to a table of records which will take this variable(.php?S=$inputboxvariable) and go to next php page which will execute a sql statement using this variable's value.
I do not want for submission , _GET or _POST or even SESSION variable in this.
Please help. I know coding would help but i dont know how, just a basic code would suffice.
Thanks
Lösung
Alright I get what your asking for now. The javascript below will make a call from your page to an external script (ajax/myscripthere.php) which will handle the SQL.
In the php script, we simply handle our SQL like we normally would if this was not an AJAX call.
Please accept answer if this is correct to your expectations :)
javascript:
<script>
var xmlHttpReq = false;
// Set ID Equal to your input box ID
input_box_value = document.getElementById("inputboxvariable").value;
if(window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
else if(window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
// Set filename equal to your PHP script
self.xmlHttpReq.open('POST', "ajax/myscripthere.php", true);
self.xmlHttpReq.onreadystatechange = function()
{
if(self.xmlHttpReq.readyState == 4)
{
// Update is complete
}
};
self.xmlHttpReq.send("value="+input_box_value);
</script>
PHP:
<?php
if(isset($_POST['value']))
{
$conn = new mysqli("host", "username", "password", "db_name")
or die ('Error: '.mysqli_connect__error());
// Make Correct SQL Query (i dont know your DB)
$query = "UPDATE table_name SET col=".$_POST['value']." WHERE xxxxxx;";
$result = @$conn->query($query);
$conn->close();
?>
Andere Tipps
You dont want to use get, post, or session to pass the variable but your example uses this:
(.php?S=$inputboxvariable)
Which would be passing the variable using GET.
If you are truely trying to avoid using these,
Use a database to store the data.
Use a file to store the data.
Maybe you could tell me why you are avoiding get, post, or session variables and I will understand better what you are trying to accomplish?