none
Posting multiple key/value pairs to php file RRS feed

  • Question

  • I want to fill in a form on a vb.net desktop application and then send the values to a php for that will update a MySQL database.

    I can only figure out how to send one key/value pair though.
    "message1=" & TextBox1.Text
    As in the code blocks below.
    I also want to be able to send:
    "message1=" & TextBox1.Text & "message2=" & TextBox2.Text & "message3=" & TextBox3.Text

    Anyone know how to do that?

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim postData As Byte() = Encoding.Default.GetBytes("message1=" & TextBox1.Text) '
            sendPost(postData)
        End Sub
    Private Function sendPost(ByVal p As Byte()) As String
            Dim encoding As New UTF8Encoding
            Dim byteData As Byte() = p
            Dim postReq As HttpWebRequest = HttpWebRequest.Create(url)
            postReq.Method = "POST"
            postReq.KeepAlive = True
            postReq.ContentType = "application/x-www-form-urlencoded"
            'postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
            postReq.ContentLength = byteData.Length
    
            Dim postreqstream As Stream = postReq.GetRequestStream()
            postreqstream.Write(byteData, 0, byteData.Length)
            postreqstream.Close()
            Dim postresponse As HttpWebResponse
            postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
            Stop
            Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
            Return postreqreader.ReadToEnd()
            'Stop
        End Function

    Thanks

    Andy

    Monday, May 13, 2019 4:56 PM

All replies

  • Hi,

    see the following link,you need to make sure that each key/value pair must be separated by &.

    https://stackoverflow.com/questions/30465184/how-can-i-send-multiple-variables-from-vb-net-to-php-via-post-method

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 14, 2019 2:06 AM
  • Hi Alex, I tried that one already but I can't get it to work. I just get undefined variable error in the php file.

    There is a comment in the thread you sent stating that:
    --------------------------

    
    

    This statement will still be considered as a single array. (NOT two different values)

    or you can say different values in single string.

    I guess in order to send multiple values, we must have to use array with (key->value) combination.

    where Key = msg and value will betextbox1.text and so on.
    ---------------------------------

    Maybe I could send it as a single string with a delimiter between the values and split the string up in the php but that misses the point.

    I've seen mention of using the dictionary function, but the method of using it was unclear.

    Tuesday, May 14, 2019 6:33 AM
  • Hi,

    I have no other ideas,but the string provided by link is one more & than yours, do you have try it?

    Dim postData = "msg=" & TextBox2.Text & "&msg2=" & TestBox3.Text

    Best Regards,

    Alex



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 15, 2019 8:19 AM
  • Hi Alex,
    Could be I'm doing something wrong but in the end I just concatenated the values together separated with a comma and split them apart in the php script.

    vb:

    myPost = "message1=" & strSerial & "," & intRecCount

    php:

    $string = $POST['message1'];
       
    list($serial,$RecCount) = explode(',',$string);

    Wednesday, May 15, 2019 1:35 PM
  • Hi,

    You can split string myPost into multiple strings and then post them one by one.Is this method ok?

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 20, 2019 9:10 AM