html - How would you display a sent message in PHP without redirecting through header location? -
i using jquery mobile , has issues loading external files - displaying "error loading page" message. can't utilize header location in php redirect page after submiting form.
how display sent message underneath submit button 1 time it's been pressed?
this html:
<form method="post" id="update_beer" action="php/input_pint.php"> <p><label for="name">your name</label><input type="text" size="30" name="name" id="name" /></p> <p><label for="price">price of pint</label><input type="number" name="price" id="price" cols="5" /></p> <p><input type="submit" value="update" name="commit" id="message_submit"/> or <a class="close" href="/">cancel</a></p> </form>
this php sending input database:
<?php //input price_pint.php $con = mysql_connect("hostname", "databasename", "password"); if (!$con) { die("could not connect." . mysql_error()); } mysql_select_db("databasename", $con); $sql="insert price_pints (name, price) values ('$_post[name]','$_post[price]')"; if (!mysql_query($sql,$con)) { die('error: ' . mysql_error()); } mysql_close($con); ?>
i don't know how relate echo message form.
you can utilize ajax, need sql escape inputs, else drink foster's
sam smith's
ect going break query.
<script> $("#update_beer").submit(function(e){ $.ajax({ type: "post", url: "php/input_pint.php", data: $("#update_beer").serialize(), success: function(data){ if(data=='true'){ alert('beer added'); } } }); e.preventdefault(); }); </script>
also heres improve way of connecting , inserting database, using pdo:
<?php seek { $db = new pdo("mysql:host=127.0.0.1;dbname=databasename", 'dbuser', 'dbpassword'); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception); $db->setattribute(pdo::attr_emulate_prepares, false); }catch (exception $e){ die('cannot connect mysql server.'); } if($_server['request_method']=='post'){ if(!empty($_post['name']) && !empty($_post['price'])){ $sql = 'insert price_pints (`name`,`price`) values (:name,:price)'; $query = $db->prepare($sql); $query->bindparam(':name', $_post['name'], pdo::param_str); $query->bindparam(':price', $_post['price'], pdo::param_str); $query->execute(); die('true'); } } die('false'); ?>
php html forms jquery-mobile echo
No comments:
Post a Comment