I need to insert some form values into a db table , how would I create a function to call once the user clicks on the button and run the insert scrtipt .
<form name="quiz_info" method="post">
<?php
echo $this->quiz->title;
echo $mainframe->getPageTitle();
echo '<p><input type="checkbox" id="checkToProceed" name="checkToProceed" onclick="proceed();" />
<label for="checkToProceed">' . JText::_('I have Read and Acknowledge the procedure'). '</label></p>' ;
echo '<input id="proceedButton" name="proceedButton" disabled="true" value="' . JText::_('Acknowledge') . '" type="submit" />' ;
//Declare Variables
$user = JFactory::getUser();
$id = $user->get('id');
$name = $user->get('name');
$username = $user->get('username');
$department = $user->get('department');
$vardate = date("m/d/y : H:i:s", time());
$courseTitle = $mainframe->getPageTitle();
$db = &JFactory::getDBO();
$query ="INSERT INTO `jos_jquarks_users_acknowledge` (course_name,user_id,employeeNumber,department,name,acknoledge,timeStamp) VALUES ($courseTitle,$id,$username,$department,$name,acknoledge,vardate)";
$db->setQuery( $query );
$db->query();
?>
<input type="hidden" name="layout" value="default" />
<?php echo JHTML::_( 'form.token' ); ?>
</form>
PHP is executed on the server-side. If you want to call the function without reloading the page you will have to use an AJAX call, for example with jQuery.
You can find billions of tutorials through google.
<?php
if ($_POST['proceedButton'] != '') {
$user = JFactory::getUser();
$id = $user->get('id');
$name = $user->get('name');
$username = $user->get('username');
$department = $user->get('department');
$vardate = date("m/d/y : H:i:s", time());
$courseTitle = $mainframe->getPageTitle();
$db = &JFactory::getDBO();
$query ="INSERT INTO `jos_jquarks_users_acknowledge`(course_name,user_id,employeeNumber,department,name,acknoledge,timeStamp) VALUES ($courseTitle,$id,$username,$department,$name,acknoledge,vardate)";
$db->setQuery( $query );
$db->query();
}
?>
<form name="quiz_info" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php echo $this->quiz->title; ?>
<?php echo $mainframe->getPageTitle(); ?>
<input type="checkbox" id="checkToProceed" name="checkToProceed" onclick="proceed();" />
<label for="checkToProceed"><?php echo JText::_('I have Read and Acknowledge the procedure'); ?></label>
<input id="proceedButton" name="proceedButton" disabled="true" value="<?php JText::_('Acknowledge'); ?>" type="submit" />
<input type="hidden" name="layout" value="default" />
<?php echo JHTML::_( 'form.token' ); ?>
</form>
PHP doesn't work this way. If you wish to activate your PHP script on form submit, you'll need to submit the form to a PHP page, and on that page to put your script, for example
<form action=submit.php method=post>
<input type=text name=text>
<input type=submit>
</form>
Next, on submit.php:
<?php
if (!empty($_POST['text'])) { //If the POST variable set by input named 'text' is not empty...
echo $_POST['text']; //Print it on the screen.
} else { //If it is empty
echo "No form submission detected"; //Print an error
}
?>
If you want it to work without a page reload, you'll have to use some client side technology. The most popular one for that purpose is AJAX
Related
I'm trying to call the old values to be edited. What part am I wrong at?
<?php
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$BookNo'");
if (mysqli_num_rows($record) == 1 ) {
$n = mysqli_fetch_array($record);
$BookNo = $n['BookNo'];
$ISBN = $n['ISBN'];
$title = $n['title'];
$author = $n['author'];
$publisher = $n['publisher'];
$status = $n['status'];
$cost = $n['cost'];
}
}
?>
<a href="viewBook.php?edit=<?php echo $row['BookNo']; ?>" class="edit_btn" >Edit</a>
</td>
<?php
if (isset($_GET['edit'])) { ?>
<form method="post" action = "viewBook.php">
<input type="hidden" name="BookNo" value="<?php echo $BookNo; ?>">
<input type="text" name="ISBN" value="<?php echo $ISBN; ?>">
<input type="text" name="title" value="<?php echo $title; ?>">
<input type="text" name="author" value="<?php echo $author; ?>">
<input type="text" name="publisher" value="<?php echo $publisher; ?>">
<input type="text" name="status" value="<?php echo $status; ?>">
<input type="text" name="cost" value="<?php echo $cost; ?>">
<?php if ($update == true): ?>
<button class="btn" type="submit" name="update" style="background: #556B2F;" >update</button>
<?php else: ?>
<button class="btn" type="submit" name="save" >Save</button>
<?php endif ?>
<?php } ?>
</form>
So far, what it does is, when the user clicks the edit button, it just shows 6 text fields. I thought by doing what I did, it was supposed to show the details already filled in the textbox.
When you do
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$BookNo'");
$BookNo is not defined.
maybe you wanted to do something like this:
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$id'");
<form method="post" action = "viewBook.php">
your form method is "post" but you are checking $_GET You must check $_POST
if (isset($_GET['edit']))
you are passing value in $id And using $BookNo which not define.
only 6 input field will be show because first one is using hidden property.
<input type="hidden" name="BookNo" value="<?php echo $BookNo; ?>">
when you click on submit button data will be receive by $_POST
How would I go about doing this?
<?php
if (isset ($_SESSION['ID'])) {
echo "
<form action = 'updateacct.php' method = 'POST'>
Email:
<input type = 'text' name = 'eml' value = '" . echo $_SESSION['ID'] . "' placeholder = 'Email' size = '30' required/>
</form>
?>
I'm trying to pull a var from the session and put it inside a form value and can't figure out how to do so.
It's not recommended to echo your whole html in PHP... You could do it like this:
<?php if(isset($_SESSION['ID'])): ?>
<form action='updateacct.php' method='POST'>
Email: <input type='text' name='eml' value='<?php echo $_SESSION['id']; ?>' placeholder='Email' size='30' required/>
</form>
<?php endif; ?>
No need for the second echo. You are already echoing.
I took your code and simplified it a bit. I use multiple echos to make it clearer what we do.
<?php
if (isset($_SESSION['ID'])) {
echo '<form action="updateacct.php" method="POST">';
echo ' Email:';
echo ' <input type="text" name="eml" value="' . $_SESSION['ID'] . '" placeholder="Email" size="30" required />';
echo '</form>';
}
?>
I would go like this:
<?php if (isset ($_SESSION['ID'])) : ?>
<form action = 'updateacct.php' method = 'POST'>
Email:
<input type = 'text' name = 'eml' value = '<?= $_SESSION['ID'] ?>' placeholder = 'Email' size = '30' required/>
</form>
<?php endif; ?>
You can say:
<?php
if (isset ($_SESSION['ID'])) {
?>
// HTML goes here
<?php
}
?>
I have struggled with this issue for a majority of the day and I am really stumped.
I have this page called preview.php with the following code below. Values are being passed to this page from another page called order.php. This preview.php page allows a user to review his or her data input from order.php.
If correction is needed, the user returns to order.php to make his or her corrections. If everything looks fine, the user sends his or her orders to process.php to be processed. The information sent to process.php are hidden form fields.
For some reason that I am unable to figure out, process.php is not recognizing the hidden form field values.
Does anyone have an idea how to resolve this?
This gentle man #spencer7593 actually helped me out with this insert statement. However, each time I attempt to add a record, I get the following:
Error: custname value cannot be null
This happens because the hidden form values are null and I can't figure out why.
preview.php
?php
error_reporting(E_ALL);
echo "DEBUG POST DATA: <pre>".print_r($_POST, 1)."</pre>";
if(isset($_POST['custname']))
$custname = $_POST['custname'];
if(isset($_POST['zip']))
$zip = $_POST['zip'];
if(isset($_POST['city']))
$city = $_POST['city'];
if(isset($_POST['email']))
$email = $_POST['email'];
$address = $_POST['address'];
$rowIDs = $_POST['rowIDs'];
$row2IDs = $_POST['row2IDs'];
echo $custname .'<br>';
echo $zip .'<br> <hr width=400 align=left>';
$rowIDs = $_POST['rowIDs'];
foreach ($rowIDs as $id) {
$agentname = $_POST['agentname' . $id];
$agentaddress = $_POST['agentaddress' . $id];
$agentincome = $_POST['agentincome' . $id];
echo 'Name: '. $agentname . '<br />';
echo 'Address: '. $agentaddress . '<br />';
echo 'agentincome: '. $agentincome . '<br /><br>';
}
?>
<body>
<form action='process.php' method = 'POST'>
<input type='hidden' name='custname' value="<?php echo $custname; ?>">
<input type='hidden' name='address' value="<?php echo $address; ?>">
<input type='hidden' name='email' value="<?php echo $email; ?>">
<input type='hidden' name='city' value="<?php echo $city; ?>">
<input type='hidden' name='zip' value="<?php echo $zip; ?>">
<input type='hidden' name='agentname' value="<?php echo $agentname; ?>">
<input type='hidden' name='agentaddress' value="<?php echo $agentaddress; ?>">
<input type='hidden' name='agentincome' value="<?php echo $agentincome; ?>">
<input type="action" type="button" value="Return to data" onclick="history.go(-1);" /> | <input type="submit" value="Submit your form" />
</form>
</body>
process.php
<?php
global $wpdb;
// Database connection
$conn = mysqli_connect("localhost","myuser","mypwd","myDB");
if(!$conn) {
die('Problem in database connection: ' . mysql_error());
}
// Data insertion into database
$sql = 'INSERT INTO `myDB`.`myTable` ( `custname`'
. ', `address`, `city`, `zip`, `email` )'
. ' VALUES ( ? , ? , ? , ? , ? )';
if( $sth = mysqli_prepare($conn,$sql) ) {
mysqli_stmt_bind_param($sth,'sssss'
,$_POST["custname"]
,$_POST["address"]
,$_POST["city"]
,$_POST["zip"]
,$_POST["email"]
);
if( mysqli_stmt_execute($sth) ) {
echo '<h1>Thank you</h1> <br> Go back to the main page <a href="index.php");';
} else {
printf("Error: %s\n",mysqli_stmt_error($sth));
}
} else {
printf("Error: %s\n",mysqli_connect_error($conn));
}
?>
I have made an Event calender but now I'm facing a problem when the user deletes a event.
Screenshot: http://i.stack.imgur.com/LBLJ9.png
<h2>Date <?php echo "$day"."/"."$month"."/"."$year"."<br>"; ?></h2>
<?php
// event weer gegeven
while ($events = mysql_fetch_array($resultEvents)){
echo "<strong>Event ID:</strong> ".$events['id']."<br>";
echo "Added: ".$events['added']."<br>";
echo "Title: ".$events['titel']."<br>";
echo "Detail: ".$events['content']."<br>";
?>
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="delete" value="Delete"><br >
<input type="submit" name="edit" value="Edit"><br >
</form>
<?php
if(isset($_POST['delete'])){
$user = $_POST['delete'];
$delet_query = mysql_query("DELETE FROM kalender_contents WHERE `id` = '$events'") or die(mysql_error());
if($delet_query) {
echo "event deleted";
echo $events;
}
}
}
}
}
?>
But the WHEREid= '$events' does not work. How can I specify the ID on the button click.
Try to put its relevant event id into the form like
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="event_id" id="event_id" value="<?php echo $events['id'];?>">
<input type="submit" name="delete" value="Delete"><br >
<input type="submit" name="edit" value="Edit"><br >
</form>
And in your submit query use like
$delet_query = mysql_query("DELETE FROM kalender_contents
WHERE `id` = $_POST['event_id']");
Whenever you are submitting the form then its event id will be passed and you need to pass that post variable to your delete query.
Further you need to escape the id to prevent the sql injection with mysql_real_escape_string at your query like
$delet_query = mysql_query("DELETE FROM kalender_contents
WHERE `id` = ".mysql_real_escape_string($_POST['event_id']));
Create a hidden field with value of event id.
<h2>Date <?php echo "$day"."/"."$month"."/"."$year"."<br>"; ?></h2>
<?php
// event weer gegeven
while ($events = mysql_fetch_array($resultEvents)){
echo "<strong>Event ID:</strong> ".$events['id']."<br>";
echo "Added: ".$events['added']."<br>";
echo "Title: ".$events['titel']."<br>";
echo "Detail: ".$events['content']."<br>";
?>
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="event_id" value="<?php echo $events['id'];?>">
<input type="submit" name="delete" value="Delete"><br >
<input type="submit" name="edit" value="Edit"><br >
</form>
<?php
if(isset($_POST['delete'])){
$user = $_POST['delete'];
$delet_query = mysql_query("DELETE FROM kalender_contents WHERE `id` = '$events'") or die(mysql_error());
if($delet_query) {
echo "event deleted";
echo $events;
}
}
}
}
}
?>
I have a submit button on the page submitting and it comes back blank and does not update , the echo is also blank.
<?
if (isset($_POST['submit'])) {
$newtext = mysql_real_escape_string($_POST['text']);
$doTextEdit = "UPDATE `$database_main`.`texts` SET `texts`.`text` = '$newtext' WHERE `texts`.`id` = '$sid' LIMIT 1" or die(mysql_error());
$retval = mysql_query( $doArtistEdit, $main );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
header("Location: ".$_SERVER['HTTP_REFERER']);
}
?>
This is in the form code
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="$ID" method="post">
Song Name : <input type="text" name="title" value="<?php echo $row_song['title']; ?>"> by Artist Name :<input type="text" name="artist" value="<?php echo $row_artist['artist']; ?>"><br><?php echo $sid; ?><?php echo $database_main; ?><?php echo $doTextEdit; ?>
Lyrics :<br><textarea name="text" cols="72" rows="20"><?php echo str_replace("\n","\n",$row_Recordset1['text']); ?></textarea><br>
<input type="submit" name="editlyrics" value="Edit"></form>
You are checking if $_POST['submit'] is set -
if (isset($_POST['submit']))
But your submit button name is editlyrics, so it should be -
if (isset($_POST['editlyrics']))
Add a hidden input inside your form -
<input type="hidden" name="song_id" value="<?php echo $sid; ?>">
And now your code would be something like-
<?
if (isset($_POST['editlyrics'])) {
$song_id = mysql_real_escape_string($_POST['song_id']);
$newtext = mysql_real_escape_string($_POST['text']);
$doTextEdit = "UPDATE `$database_main`.`texts` SET `texts`.`text` = '$newtext' WHERE `texts`.`id` = '$song_id' LIMIT 1" or die(mysql_error());
$retval = mysql_query( $doTextEdit, $main );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
header("Location: ".$_SERVER['HTTP_REFERER']);
}
?>
Note that you should not be writing code using the outdated mysql_ functions, and learn either mysqli or PDO - php.net/manual/en/mysqlinfo.api.choosing.php
You are setting up a SQL query in $doTextEdit but you are passing $doArtistEdit to the mysql_query() function.