Post data to php form



  • Hello i can use something of webkit to post data to a php form?
    ex:
    @ <table>
    <b>Inserisci la password</b>
    <form method="POST">
    <input type="passwd" name="password"></input>;
    <input type="submit" name="Entra" value="entra"></input>;
    </form>
    </table>

    </div>@
    how i can pass the value "password"?

    Thanks
    Luca



  • Hi,

    You should specify destination URL (aka the php script location) as an attribute called action.

    @<form method="POST" action="http://example.com">@

    Best regards,
    Leon



  • Since you mentioned Webkit, the most straightforward way IMHO is to use a little piece of JavaScript that fills in and submits the form.

    If you're interested in submitting a form without using Webkit, then you can use QNetworkAccessManager::get/post (depending on the form's method). For the former, you can build up the right URL by using QUrl and addQueryItem. For the latter, there's nothing in Qt 4.7 if you want to submit multipart/form-data (but the QHttpMultiPart/QHttpPart classes will be introduced in Qt 4.8), while instead you may use QUrl again if you want to submit the form as application/x-www-form-urlencoded.



  • The url is thwe same,
    the php post don't send data via url, this is my problem



  • This snippet is untested and adapted from "this post":http://stackoverflow.com/questions/2214595/how-can-i-create-a-http-post-request-with-qt-4-6-1, it should work or give you at least a hint on how to proceed:

    @
    QUrl params;
    params.params.addEncodedQueryItem("password", QUrl::toPercentEncoding("yourvalue"));
    // the string contains a ? at the beginning
    // mid(1) strips this off
    QByteArray paramBytes = params.toString().mid(1);

    QNetworkRequest request(QUrl("http://your.server.com/path/to/send"));
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");

    QNetworkAccessManager *nam = new QNetworkAccessManager;
    QNetworkReply *reply = nam(request, paramBytes);
    @



  • [quote author="peppe" date="1301933226"]If you're interested in submitting a form without using Webkit, then you can use QNetworkAccessManager::get/post (depending on the form's method). For the former, you can build up the right URL by using QUrl and addQueryItem. For the latter, there's nothing in Qt 4.7 if you want to submit multipart/form-data (but the QHttpMultiPart/QHttpPart classes will be introduced in Qt 4.8), while instead you may use QUrl again if you want to submit the form as application/x-www-form-urlencoded.[/quote]

    Just reflecting if it is possible to send form-data or not with QNetworkAccessManager: it is possible, you have to put together your name/value pairs to a string and send it through as the content of your post request. You can see it below:

    @void ClientLogin::doLogin(const QString email, const QString password, const QByteArray captchaToken, const QString captchaValue)
    {
    QUrl url = QUrl(CLIENTLOGIN_URL);
    QNetworkRequest request;
    QByteArray content = "";

    request.setUrl(url);
    
    content += "accountType=HOSTED_OR_GOOGLE&";
    content += "Email=" +  email.toAscii() + "&";
    content += "Passwd=" + password.toAscii() + "&";
    content += "service=jotspot&";
    content += "source=gsites-0.1a";
    
    if (!captchaToken.isEmpty()) {
        content += "&";
        content += "logintoken=" + captchaToken + "&";
        content += "logincaptcha=" + captchaValue.toAscii();
    }
    
    m_manager.post(request, content);
    

    }
    @

    ADDED: actually this snippet is part from my working app, so it is tested



  • The snippet is slightly wrong --- you're supposed to:

    • percent-encode the fields (QByteArray::toPercentEncoding), otherwise a field containing spaces or ampersands will blow up everything;
    • don't use the "dangerous" toAscii() but toUtf8() (see also http://tools.ietf.org/html/rfc3986#section-2.5 );
    • set the content-type for the post to "application/x-www-form-urlencoded; charset=utf-8" (or whatever encoding you are using).


  • @peppe: of course you are right, it is still work in progress and moreover it is only a small application for learning purposes (therefore it will never be thorougly tested). My aim was just to show that you can send form data with using QNetworkAccessManager.



  • You're absolutely right -- perhaps I expressed myself badly before.

    You CAN do it as of now using QNetworkAccessManager. The only culprit is that in 4.7 there's nothing to help you for making multipart/form-data POST requests. application/x-www-form-urlencoded POST requests are easy to deal with (by hand or using QUrl).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.