Jump to content

Recommended Posts

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!');
                });
 

 

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();
?>

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.

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>

 

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?

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.