Jump to content
Rayj00

Newbee Needs Help With Javascript/PHP/Mysql

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

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

PS - is this line correct?

     data: {streamId : streamId}
           

Seems like there s/b quotes in there; otherwise it simply looks like two varnames

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

So hold on for a bit.  I found something weird going on with my index page.

Share this post


Link to post
Share on other sites

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>

 

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

That was suggested to the OP days ago.  Apparently no desire to test it out.

Share this post


Link to post
Share on other sites

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:-(

Share this post


Link to post
Share on other sites

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.