hello friends, first i want say i'm sorry for the title it is very strange.
i have a page where a user insert data in DB table, include he's username "email" but i whant when user click to insert if he is not log in the website to show a pop up page to require him to log in or register like on this picture 48b3e9678dee21ce0fcc5aa172b015c1
here is my code

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "formulaireajout")) {
  $insertSQL = sprintf("INSERT INTO data (client, article, SKU, quantity, shipping, prix, total) VALUES (%s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['client'], "text"),
                       GetSQLValueString($_POST['article'], "text"),
                       GetSQLValueString($_POST['SKU'], "text"),
                       GetSQLValueString($_POST['quantity'], "int"),
                       GetSQLValueString($_POST['Shipping'], "double"),
                       GetSQLValueString($_POST['prix'], "double"),
                       GetSQLValueString($_POST['total'], "double"));

  mysql_select_db($database_marketbase, $marketbase);
  $Result1 = mysql_query($insertSQL, $marketbase) or die(mysql_error());

here is the form

<form action="<?php echo $editFormAction; ?>" method="POST" name="formulaireajout" class="formulaireajout" id="formulaireajoutid">
        <label>Product<input name="article" type="text" id="champajout" value="<?php echo $row_itemdetaille['article']; ?>" readonly></label>
        <label>Select Color<select name="aditional" id="aditional">
        <option value="black" selected="selected">select color</option>
           <option value="black">Black</option>
           <option value="white">White</option>
         </select></label><br>
         <label>Price<input name="prix" type="text" id="pricefield" value="<?php echo $row_itemdetaille['prix']; ?>" readonly></label>
         <label> Quantity<input name="quantity" type="number" min="1" max="20" value="1" onKeyPress="calculateTotal()" id="quantityfield"></label>
        <label>Shipping<select name="Shipping" id="shipping"> Shipping
        <option value="0" selected="selected">Shipping</option>
           <option value="0">0.00</option>
           <option value="9.80">9.85</option>
           <option value="19.60">19.80</option>
         </select></label>
        <label>Total<input name="total" type="text" id="totalfield" value="<?php echo $initialTotal;?>" readonly></label>
         <input name="client" type="hidden" value="<?php echo $_SESSION['MM_Username'];?>">
         <input name="addtocart" type="submit" id="submittocart" value="INSERT">
         <input type="hidden" name="MM_insert" value="formulaireajout">
        </form>
Member Avatar for iamthwee

Logic:

If session is not set redirect to another page with logon prompt.

How you implement that in your code is up to you.

there is my problem but i tried this and i got a syntax error on line 15

mysql_select_db($database_marketbase, $marketbase);
  $Result1 = mysql_query($insertSQL, $marketbase) or die(mysql_error());

  $insertGoTo = "../myaccount/shopping-cart.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
if (!isset($_session))$insertGoTo = "../myaccount/login.php";
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  header(sprintf("Location: %s", $insertGoTo));
}();
Member Avatar for iamthwee

Try changing line 15 to just }

it don't work with } and delete it () and } it work but the logic i not what i need because when user go to the page i need if he is no login he can't view the the page it direct redirect to login page, i want ot redirect only is he try insert data in db and he is not login. thank you.

Why not do something simple like store a value in a hidden element such as true or false representing if the user is logged on. When the user tried to take the action, check this value. If the user is not logged on, show the login dialog box.

This would prevent the post back action.

However, you still have to check the user status when the post back does occur since you can't be 100% sure the client side check is working as expected.

This solution will likely work in most instances without a postback.

thank you to reply
you can't see on my post the client field is hiden...:
all what you say i think is what i find but i don't know how to do

someone can help here please??? to create a fonction who display a pop up window if user are trying to insert data into db when i fied miss????

Member Avatar for iamthwee

The problem is a lack of clarity with your request. The other problem is the answer relates to how you have written the rest of your code as well.

what you told me to do before is not what i need cause when user go to page direcly he will redirect to other page what i need is if use try submit when he is not connect to be redirect

i have generated this js code it check if field is empty and it alert error message it generated in dreamweaver
how can i make the alert message to show popup window like the pictures

<script type="text/javascript">
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }
</script>

6fae4c3d45739407724f445a8e788e52

please somebody help me here??

please somebody help me here??

You have been getting help. There has been some advice and guidance provided but you have indicated that is not what you wanted.

what you told me to do before is not what i need cause when user go to page direcly he will redirect to other page what i need is if use try submit when he is not connect to be redirect

If you read over my previous suggestion, I think my solution would work. If a user tries to submit, but the user is not logged in, you should prompt the user to log in, correct? Ok, then you need to handle that event. When the user hits the submit button, you need to figure out if the user is logged in. How can you do that? One way is to store the value of being logged on somewhere... a hidden element, a cookie, etc... If you dont handle it client side, then you would allow the page to post back and then check the logon status server side. You would have that logon status in a session variable, cookie, etc...

I think it should be relatively easy to handle this client side (and then have server side code to make sure once you allow the user to submit to make sure that your client side code wasnt bypassed). You can have a hidden element that stores either true or false. When the user hits the submit button, check that value. If its false, the user is not logged in, therefore show the popup for the user to log in. If the user is logged in, then allow the process to continue.

If you want someone to provide you with the exact code, not sure if that is going to happen.

commented: well said +14
Member Avatar for iamthwee

Just a few thoughts...

If what I understand is correct, you are simply asking you have some sort of forum like daniweb where you have a message box. The user cannot submit the answer box unless he is logged in.

Normally, you would just check if their session is logged in then redirect to a login page if they are not. Your request is slightly different because your login is actually a modal box rather than a page. In that case you would need jquery to help you along so authentication can be handled asynchronously without a page refresh.

The reason, I'm hesitant to help (and probably others too) is because I know this is going to be a case that will span several pages, looking at your recent request in the javascript forums you don't even know how to create a running total.

http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/471742/how-to-set-do-and-while-loop-on-a-exissting-jquery-function

I would recommend taking the time out to learn jquery with PHP properly. Otherwise it is just going to be a constant yo-yo with us and nothing ever gets truly learned properly.

Good luck

thank you for help "i called someone can help here just cause didn't know what to do next" brief
it will be better for me to do in client side for it i have hide the client username field with css, and place js client side verification when user is not connect it allert the the field client is required but how can i show the popup for the user to log in ???? i have my connection phph code
here is the js code who alert the error mesage.
HTML

<form action="<?php echo $editFormAction; ?>" method="POST" name="formulaireajout" class="formulaireajout" id="formulaireajoutid" onSubmit="MM_validateForm('clientmail','','RisEmail');return document.MM_returnValue">

JAVASCRIPT

<script type="text/javascript">
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }
</script>

should i creat a div where i will include login page ???
and to raplace in alert getelementbyid ??? id of div who include login ??

Member Avatar for iamthwee

I would recommend taking the time out to learn jquery with PHP properly. Otherwise it is just going to be a constant yo-yo with us and nothing ever gets truly learned properly.

^^Re-read the above for clarification.

And just as a side note always authenticate these things server side. It really is the only way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.