Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript need help making a script

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #27222
    skullcrusher
    Member

    Hi everyone.

    Hi need help to build a script, here is what i want..

    i have two pages, first for the produts, second a quote form page. i whant thoose products have an icon that when the user clicks its adds to some database and then appears in the quote page with some more inputs (width, height, quantity) of the product.

    Example: the user added product #1 to quote page;

    Quote:
    products added:

    product #1 – reference bla bla bla
    quantity: __________________
    width: __________________
    height: __________________

    product #2 – reference bla bla bla
    quantity: __________________
    width: __________________
    height: __________________

    (send form)

    and the items always appears when in the same session…

    help please! dont know where to start.. thanks :)

    #68389
    skullcrusher
    Member

    No one?

    #68412
    BaylorRae
    Member

    I would save the persons shopping cart in a PHP session separated with commas.

    Code:
    $_SESSION[‘user_cart’] = ‘5,5,2,4,2’;

    Then use jQuery to send information to a php page to add to the session.

    Code:
    jQuery(document).ready(function($) {

    $(‘.product’).click(function() {

    // The link will look like this
    // Add Product
    var prod_id = $(this).attr(‘rel’).replace(/id-/, ”);

    $.get(‘add2cart.php’, {item_id: prod_id});

    return false;
    });

    });

    // The PHP page

    Code:

    And then loop through the session to display each item

    Code:
    $quantity ) :
    echo $id . ‘ = ‘ . $quantity . ‘
    ‘;
    endforeach;

    }else
    echo “Your cart is empty”;
    ?>

    In the last foreach, you’ll want to make a function to get the item’s title from the database.
    Similar to this.

    Code:
    function title_from_id($id) {
    $id = mysql_real_escape_string($id);
    $sql = “SELECT * FROM `products` WHERE `id` = ‘$id'”;
    $res = mysql_query($sql) or die(mysql_error());
    $row = mysql_fetch_assoc($res);
    return $row[‘title’];
    }
    #68416
    BaylorRae
    Member

    I’ve zipped up all my files that I used to test this.

    #68419
    skullcrusher
    Member

    Yaiks! Thanks man..

    1. There is anyway to adapte the script to use prototype instead of jquery? (its the library that im using in the page)

    2. Can i put the product name like..

    <a rel="id-product name" class="product" href="#">Add Product</a>

    #68462
    BaylorRae
    Member

    I don’t know prototype so you’ll have to look through the documentation.

    If you want to use the product name, then it should work just like that.

    #68508
    skullcrusher
    Member

    Ok, thank you. One more thing: whats the code to a "remove" icon?

    Edited: 2º question.. i want to send the data by email.

    Code:
    foreach( $item_quantity as $id => $quantity ) :
    echo ‘

    Nome do producto
    Remover




    ‘;
    endforeach;

    Code:
    $nomeprod = $_POST[“Nomeprod”];
    $comprimento = $_POST[“Comprimento”];
    $largura = $_POST[“Largura”];
    $altura = $_POST[“Altura”];
    $consumo = $_POST[“Consumo”];
    Code:
    Produto: $nomeprod
    Comprimento: $comprimento
    Largura: $largura
    Altura: $altura
    Consumo: $consumo

    Basiclly is that. But i want this for every products in the cart.. how can i change to dynnamic variables?

    #68721
    skullcrusher
    Member

    The problem of sending data by email is solved.. i just need to code a "remove" button.

    #68779
    skullcrusher
    Member

    the code for remove button…

    Code:
    if( isset($_SESSION[‘user_cart’]) ) {
    if ($numero > 1) {
    $del = “,”;
    $del .= $_GET[‘del_id’];
    $_SESSION[‘user_cart’] = str_replace($del, “”, $_SESSION[‘user_cart’]);

    } else {
    unset($_SESSION[‘user_cart’]);
    }

    }else {
    echo “erro”;
    }

    Its working.. just one problem. Cant remove the first item, becase dont have the ‘,’ before.. how can i solve it? help please :)

    PROBLEM SOLVED, here is the solution:

    Code:
    if( isset($_SESSION[‘user_cart’]) ) {
    if ($numero > 1) {
    if ($items[0] == $_GET[‘del_id’]) {

    $del = $_GET[‘del_id’];
    $del .= “,”;
    $_SESSION[‘user_cart’] = str_replace($del, “”, $_SESSION[‘user_cart’]);

    } else {

    $del = “,”;
    $del .= $_GET[‘del_id’];
    $_SESSION[‘user_cart’] = str_replace($del, “”, $_SESSION[‘user_cart’]);

    }

    } else {
    unset($_SESSION[‘user_cart’]);
    }

    }else {
    echo “erro”;
    }

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.