Rayj00 Posted June 23, 2019 Author Share Posted June 23, 2019 This is the $.ajax: $.ajax({ type: 'POST', url: 'connUpdate.php', data: {streamId : streamId} )}.done(function(){ alert('AJAX was successfull!'); alert('Data to the server'); }).fail(function(){ alert('There was some error performing the AJAX call!'); }); Quote Link to comment Share on other sites More sharing options...
Rayj00 Posted June 23, 2019 Author Share Posted June 23, 2019 And this is connUpdate.php: <?php error_reporting(E_ALL); ini_set('display_errors', '1'); $servername = "localhost"; $username = "root"; $password = "password"; $dbname = "WebRTCApp"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $stream1 = $_POST['streamId']; $sql = "UPDATE broadcast SET streamID='stream1' WHERE id=1"; if ($conn->query($sql) === TRUE) { echo "Record updated successfully"; } else { echo "Error updating record: " . $conn->error; } $conn->close(); ?> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 23, 2019 Share Posted June 23, 2019 OK - that is not what you first posted. Do you know IN FACT that connupdate is NOT getting called? And the query statement you just posted is incorrect. Try simply doing an echo at the beginning of the called script and then exit. Don't do anything else. Then in your caller, show the message returned from that exercise and prove that the ajax call is actually happening. You have me confused as to what's not working with your changes in posted code samples here. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 23, 2019 Share Posted June 23, 2019 PS - is this line correct? data: {streamId : streamId} Seems like there s/b quotes in there; otherwise it simply looks like two varnames Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 23, 2019 Share Posted June 23, 2019 PPS - if connupdate is an ajax-called-only script then the display_errors setting wont' work since you have no console/client to show messages on. Have you found the error_log file and checked it out lately? Quote Link to comment Share on other sites More sharing options...
Rayj00 Posted June 23, 2019 Author Share Posted June 23, 2019 So hold on for a bit. I found something weird going on with my index page. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 23, 2019 Share Posted June 23, 2019 Later. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 23, 2019 Share Posted June 23, 2019 Try this <?php require 'db_inc.php'; $db = pdoConnect('test'); /*** INITIALISE TABLE - only required once *********************************************************** $db->exec("CREATE TABLE IF NOT EXISTS broadcast( id int not null primary key, streamid varchar(20)) "); $db->exec("INSERT IGNORE INTO broadcast VALUES (1, '0')"); */ // // HAS AN AJAX POST BEEN SENT? // if (isset($_POST['streamid'])) { try { $stmt = $db->prepare("INSERT INTO broadcast (id, streamid) VALUES (1, ?) ON DUPLICATE KEY UPDATE streamid = VALUES(streamid) "); $stmt->execute( [ $_POST['streamid'] ] ); exit($_POST['streamid']); } catch (Exception $e) { exit("ERROR"); } } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="generator" content="PhpED 18.0 (Build 18044, 64bit)"> <title>Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> function randomToken() { return Math.random().toString(36).substr(2) } $().ready( function() { $("#btnSend").click(function() { var tok = randomToken() $.post ( "", // blank - calls self {"streamid" : tok }, function (resp) { alert(resp) }, "TEXT" ) }) }) </script> </head> <body> <button id="btnSend">Send Stream ID</button> </body> </html> Quote Link to comment Share on other sites More sharing options...
Rayj00 Posted June 23, 2019 Author Share Posted June 23, 2019 So one thing I forgot....I already had a submit button. It's used to initialize the broadcaster stream and start the broadcast. <button onclick="startPublishing()" class="btn btn-info" disabled id="start_publish_button">Start Broadcasting</button> So how would your <button id="btnsend">Send Stream ID</button> integrate with what I have? Or can I just insert your code after the startPublishing() function? Quote Link to comment Share on other sites More sharing options...
Barand Posted June 23, 2019 Share Posted June 23, 2019 9 minutes ago, Rayj00 said: Or can I just insert your code after the startPublishing() function? How should I know? That's your first mention of a startPublishing() function. The code I posted is a functioning, stand-alone script. Do with it what you want. Quote Link to comment Share on other sites More sharing options...
Rayj00 Posted June 23, 2019 Author Share Posted June 23, 2019 Appreciate your help. I only got more confused with your code...llike how does it call the php without a URL? I'll just keep digging I guess. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 23, 2019 Share Posted June 23, 2019 3 minutes ago, Rayj00 said: llike how does it call the php without a URL? As the comment in the code says - if URL is blank it calls itself. Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 24, 2019 Share Posted June 24, 2019 Javascript and Ajax run in the browser, so you need tools that give you some visibility into what is going on. Most developers use Chrome Dev tools integrated into Chrome. You right click and look at the console tab for errors, as well as the network tab for HTTP requests and responses. Getting familiar with those tabs and what you can do with them are essential for debugging ajax calls in a page as they will show you whether or not an ajax call is able to connect to its target, as well as the data sent and any response or errors that might occur. Here's a video I found that does pretty good job of showing how to use the network tab to debug an ajax call. Quote Link to comment Share on other sites More sharing options...
MPM Posted June 25, 2019 Share Posted June 25, 2019 What happens if you change what you posted on Sunday so the line shown here becomes the amedned code below: $stream1 = $_POST['streamId']; $stream1 = "test data; Load connUpdate.php directly in the browser. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 25, 2019 Share Posted June 25, 2019 That was suggested to the OP days ago. Apparently no desire to test it out. Quote Link to comment Share on other sites More sharing options...
MPM Posted June 26, 2019 Share Posted June 26, 2019 I know it was suggested but perhaps not as clearly as the OP needed so I thought I'd explicitly state it despite the fact they should have done it ages ago. Apparently the OP doesn't feel it was helpful from me either:-( Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.