1

I'm a new to AJAX so I'm sorry if the solution is obvious. I must send the dates to a MySQL table without refreshing the page and I currently have a problem doing this.

The code gives me the following error:

" Notice: Undefined index: post in inserisci.php on line 6"

FORM

 <form id="form" class="form-horizontal shadow-z-1" action="includes/inserisci.php" method="post">
        <fieldset>
            <legend style="text-align: center; position: relative; top: 8px;">Inserisci un Post</legend>
            <hr>
            <div class="form-group">
                <label for="inputTitle" class="col-lg-2 control-label">Titolo</label>
                <div class="col-lg-10">
                    <input type="text" class="form-control" id="inputText" name="titolo" placeholder="Scrivi qui il Titolo">
                </div>
            </div>
            <div class="form-group">
                <label for="textArea" class="col-lg-2 control-label" name="testo">Messaggio</label>
                <div class="col-lg-10">
                    <textarea id="messaggio"></textarea>
                </div>
                </div>

               <div class="form-group is-empty is-fileinput">
                    <label for="inputFile" class="col-md-2 control-label">File</label>

                    <div class="col-md-10">
                      <input type="text" readonly="" class="form-control" placeholder="Browse..." pmbx_context="19E61A0C-3526-4E51-8535-935982C4C335">
                      <input type="file" id="inputFile" multiple="" pmbx_context="1D2BCAEA-08CC-476A-8F4A-EF6BD51B9102">
                    </div>
                  <span class="material-input"></span></div>

            <div class="col-md-10 col-md-offset-2">
                      <button type="button" class="btn btn-default" onclick="document.getElementById('modalposta').style.display = 'none';">Cancel</button>
                      <button type="submit" class="btn btn-primary" onClick='send(); return false;' >Submit</button>
                    </div>
        </fieldset>
    </form>

INSERISCI.JS

  function send(){
var content = $.ajax({
        type: "POST",
        url: "index.php",
        data: {titolo:titolo & post:testo}           
  })
  .done(function() {
    alert( "success" );
  })                                                 
  .fail(function() {
    alert( "error" );
  })                                                
  .always(function() {
    alert( "complete" );
  });
}

INSERISCI.PHP

<?php

include('../core.php');

$titolo=$_REQUEST ['titolo'];
$post=$_REQUEST ['post'];

$sql = mysql_query("");

mysql_close();

?>

Other question. I need to reload the content of this without refresh the page.

INDEX.PHP

<?php $sql = mysql_query("")  or die ("Nessun errore");
    if (mysql_num_rows($sql) > 0)
    {


        while ($row = mysql_fetch_assoc($sql))
        {
            echo '

            <article class="flipper white-panel">
        <a class="flipcard flip" style="
        position: absolute;
        right: 0px;
        top: -10px;
        z-index:1;
    "><i class="material-icons" style="font-size: 40px;">bookmark</i></a>
        <div class="articolo">
        <img src="http://i.imgur.com/sDLIAZD.png" alt="">

            <h4><a href="#">'.$row ['titolo'].'</a></h4>
            <p>'.Markdown($row ['contenuto']).'</p>
             <div class="pull-right">
                            <span class="label label-default">alice</span>
                            <span class="label label-primary">story</span>
                            <span class="label label-success">blog</span>
                            <span class="label label-info">personal</span>
                            <span class="label label-warning">Warning</span>
                            <span class="label label-danger">Danger</span>
                          </div>
                          <hr>
                          </div>
        <div class="commenti">
            <ul class="comment-list">
                        <li>
                            <div class="comment-img">

                            </div>
                            <div class="comment-text">

                            </div>
                        </li>
                    </ul>
                    <input type="text" class="form-control inputcomment" placeholder="Lascia un Commento.."/>
                  </div>
      </article>
         ';
        }


    }

Thanks everyone.

3 Answers 3

3

Notes:

  1. Please do not use mysql_* functions. They are deprecated.
  2. Your code is vulnerable to SQL Injection Attacks. Please make sure you address them.

That's a syntax error. That's not a valid JavaScript Object. Change your data object to:

data: {titolo:titolo, post:testo}

Also you need to close the $.ajax before you are using .done.

  var content = $.ajax({
        type: "POST",
        url: "index.php",
        data: {titolo:titolo & post:testo}           // Should be closed here.
  })
  .done(function() {
    alert( "success" );
  })                                                 // Should not be closed. Remove ;
  .fail(function() {
    alert( "error" );
  })                                                 // Should not be closed. Remove ;
  .always(function() {
    alert( "complete" );
  });
  // });                                                Remove this!

If nothing works out, please change these lines:

$titolo = $_POST['titolo'];
$post = $_POST['post'];

And wait, did you get the values for those two?

var titolo = $('input[name="titilo"]').val();
var testo = $('input[name="testo"]').val();
Sign up to request clarification or add additional context in comments.

10 Comments

Same problem. Undefined index: post in inserisci.php on line 6
You aren't closing the line: $.ajax({ type: "POST", url: "index.php", data: {titolo:titolo & post:testo}) .done Close it using ) before .done.
@Master-Antonio There are lot of problems. Please address them. See the above answer.
@Master-Antonio Did you see the above answer?
@Master-Antonio Updated again for an ultimate solution.
|
1

This is because the data you are passing in your AJAX request is not a valid javascript object and you are not getting the variables from the form:

data: {titolo:titolo & post:testo}

You should instead do something like:

var titolo = $('form input[name="titolo"]').val();
var testo = $('form input[name="testo"]').val();
data: {
   titolo:titolo,
   post:testo
}

Also, as a side note, when accessing array elements, its not good form to put a space between the variable and the bracket. I'm surprised that even works.

$_REQUEST ['titolo']

Should be

$_REQUEST['titolo']

8 Comments

You are right. I was just working on something and for some reason that was in my head. Fixed it.
Ha ha. Moreover, I was coincidentally reading that so thought of sharing. :) No offence.
Same problem. Undefined index: post in inserisci.php on line 6
@Master-Antonio I updated my answer. I didn't realize you weren't grabbing the variables first. You are sending a null value and it gets discarded.
|
1

If you want to use ajax form submit, Update your form definition with

<form id="form" class="form-horizontal shadow-z-1" action="#" method="post">

use following updated javascript

function send() {
    var content = $.ajax({
        type: "POST",
        url: "process.php",
        data: {
            titolo:  "Value for titolo",
            post: "Value for post"
        }
    }).done(function() {
        alert( "success" );
    }).fail(function() {
        alert( "error" );
    }).always(function() {
        alert( "complete" );
    });
}

1 Comment

I believe that might cause a W3C validation issue, action="#".

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.