WordPress.org

Ready to get started?Download WordPress

Forums

Forwarding page to URL with unique ID (20 posts)

  1. nisals
    Member
    Posted 2 years ago #

    HI,

    I am trying to redirect my page to a URL with a unique ID. The unique ID is taken from the hidden field on mysql table.

    here is the code

    $id=$_POST['id'];

    header("Location: http://www.mysite.com/mycheck/?id=$id");
    exit;
    ?>

    Now, i do get redirected to the correct page, but the ID field is blank. for some reason the variable $id does not get the unique ID from the form.

    URL looks like this: http://www.mysite.com/mycheck/?id=

    please help.

  2. stvwlf
    Member
    Posted 2 years ago #

    Hi

    1) Try using this function to add the ID # to the redirect URL:
    http://codex.wordpress.org/Function_Reference/add_query_arg
    WordPress strips off query vars its not made aware of in advance

    2) use this
    http://codex.wordpress.org/Function_Reference/wp_redirect
    instead of "header("Location"

  3. nisals
    Member
    Posted 2 years ago #

    hey, thank you for the reply.

    now the code is like this:

    $id=$_GET['id'];
    wp_redirect( 'http://www.mysite.com/mycheck/?id=$id', 301 ); exit; ?>

    we are getting closer, the URL is doing this now: http://www.mysite.com/mycheck/?id=id

    instead of the id number, it is throwing id

  4. nisals
    Member
    Posted 2 years ago #

    it is actually

    $id=$_POST['id'];
    wp_redirect( 'http://www.mysite.com/mycheck/?id=$id', 301 ); exit; ?>

    i am not using GET

  5. stvwlf
    Member
    Posted 2 years ago #

    You are passing the ID on the URL in the redirect statement. Any query params passed on a URL are by definition a GET - that is what a GET is. WordPress strips off unknown GET params. You can make WP aware of unknown params like ID by using the function I linked you to.

  6. nisals
    Member
    Posted 2 years ago #

    are you talking about this type of a function

    // This would output whatever the URL to post ID 9 is, with 'hello=there' appended with either ? or &, depending on what's needed
    echo add_query_arg( 'hello', 'there', get_permalink(9) );

  7. stvwlf
    Member
    Posted 2 years ago #

    <?php
    $location = add_query_arg( 'id', $id, 'http://www.mysite.com/mycheck/' );
    wp_redirect( $location, 301 );
    exit;
    ?>
  8. nisals
    Member
    Posted 2 years ago #

    hm, now for some reason the link does not pick up /?id

    it looks like this
    http://www.mysite.com/mycheck/

  9. nisals
    Member
    Posted 2 years ago #

    its the $id thats giving the issue. its not resolving it for some reason

  10. stvwlf
    Member
    Posted 2 years ago #

    To debug do an echo statement in your code just before using the $id to confirm it in fact has a visible value. Depending on where in your code it is you might have to look at the browser View Source to see it.

  11. nisals
    Member
    Posted 2 years ago #

    yup ive done that. there is no value

    <input name="id" type="hidden" value=""></td>

  12. stvwlf
    Member
    Posted 2 years ago #

    I don't know if what you just reported is what I suggested doing.

    Put the echo BEFORE the $location = line in the code below, to make sure the variable $id you are trying to pass as a query param has a value at the time you are adding it onto the URL. If it has no value that is your problem. That is a problem before the redirect occurs. You have to find out why.

    <?php
    echo $id;
    $location = add_query_arg( 'id', $id, 'http://www.mysite.com/mycheck/' );
    wp_redirect( $location, 301 );
    exit;
    ?>
  13. nisals
    Member
    Posted 2 years ago #

    yes this is where the problem is. $id does not have a value.

  14. nisals
    Member
    Posted 2 years ago #

    this is the way this site suppose to work:

    1. submit an order from the form
    2. write to the db and reads the table with unique id (auto increment) (hidden value) and displays it.

    but i cannot figure out why i cant pull the unique ID for that particular order.

  15. stvwlf
    Member
    Posted 2 years ago #

    Have you looked in the database to make sure that order HAS a unique ID?

    Are you sure the variable $id is in scope when you are trying to use it? Meaning, if it is declared in a different function than the one you are trying to use it in, you have to declare it as global in the function, or else set its value again from the $_POST variable.

    The real issue is, after you submit the form, check the value of $_POST and make sure the variable ['id'] has been set.

    Also, in the hidden form field I'm pretty sure you have to echo the value

    <input type="hidden" value="<?php echo $id; ?>" />

  16. nisals
    Member
    Posted 2 years ago #

    ok so simply put, this is what i have for my form

    <form name="myWebForm" action="testorder" method="post">
    First: <input title="Please Enter Your First Name" id="first" name="first" type="text" size="12" maxlength="12" /> Last: <input title="Please Enter Your Last Name" id="last" name="last" type="text" size="18" maxlength="24" /><br />
    <input type="hidden" name="orderNumber" id="orderNumber" value="<?php echo $orderNumber; ?>" /><br />
    <input type="submit" value="SUBMIT" />
    <input type="reset" value="RESET" />
    </form>

    this is my php code:

    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // get data that sent from form
    $orderNumber=$_POST['orderNumber'];
    $first=$_POST['first'];
    $last=$_POST['last'];
    
    $sql="INSERT INTO $tbl_name(orderNumber, first, last) VALUES('$orderNumber', '$first', '$last')";
    
    $result=mysql_query($sql);
    
    if($result){
    
    echo "Successful<BR>";
    echo $orderNumber;
    echo $first;
    }
    else {
    echo "ERROR";
    }
    mysql_close();
    ?>

    now, $first is outputting everything correctly. but the orderNumber is giving me no results.

    this is my code for the table:

    CREATE TABLEtestform` (
    orderNumber int(6) NOT NULL auto_increment,
    first varchar(65) NOT NULL default '',
    last varchar(65) NOT NULL default '',
    KEY orderNumber (orderNumber)
    ) TYPE=MyISAM;`

    Any help would be greatly appreciated. I need to find out a way to spit out the orderNumber

  17. stvwlf
    Member
    Posted 2 years ago #

    What is unclear in the code you sent is, you are outputting order # as a hidden field in the form. However in that form code snippet it does not show where you are retrieving order # from the database before you display it in the hidden field.

    One thing that looks incorrect is you have orderNumber defined to auto increment in the mySQL table definition, yet you are assigning a value to it in the SQL insert statement. An auto-increment field automatically fills in its value when inserted. You don't assign a value to it.

    Where is the code that runs before the HTML form displays, that retrieves the order # from the database before you add the hidden field to the form? If the form is for adding a new record, no value should be assigned to orderform, as I said it auto increments on INSERT. If you are editing an existing record, you can retrieve it prior to displaying the form and echo it as a hidden field. Then you will do a SQL UPDATE not an INSERT to modify the existing record.

  18. nisals
    Member
    Posted 2 years ago #

    ok i get what your saying. but is is possible for me to pull the auto increment value for that particular order?

  19. stvwlf
    Member
    Posted 2 years ago #

    If you already know the order # at the time you display the form you echo it in the hidden field just as you are doing. If you don't know the order # at the time you display the form then there is no order # to echo as a hidden field.

    How do you know what the order # is supposed to be before you display the form?

    If you are adding new records to the table you don't need to deal with the order # because the auto increment will assign the order #.

    Apparently if you need to retrieve the auto increment ID of the most recent INSERT you can do it with this
    SELECT LAST_INSERT_ID();

  20. nisals
    Member
    Posted 2 years ago #

    got it. thanks alot for your help

    $id = mysql_insert_id(); worked

Topic Closed

This topic has been closed to new replies.

About this Topic