-2

I tested my update.php file and it works perfect and there is not any error when i checked my script via console . Only problem in here . Ajax can't send values "id" "comment_area" to update.php file . What is the mistake in here ?

<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.6.2.min.js">
$(document).ready(function() {
  $("#b_news").submit(function(evt) {
    evt.preventDefault();

    var text = $('#breaking_news_text').val();
    var id = 21;
    $.ajax({
      type: "POST",
      url: "update.php",
      data: {
        comment_area: text,
        id: id
      },
      success: function() {
        alert("sucess");
      }
    });
  });
});



</script>

<form id="b_news" method="post" action="">
  <div>
    <div>
      <textarea id="breaking_news_text" class="breaking_news_text" rows="6" cols="50" placeholder="Add text here..." required></textarea>
    </div>
  </div>
  <div>

    <input type="button" id="save" value="Save Changes" />

  </div>
</form>

<?php 
include("./inc/connect.inc.php");

$id=$_POST['id'];
$update = $_POST['comment_area'];
$sql = "update comments set comment_area='$update' Where id='$id'";
$result = mysqli_query($mysqli, $sql)or die("error");

     


?>
Jonathan
  • 10,936
  • 8
  • 64
  • 79
anor
  • 43
  • 1
  • 7
  • 1
    show the php code. done any basic debugging? maybe there's a redirect and your script is being called as a GET, and the post values are lost. – Marc B Jul 13 '16 at 18:01
  • `var_dump($_SERVER['REQUEST_METHOD'], $_POST)` and see what comes up. And note that you're vulnerable to [sql injection attacks](http://bobby-tables.com). – Marc B Jul 13 '16 at 18:05
  • Try to debug by putting this in your PHP script, `var_dump($_POST)` and make sure you see those two params. – odannyc Jul 13 '16 at 18:05
  • try sending your data serialised. `data: comment_area=${text}&id=${id}` – Uzi Jul 13 '16 at 18:06
  • Just FYI, your PHP file is very insecure. What if I sent you a POST request with an `id` of `' OR ''='`? – gen_Eric Jul 13 '16 at 18:06
  • string 'GET' (length=3) array (size=0) empty – anor Jul 13 '16 at 18:08
  • i know it is insecure . I just practicing ajax . – anor Jul 13 '16 at 18:08
  • There doesn't seem to be a way to submit the form. `` has no default action and won't fire the `submit` handler. – showdev Jul 13 '16 at 18:09
  • @Uzi — Manually serializing the data is simply error prone code that is harder to read. Don't do that. – Quentin Jul 13 '16 at 18:12
  • what should i do to submit it – anor Jul 13 '16 at 18:13
  • enter key didn't work . What i need to do . What ashould i change in html file . – anor Jul 13 '16 at 18:16

2 Answers2

0

Seems to me that your button simply can't submit the form because of its type. Try to change the type attribute of the button to submit this way :

<input  type="submit" id="save" value="Save Changes"/>
showdev
  • 28,454
  • 37
  • 55
  • 73
Vatre
  • 21
  • 5
0

You have two problems.

First you can only have one script per script element. The script can either be referenced by the src attribute or it can be between the start and end tags.

If you try to do both, as you are here, then only the src will be respected.

Use separate <script> elements for your two scripts.


Second, you have no way to trigger the submit event. The submit event will trigger when the form is submitted, but you can't do that from a textarea or a button. Replace the button with a submit button.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Forms can be submitted without any buttons present though. Try selecting the input field & hitting enter in [this fiddle](https://jsfiddle.net/xwLsuapj/). – Siguza Jul 13 '16 at 18:16
  • 2
    Isn't that why he mentions the `textarea`? – Jonathan Jul 13 '16 at 18:17
  • 1
    @Siguza — They can, but as I said in the answer, the form in the question only has a textarea and a button. It doesn't have a text input. – Quentin Jul 13 '16 at 18:17
  • it gives error if i remove src . – anor Jul 13 '16 at 18:20
  • @anor — What error? Is it complaining that `$` isn't defined? That's because you need to load jQuery before running your own script. Previously you were loading jQuery and not running your own script at all. You need two script elements as I said in the answer. – Quentin Jul 13 '16 at 18:23
  • ah thank you so much guys it works perfect – anor Jul 13 '16 at 18:24
  • 1
    what a stupid mistake took my hours to fix . <3 all – anor Jul 13 '16 at 18:24