How do you assign a variable to a form from earlier php code to post to next php page? -
i making site collects user comments via submission form , outputs them onto page can rated 1-10. i'm having trouble rating system part.
i have form radio buttons rating sends post value via 'sendrating.php'. want send used variable 'id' tied particular comment , particular rating. have tried use hidden input cannot seem syntax correct , whatever try seem error.
i show both php files have:
<?php $con=mysqli_connect("host","me","pwd","base"); // check connection if (mysqli_connect_errno($con)) { echo "failed connect mysql: " . mysqli_connect_error(); } $query= "select id, comment, date_made, rating table order id desc limit 15"; $result=mysqli_query($con, $query); while ($row=mysqli_fetch_array($result)) { $date=$row['date_made']; $id=$row['id']; $num_id=intval($id); $current=$row['rating']; echo '<div id="text" onpaste="return false"; oncut="return false"; readonly>'; echo '<div id="text2">'; echo "submitted: "; echo date('l js f y h:i:s ',strtotime($date));echo ' '; echo ' '; echo ' '; echo "no: "; echo $row['id']; echo '</div>'; echo '<br>'; echo nl2br($row['comment']); echo '<br>'; echo '<br>'; echo '</a>'; //foot echo '<div >'; echo '<form id="radio" name="input_rating" action="sendrating.php" method="post" >'; echo '<input type="hidden" name="id" value="'$id'" >'; echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="1">1'; echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="2">2'; echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="3">3'; echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="4">4'; echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="5">5'; echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="6">6'; echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="7">7'; echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="8">8'; echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="9">9'; echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="10">10'; echo ' '; echo ' '; echo ' '; echo "rating: "; echo ' '; echo "$current"; echo '</form>'; echo '</div>'; //end foot echo '</div>'; echo '<p></p>'; } ?>
the form action processed following (or if working):
<?php $con2=mysqli_connect("host","me","pwd","base"); // check connection if (mysqli_connect_errno($con2)) { echo "failed connect mysql: " . mysqli_connect_error(); } $value=$_post['rating']; $id=$_post['id']; mysqli_query ($con2, "update table set totalraters = totalraters + 1 id='$id' "); mysqli_query ($con2, "update table set ratingsum = ratingsum + '$value' id='$id' "); $query2 = mysqli_query ($con2, "select table ratingsum, totalraters id = '$id' "); $result2 = mysqli_query($con2, $query2); $row2 =mysqli_fetch_array($result2); $ratingsum=$row2['ratingsum']; $totalraters=$row2['totalraters']; $current = $ratingsum/$ratingtotal; mysqli_query ($con2, "insert table rating value '$current' id='$id' "); include ('/index.html') ?>
the include page should take user start page rating updated.
ratingsum total added of ratings given , totalraters number of people have rated. $current should average gives current rating.
the line i'm having trouble @ moment
echo '<input type="hidden" name="id" value="'$id'" >';
i can't seem id 1 place other used in further queries. syntax error line. gratefully received , believe have tried correct solution!! please use procedural because new , can't understand object oriented @ all...it makes head hurt ;-)
this done way:
echo '<input type="hidden" name="id" value="' . $id . '" >';
see dotes between string , variable, merge those. 1 incorrect:
echo '<input type="hidden" name="id" value="$id" >';
because single quotes ignore variables. michi
, lemondrop
tip!
Comments
Post a Comment