Search code examples
jqueryajaxposttextarea

PHP doesnt read textarea content sended by ajax call


i'm dealing with this ajax call:

HTML:

A form has a textarea in which user can type some text

      <div id="step-3">
        <h2 class="StepTitle">Testo</h2>
        <form id="text" class="form-horizontal form-label-left">
          <div class="item form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-12">Textarea <span class="required">*</span>
            </label>
            <div class="col-md-6 col-sm-6 col-xs-12">
              <textarea id="textarea" name="testo" data-parsley-required="true" class="form-control col-md-7 col-xs-12"></textarea>
            </div>
          </div>
        </form>
        <ul id="error3_ul" class="list-unstyled">
            <li id="error3_li"></li>
        </ul>
      </div>

JS:

This function is called by smartwizard. When user types some text and pushes button, an ajax call starts to do a server side check before to effectively insert text into db.

function onFinishCallback()
{
    var data = $('#textarea').val();
    $.ajax({
          method: 'post',
          data: data,
          dataType: 'html',
          url: "include/library/pull_sim.php",
        success: function(result) {
            successmessage = 'Data was succesfully captured';
            $("#error3_li").text(result);
        },
    });
}

PHP:

Php receives the posted textarea value, check if a similar_text is already into db and if yes, it alerts that to user by the ajax call result.

if((!ISSET($_POST['testo'])))
    $val='';
else
    $val=$_POST['testo'];
$q_sim='select nciarfata from nciarf.nciarfata';
$s_sim=mysqli_query($conn,$q_sim);
$n_sim=mysqli_num_rows($s_sim);
if ($n_sim>0)
{
    $simil=array();
    for ($i=0;$i<$n_sim;$i++)
    {
        $rou=mysqli_fetch_row($s_sim);
        similar_text($val, $rou[0], $percent); 
        if ($percent>=95.0)
        {
            array_push($simil,$rou[0]);
        }
    }
    echo"val=$val, rou[0]=$rou[0], percent=$percent";
}

Question:

In my opinion something goes wrong in server side, probably in the 1st if. Php doesnt recognized the posted value and then assign val="" instead of real text typed by user before..

Why? Thanks for helping me.


Solution

  • I find the solution here:

    Can't figure out why PHP not receiving POST data from $.ajax call

    It wasn't a textarea issue but an ajax jquery one (data option).