1

I've seen many js var to php question asked but none seem to help me as its either to complicated or doesn't fit my requirements. I'm looking to transfer three variable in js called title, body and author. Body being a very long string therefor I cannot place in URL. I am looking for the simplest Ajax code possible that follows this: js var already defined -> ajax -> php var Here is the code I've got so far:

<script>
var title;
var body;
var author;
function post(){
    title = document.getElementById("title").value;
    body = document.getElementById("body").value;
    author = document.getElementById("author").value;
}
    //Insert ajax code here
<?php


$title;
$body;
$author;
$post = "INSERT INTO post (title, body, author) VALUES ($title,$body,$author)";
$sql = mysqli_query($conn,$post);
?>
</script>

Thank you in advance!

6
  • "//Insert ajax code here" creates so many questions, it should not be there.... Commented Nov 24, 2016 at 7:15
  • Add your ajax code here, What you tried ? Commented Nov 24, 2016 at 7:17
  • Ajax code cannot be under PHP. Commented Nov 24, 2016 at 7:17
  • Check this answer. Commented Nov 24, 2016 at 7:20
  • @Cpt0Teemo Do you want variable in same file ? Commented Nov 24, 2016 at 7:22

2 Answers 2

3

You can do this way simple

<script>
var title = document.getElementById("title").value;
var body = document.getElementById("body").value;
var author = document.getElementById("author").value;

$.ajax({
    type: 'POST',
    url: 'insert.php',                
    data: {title:title,body:body,author:author},
 success: function(data) {

 }  
})
</script>

inside, insert.php file

<?php
$title = $_POST['title'];
$body = $_POST['body'];
$author = $_POST['author'];
$post = "INSERT INTO post (title, body, author) VALUES ($title,$body,$author)";
$sql = mysqli_query($conn,$post);
?>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks to this my JS function post() works but the Ajax doesn't seem to work as it doesn't update the database. I've tried to put a JS alert inside insert.php to test but it doesn't activate. Thank you for help!
you can't put a JS alert inside insert.php, you can print $_POST value, it will get your values. your ajax
sorry, it is unanswered, you can't put a JS alert inside insert.php,your ajax is not working, I think your script not added, please add it "<script type="text/javascript" src="code.jquery.com/jquery-2.2.4.min.js"></script>" and you can print $_POST value, it will get your values. I hope it help.
Sorry I explained this incorrectly. I put an alert inside a script in the insert.php and have put an alert in the function(data) in ajaz which activetes so I do not know what is wrong with code> It would probably be connection between both documents. Thank you!
@NidhiBarhate Gud Keep it up
0

$.post('url', {title: title, body: body, author: author})

Then use $_POST global variable to get the data you wanted.

$title = $_POST['title'];
$body = $_POST['body'];
$author = $_POST['author'];

More info on ajax post shorthand here:
https://api.jquery.com/jquery.post/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.