Questions about Javascript, PHP and MySQL

Hello everyone, my name is Magnus I am stuck on an error about PHP, MySQL, and Javascript.
This is the code I have written on the file manager (I have already uploaded the jQuery file so that jQuery codes can be worked here):

    <script>
    $('button').on('click',function(event){
        var h=$('input').val();
        console.log(h);
    <?php
    $receiver=h;
    $link=mysql_connect('sql---.epizy.com','epiz_--------','-----------');
    if(!$link)
        die('failure');
    $db_selected=mysql_select_db('epiz_--------_username',$link);
    if(!$db_selected)
        die('failure:'.mysql_error());

    $sql = "INSERT INTO `receiver`(`id`, `name`) VALUES ('NULL',$receiver)";
    if ($link->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $link->error;
    }
    mysql_close($link);
    ?>
    })

    </script>`

This code is in the <script> tag and it has PHP codes inside it. This code always runs smoothly until I typed these lines of codes:
if ($link->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $link->error; }

The usage of these lines of codes are telling me is the data successfully stored in the database. I not only did not see the data on the target database but also could not run these programs(var h=$('input').val();console.log(h);) which will show something in the console by pressing F12. I have tried a lot of times but still could not achieve my goals(store the data into the database and notifying me). Would someone mind to teach me how to do this so that I can continue my work? Thanks for your help!

Can you explain what’ll this code do? Your code is completely wrong because:

  1. PHP Is Server side. Which means that cannot launch codes live, Unlike JS. That’s possible with ajax. Please put PHP codes in another file then connect to it with Ajax.
  2. What is $receiver=h;? PHP And JS variables are different! PHP is being launched before js, so it cannot read value of h. BTW I corrected this for you (Remember to put inside separate file!)
<?php
    $receiver=htmlspecialchars($_POST['h']);
    $link=mysqli_connect('sql---.epizy.com','epiz_--------','-----------');
    if(!$link)
        die('failure');
    $db_selected=mysqli_select_db('epiz_--------_username',$link);
    if(!$db_selected)
        die('failure:'.mysqli_error());

    $sql = "INSERT INTO `receiver`(`id`, `name`) VALUES ('NULL',$receiver)";
    if ($link->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $link->error;
    }
    mysqli_close($link);
    ?>

And js file:

<script>
$('button').on('click',function(event){
        var h=$('input').val();
        console.log(h);
$.ajax({
			type: "POST",
			data: {h: h},
			url: "YOUR PHP FILE",
			cache:false
		}).done(function(stuff){
			console.log(stuff);
		});
</script>
2 Likes

Dear BayoDino,
Thanks for your notice. Before I create this topic, I have tried to solve the problem by searching the internet and I noticed that some professionals said that the type of code I have written should be separated into two files. However, A website shows me that the program’s format like this:
<script> <?php ?> </script>
But I think that I had better create two programs like yours. Thanks for your help!

About this, I have entered a forum’s web page but I carelessly took the question’s code for reference. Anyway, thanks for your help again and apologizing for my carelessness.

This is the link I have used for using PHP to insert data into a MySQL Database:
https://www.w3schools.com/php/php_mysql_insert.asp

Best Regards,
Magnus

Hello everyone, my name is Magnus. I am stuck on a question by developing the website. I have found a useful program on the internet, but I did not know how to change the codes into my situation. My codes are shown here:(‘_____’ are the parts I do not know how to finish)

Html code:

<body>
    <input type='input' name='nick' id='nick'>
    <a href='test.php'>button</a>
    <script src="https://ajax.google.apis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script>
         _______________;
    </script>
</body>
PHP code (Although Javascript and PHP codes should not be put into the same program, it worked):
<script>
    var test=_____________;
</script>
<?php
echo"<script>document.writeln(test);</script>";
?> 
</body>

Would you mind to answer me how to fill in those blanks? Thanks for your help!

Links I have referenced:

The method used to pass the variable through javascript from one HTML page to another page:
https://stackoverflow.com/questions/27765666/passing-variable-through-javascript-from-one-html-page-to-another-page

The link proves that the PHP code works:
https://www.tutorialspoint.com/How-to-pass-JavaScript-variables-to-PHP

Is this a continuation of this topic?

Because my first thought is to repeat what @anon19508339 already said in the other topic:

2 Likes

Yes, this is a continuation of that topic, but I have used another method to try to solve the solution.

<script>
    var test=_____________;
</script>
<?php
echo"<script>document.writeln(test);</script>";
?> 
About these lines of programs, although I also know Javascript and PHP codes should not be put into the same program file and this code is not really transferring the variable form Javascript to PHP, it still worked. Therefore, I decided to try to pass the variable through Javascript from one HTML page to another page and referenced the link on the first post.

Also, my final is just transfer the Javascript variable to PHP. I have seen many solution on the web like using GET[]and POST[] method (This is is the method @anon19508339 used on another post), create a cookie and more. Is the GET[]and POST[] method most reliable? Then how can I know more about using GET[]and POST[]? If you do not mind, can you recommend me some websites that can let me learn how to use them? Thanks for your help so much!

Best Regards,
Magnus

If this is a continuation of the topic, let’s keep the post in that topic too. I’ve merged the topics now.

I personally don’t have a specific recommendation on tutorial sites. But any tutorial which can explain the basics of PHP and/or Javascript could do, and there are tons of tutorials available for it.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.