Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Newbie: Jquery controls in a script

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Newbie: Jquery controls in a script

Postby danfields » Mon May 13, 2013 4:50 pm

I have inherited a PHP project and I am learning PHP as I do the work. Most of the project involved migrating data and a few scripts. I got all of that working. There is a form to which the users want a time field added that is connected to a jquery TimePicker.

The form already has a jquery DatePicker that works. I was able to add the second input, but I cannot make it display a date or time picker. I tried to copy the code from the date field, but that did not work.

Can anyone show me what I need to do? The code is listed below.

Code: Select all
<?php
session_start();
  // connect to the database
  include('db_connect.php');
 
  $error = false;
 
  if (empty($_SESSION['userid'])) {
   Header("Location: index.php");
  }
  else {


  // run this only, once the user has hit the "Add Contact" button

  if (isset($_POST['addevent'])) {

    // assign form inputs

    $date = mysql_real_escape_string($_POST['date']);

    $location = mysql_real_escape_string($_POST['location']);

    $type = mysql_real_escape_string($_POST['type']);

    $title = mysql_real_escape_string($_POST['title']);

    $comments = mysql_real_escape_string($_POST['comments']);

    $auto_push = mysql_real_escape_string($_POST['auto_push']);
   
   if($auto_push != 'T') {
      $auto_push = 'F';
   }

    $status = mysql_real_escape_string($_POST['status']);
    $timeend = mysql_real_escape_string($_POST['timeend']);
       

    // validate inputs

    if ( !empty($date) && !empty($title) && !empty($location) && !empty($type) && !empty($status)) {   

      // add member to database

      $query = "INSERT INTO events (date,location,type,title,comments,auto_push,status,created) VALUES ('".$date."','".$location."','".$type."','".$title."','".$comments."','".$auto_push."','".$status."','".date('Y-m-d H:i:s')."')";

      $result = mysql_query($query);

     

      $message = "\"".$title."\" has been successfully added.";
      Header("Location: list_events.php");
      exit;
    }
    else {
      $error = true; // input validation failed
    }
  }
?>

<html>
<head>
  <title>Add an Event</title>
  <link href="css/default-admin.css" rel="stylesheet" type="text/css" />
     <link type="text/css" href="css/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
   <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
   <script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
   <script type="text/javascript" src="js/timepicker.js"></script>
</head>
<body style="background: #e6e6e6; color: #1d3060;">

<script type="text/javascript">
$(function() {
    $('#date').datepicker({
       duration: '',
        showTime: true,
        constrainInput: false,
      dateFormat: 'yy-mm-dd',
      time24h: true
     });
});
</script>

<? include('menu.inc.php'); ?>
<h1>Add an Event</h1>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

 
<?php

  if ( !empty($message) ) {

    echo '<span style="color:green">',$message,'</span><br><br>',"\n";

    }

?> 
<?php

  if ( $error && empty($title) ) {
    echo '<span style="color:red">Error! Please enter a title.</span><br>',"\n";
  }

?>
  Event Title:
  <input name="title" size=75 type="text" value="<?php echo $title; ?>"> <br>
<?php
    if ( $error && empty($date) ) {
      echo '<span style="color:red">Error! Please enter a date.</span>',"\n";
    }
?>
  Event Date:
    <input name="date" type="text" id="date" value="<?php echo $date; ?>"><br>
   
<?php
  if ( $error && empty($location) ) {
    echo '<span style="color:red">Error! Please enter a location.</span><br>',"\n";
  }
?>

  End Time:
  <input name="timeend" type="text" id="timeend" class="time" value="<?php echo $timeend; ?>"><br>

  Event Location:

  <input name="location" type="text" value="<?php echo $location; ?>">

  <br>
  <br>

  <?php

  if ( $error && empty($type) ) {

    echo '<span style="color:red">Error! Please enter a type.</span><br>',"\n";

  }

?>
  Event Type:
 
  <select name="type">
      <option value="0" <?php if (empty($type)) echo "selected"; ?>>Select</option>
     <option value="Community" <?php if ($type=="Community") echo "selected"; ?>>Community</option>
     <option value="Campus" <?php if ($type=="Campus") echo "selected"; ?>>Campus</option>
  </select>
  <br>

Event Comments:<br>

<textarea name="comments" cols="40" rows="2"><?php echo $comments; ?></textarea><br>
(Text to be displayed in addition to other event info)
<br>

  <br>

  <?php

  if ( $error && empty($status) ) {

    echo '<span style="color:red">Error! Please enter a status.</span><br>',"\n";

  }

?>
    Event Status:
 
  <select name="status">
      <option value="0" <?php if (empty($status)) echo "selected"; ?>>Select</option>
     <option value="active" <?php if ($status=="active") echo "selected"; ?>>Active</option>
     <option value="inactive" <?php if ($status=="inactive") echo "selected"; ?>>Inactive</option>
  </select>
 
  <br>
  <br>

    <?php  }?>

    <input type="submit" name="addevent" value="Add Event">

 </form>
</body>
</html>
danfields
New php-forum User
New php-forum User
 
Posts: 5
Joined: Mon May 13, 2013 4:25 pm

Re: Newbie: Jquery controls in a script

Postby pbs » Mon May 13, 2013 7:22 pm

Try replacing you javascript code with this code

Code: Select all
<script type="text/javascript">
$(function() {
    $('#date').datepicker({
       duration: '',
        showTime: true,
        constrainInput: false,
      dateFormat: 'yy-mm-dd',
      time24h: true
     });
    $('#timeend').datepicker({
       duration: '',
        showTime: true,
        constrainInput: false,
      dateFormat: 'yy-mm-dd',
      time24h: true
     });
   
});
</script>
pbs
New php-forum User
New php-forum User
 
Posts: 39
Joined: Sat May 11, 2013 1:40 am
Location: Nashik, India

Re: Newbie: Jquery controls in a script

Postby danfields » Mon May 13, 2013 8:01 pm

Perfect! Thank you! Those are the little details that make learning a new language difficult. I was close on my first try, but thought I had to have an additional "function" statement.

Now I know.
danfields
New php-forum User
New php-forum User
 
Posts: 5
Joined: Mon May 13, 2013 4:25 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 8 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.