1

I am having an issue where i get a timeout if i run a certain php script with parameters sent from a Jquery Ajax reequest. However if i simply run this script with no parameters I.E just run it on its own its fine.

The php file the Ajax request is in is noteContent.php noteContent.php:

<script type="text/javascript">

    // Submit generalNote to the database

function submitNoteText()
{
    var noteid = <?php if(isset($_POST['noteid'])){ echo $_POST['noteid'];} ?>;
    var notetext = $("#ta1").val();

    var dataString = 'noteid1=' + noteid + '&notetext1=' + notetext;

    if(noteid == ''||notetext == '')
    {
        alert("NoteID or Text is blank");
    }
    else
    {   
        $.ajax({
            type: "POST",
            url: "submitNoteText.php",
            dataType: 'json',
            data: dataString,
            cache: false,
            success: function(result){
                alert("Success");
            }
        });
    }
    return false;
};

</script>

The script its trying to run submitNoteText.php:

<?php include 'connectionDetails.php'; ?>
<?php
if (isset($_POST['noteid1'], $_POST['notetext1'])) 
{
    var_dump($_POST['notetext1']);
    $noteid2 = $_POST['noteid1'];
    $notetext2 = $_POST['notetext1'];
    $stmt = "UPDATE Notes SET Note = '?' WHERE NoteID = ?";
    $params = array($noteid2, $notetext2);
    $stmt = sqlsrv_query($conn, $stmt, $params);
    if ($stmt === false) 
    {
        die( print_r(sqlsrv_errors(), true));
    }
}
else
{
    echo "No Data";
}
?>

they are all connected with an include to connectionDetails.php(which has never returned an error before):

<?php
$myServer = "Test";
$connectionInfo = array('Database' => 'Test', 'UID' => 'Test', 'PWD' => 'test');

//connection to the database
$conn = sqlsrv_connect($myServer, $connectionInfo)
  or die("Couldn't connect to SQL Server on $myServer"); 

//Test connection to server
// if ($conn) 
// {
//     echo "connection successful";    # code...
// }

?>

<?php

//Defining my queries
$getNotes = "SELECT NoteID, NoteName, Note FROM Notes ORDER BY NoteName ASC";
$getTemplateNotes = "SELECT TemplateNoteID, TemplateNoteName, TemplateNote FROM TemplateNotes ORDER BY TemplateNoteName ASC";
$getReplaceVariables = "SELECT ReplaceVariableID, ReplaceVariableName, ReplaceVariableNote FROM ReplaceVariables ORDER BY ReplaceVariableName ASC";

$resultNotes = sqlsrv_query($conn, $getNotes);
$resultTemplate = sqlsrv_query($conn, $getTemplateNotes);
$resultVariables = sqlsrv_query($conn, $getReplaceVariables);





if( $resultNotes === false) 
{
    die( print_r( sqlsrv_errors(), true) );
}
if( $resultTemplate === false) 
{
    die( print_r( sqlsrv_errors(), true) );
}

if( $resultVariables === false) 
{
    die( print_r( sqlsrv_errors(), true) );
}

?>

When i click the button to run the request i get this: enter image description here

All the text to be submitted is in the params: enter image description here

user7409253
  • 103
  • 1
  • 9
  • 1
    in "submitNoteText.php", this line : $stmt = "UPDATE Notes SET Note = '?' WHERE NoteID = ?"; -> have you tried to remove single quote around '?' ? $stmt = " UPDATE Notes SET Note=? WHERE NoteID=? "; – OldPadawan Mar 31 '17 at 11:35
  • but since $notetext2 is a string wouldn't it need to be in quotes? – user7409253 Mar 31 '17 at 11:39

1 Answers1

0

Ajax data parameter should be an array :

$.ajax({
    type: 'POST',
    url: "your urls",
    data: {
     id: youridval,
     anothervar: anothervalvalue
    },
    beforeSend: function() {

    },
    complete: function() {

    },
    success: function(_result) {

    }
});
kanzari
  • 87
  • 4
  • so could i do it like the top answer in this? http://stackoverflow.com/questions/8890524/pass-array-to-ajax-request-in-ajax – user7409253 Mar 31 '17 at 11:22
  • you can do but in that approach you are sending an array to php, the one i adviced is sending seperate values that you can catch in php using _POST['id'] and _POST['anothervar'], it's depend on your logic. – kanzari Mar 31 '17 at 11:28