search

Home  >  Q&A  >  body text

How to solve the "Notice: Undefined index" error when QT application calls php script

<p>This is my 1 line of PHP code;</p> <pre class="brush:php;toolbar:false;"><?php echo $_POST['api']; ?></pre> <p>This is how my QT application calls the PHP script;</p> <pre class="brush:php;toolbar:false;">QUrl url("http://*****.com/id.php"); QNetworkRequest request(url); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); QUrlQuery params; params.addQueryItem("api", "test"); networkClients->post(request, params.query().toUtf8());</pre> <p>This is a PHP notification</p> <p>The code ran seamlessly on my previous hosting provider. After I moved my stuff to hostinger, I am now unable to send data to my application using POST requests ($_POST['api'] returns empty and echo does not output anything). GET requests work fine. I also tested on reqbin and it works fine, but the application doesn't. </p>
P粉293341969P粉293341969450 days ago535

reply all(1)I'll reply

  • P粉466290133

    P粉4662901332023-09-05 00:58:47

    QUrlQuery will add parameters to the URL. This means they must be received using the GET method.

    <?php
    echo $_GET['api']; ?>
    

    However, it is always recommended to check if the value is null.

    <?php
    if isset ($_GET['api'])
      {echo $_GET['api'];}
    else
      {echo 'Not found';}
    ?>
    

    The POST method can only be used when submitting or posting the form using a form and a button or an input of type button.

    reply
    0
  • Cancelreply